Question:
How to send multiple HTML form fields to PHP arrays via Ajax?

Problem

I'd like some help. I have the HTML form fields below and I want them to be sent to a PHP page to individual PHP arrays.

HTML

<input class="form-control" type="date" onchange="reportSave()" name="testreportname[]">

<input class="form-control" type="date" onchange="reportSave()" name="testreportdate[]">

<input class="form-control" type="date" onchange="reportSave()" name="testreportissue[]">



Javascript:

<script>

    function reportSave() {

        //Test Report Name

        //get the data from test report name

        var testreport=document.getElementsByName('testreportname[]');

        var testreportdate=document.getElementsByName('testreportdate[]');

        var testreportissue=document.getElementsByName('testreportissue[]');

        var data1 = $(this).serializeArray();

            

        //for each item in the array

        for(key=0; key < testreport.length; key++)  {        

                    

        data1.push({

        testreport: $("#testreport").val(), testreportdate: $("#testreportdate").val(), testreportissue: $("#testreportissue").val(),

        });

        console.log("key: ", key)

        console.log(testreport)

        }

        

    //send to PHP via ajax

        $.ajax({

            type: "post",

            url: "draftsave.php",

            dataType: "json",

            data: data1,

            success: function(result) {

                console.log(result)

                }

            });

    }

</script>


PHP

<?php

$stestreport=$_POST['testreport'];

$stestreportdate=$_POST['testreportdate'];

$stestreportissue=$_POST['testreportissue'];


for($i=0;$i<count($stestreport);$i++)

        { code here, etc};

?>



Edit, removed already tried code.


Solution:

jQuery provides the serialize() function, which provides a really simple way to get all the data from a form or a specific set of fields and convert them into the form-url-encoded format that they would ordinarily be transmitted in if you submitted a regular form (without AJAX). This is the format PHP recognizes and will automatically convert into data in its $_POST array.


This little demo shows


  1. What serialize() outputs

  2. A way to select the specific fields you're interested in using a class name (notice the extra class on each input field in the HTML)

  3. A nicer way to handle the "change" event unobtrusively using a selector, rather than littering the HTML with onchange attributes.


$(".reportField").change(function() {

  var formFields = $(".reportField").serialize();

  console.log(formFields);

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input class="form-control reportField" type="date" name="testreportname[]">

<input class="form-control reportField" type="date" name="testreportdate[]">

<input class="form-control reportField" type="date" name="testreportissue[]">



So if you wanted to use it in your AJAX, you'd end up with something like:


$(".reportField").change(function() {

  var formFields = $(".reportField").serialize();


  $.ajax({

    type: "post",

    url: "draftsave.php",

    data: formFields,

    success: function(result) {

        console.log(result)

    }

  });

});



Answered by: >ADyson

Credit: >StackOverflow


Blogs Link: 

>How to solve the encoding issue when writing to a text file, with Python?

>How to solve problems with Python syntax while writing a calculator app?-Python

>How to solve XGBoost model training fails in Python

>How to route between different components in React.js?

>How to build interactive_graphviz in Python


Ritu Singh

Ritu Singh

Submit
0 Answers