How do I create an uploda form that reads data from file and outputs that data to the page via AJAX?

I am looking to integrate a feature onto my website that reads the data from a JSON file and then outputs that data, all via AJAX.

A bit of background is that I am creating a specific online tool where users can upload and view a JSON file without needing software installed on their PC to do so. The goal is for the user to drag and drop their JSON file into my web page, or click a file select button on the page and choose the JSON file. The background JSON decode would then translate all information from that file, perform some math calculations and output it directly below the file box/select via AJAX without a page load.

The process would be;

  • Form on my website where user selects a JSON file from their computer (file not uploaded to site, just read)
  • The file is then read, decoded using the following or similar code
$json = file_get_contents($url, false, stream_context_create($arrContextOptions)); // Get content from JSON file
$data = json_decode($json); // Decode the JSON setup file
  • That code is then outputted to the page just below the form using code similar to the below
<script>
function formsubmit() {
    event.preventDefault();
            //AJAX Output
            sendData = {
                action: 'query_ajax_function',
                final_output: final_output,
              };
            //AJAX Request
            jQuery.ajax({
              url: ajaxadminurl.ajaxurl,
              type: "POST",
              data: sendData,
              dataType: "html",
              cache: false,
              success: function(data) {
                   $('.response').html(data);
                }
            })
        });
}
</script>

How would I go about implementing the upload button and set this up to acheive my desired result. I don’t want to upload the file that is being read, just have a way of the user adding the JSON file to this reader to read the output.

I have successfully created all math equations to read the JSON data from a specific URL using the below code. However, this code allows me to choose a file via a specific URL (in this case, via an ACF custom field).

<?php
$url = get_field('json_file'); // Gets the custom uploaded JSON setup file from URL
$arrContextOptions=array(
      "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    );          
$json = file_get_contents($url, false, stream_context_create($arrContextOptions)); // Get content from JSON file
$data = json_decode($json); // Decode the JSON setup file
?>

I am not sure how to change this to allow the user to choose a file from their computer, and then perform the same JSON decode and file output that I have already created via AJAX. This is the part I need help with.

Thanks

New contributor

RichRBX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật