-2

i have form with input field generated from loop, ie

<form name="" method='post' action=''>
<?php
for($i=0;$i<10;$i++){
 echo "<input type='text' name='data[]' class='data_cls' value='".$i."'>";
}
?>
<input type='submit' id='btn' value='save'>

i want to submit the form using jquery ajax.

$('.btn').click(function(){

            var datString = "HOW TO GET THESE VALUES";

            $.ajax({
            url: "data_process.php",
            type: "post",
            data: dataString,
            success: function(data) {
                alert('OK');
            }
        }); 

    });
Jasir alwafaa
  • 586
  • 1
  • 7
  • 24
  • 1
    Use `serialize()`. Then in your `$.ajax` call: `data: $('form').serialize();` – Rory McCrossan Aug 08 '16 at 12:32
  • 1
    Possible duplicate of [serializing and submitting a form with jQuery POST and php](http://stackoverflow.com/questions/15173965/serializing-and-submitting-a-form-with-jquery-post-and-php) – KAD Aug 08 '16 at 12:35
  • The best way to do this really depends on what your desired result is. – Patrick Q Aug 08 '16 at 12:36
  • @Jasiralwafaa check ma ans, i have also added how you could receive the data on the PHP page. Hope the solutions would help – Omari Victor Omosa Aug 08 '16 at 20:11

5 Answers5

3

You can use .serialize() jQuery method to get the form data. Like this,

$('.btn').click(function(){

            var dataString = $('#FORM_ID').serialize();
            //replace FORM_ID with the ID of the form.

            $.ajax({
            url: "data_process.php",
            type: "post",
            data: dataString,
            success: function(data) {
                alert('OK');
            }
        }); 

    });
Alok Patel
  • 7,842
  • 5
  • 31
  • 47
0

Try this:

var datString = "";

$(".data_cls")
        .map(function () {
            datString += $(this).val();
        })
        .get();
rokas
  • 1,521
  • 9
  • 16
0

Try

var Data = {};
$('input[name=data]').each(function(i) {
  Data[i] = $(this).val();
});
porkbrain
  • 720
  • 5
  • 16
0

I think you have forgot to bind parameter in data ajax post

$('.btn').click(function(){

            var datString = "'HOW TO GET THESE VALUES'";

            $.ajax({
            url: "data_process.php",
            type: "post",
            data: "'USERPARAMETR' : " dataString,
            success: function(data) {
                alert('OK');
            }
        }); 

    });

If you not use parameter in data_process.php page. Please use it.

Please update and check

0

First give the form an ID or class i.e. form

<form name="" method='post' action='' id='form'>...

Then you may capture all inputs from the from using the general serialize method

<script type="text/javascript">
$(document).on("click", ".btn", function(e) {
e.preventDefault();
var datastring = $("#form").serialize();

{
        $.ajax({
        url: "data_process.php",
        type: "post",
        data: dataString,
...

And on the PHP page you will receive the data as posted

$values = $_POST['data'];

$N = count($values);

for($i=0; $i < $N; $i++)
{

   $currentvalue = $values[$i];
   //now this value will loop and you will do with it the way you want to
   //For example
   $save =  mysqli_query($link, "UPDATE table SET `column` = '$currentvalue' WHERE ...") or die(mysqli_error($link));

}
Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46