2

I have this form, that loads the details using jquery load function here is the form

 <form id="form1" name="form1" method="post" action="">
<div id="tableBg">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="list">
<thead>
<tr>
      <th width="39%" scope="col">WHITE LIST</th>
      <th width="61%" scope="col">&nbsp;</th>
      </tr>
      </thead>
</table>

<div style="max-height:400px; overflow-y:auto">
  <table width="100%" border="0" cellspacing="0" cellpadding="0" id="list">
    <tr class="sub_head">
      <td scope="col"><strong>REG. NO.</strong></td>
      <td scope="col"><strong>STUDENT NAME</strong></td>
      <td scope="col"><strong>GRADE</strong></td>
      <td scope="col"><strong>MESSAGE ID</strong></td>
      </tr>
    <tbody></tbody>
  </table>
</div>
<div id="PageFooter">
 <input type="submit" name="save_result" id="save_result" value="Save Results" />
</div>
</div>
</form>

and loads the loads the code below from another page

do{
    ?>
<tr <?php echo (($x%2) != 0) ? '' : ' class="alternate-row row"' ?>>
  <td scope="col"><?php echo $learners['reg_no'];?></td>
  <td scope="col"><?php echo $learners['surname']." ".$learners['full_names']." (".$learners['nickname'].")";?></td>
  <td scope="col"><?php echo $learners['grd']." ".$learners['grade_section'];?></td>
  <td scope="col">
  <input name="sms_id[<?php echo $learners['reg_no']; ?>]" type="text" size="3" /> 
    </td>
  </tr>
<?php $x++;}while($learners = $getLearners->fetch());

this code is within a form and there is a submit button at the bottom. This for is posted to a process.php which with the following code, print's some output

if(isset($_POST['submit'])){

    foreach($_POST['sms_id']  as $reg=>$msg_id){
        echo $reg. " / ".$msg_id;
    }

}

the above displays both values if the form is posted directly from the form to process.php

But I wanted to use ajax and tried the following code that is not working. Just posting the $msg_id but not the other value.

var formData = $("#form1").serializeArray();
            $.ajax({
                url: "models/process.php",
                type: "POST",
                data: formData,
                success: function(data)
                {
                    alert(data);
                }
            });

This code is not working can someone help? Thanks in advance.

J D K

Yoweli Kachala
  • 423
  • 6
  • 13

3 Answers3

4

I don't know if this will be much help but try adding the ajax option traditional

    $.ajax({
        url: "models/process.php",
        type: "POST",
        traditional: true,
        data: formData,
        success: function(data)
        {
            alert(data);
        }
    });

This changes the way Ajax serializes its data.

johnny 5
  • 19,893
  • 50
  • 121
  • 195
3

Actually, you are checking if(isset($_POST['submit'])) in your process.php which was not passing ajax.

$('#form1').serializeArray() doesn't include data for submit button.

Check the documentation here: http://api.jquery.com/serializearray/

Your PHP Code (process.php) should be changed to:

//if(isset($_POST['submit'])){ // this will not be available when using .serializeArray()

    foreach($_POST['sms_id']  as $reg=>$msg_id){
        echo $reg. " / ".$msg_id;
    }
//}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
1

I think the problem its that to send a form, you need input values in your form, so you can send them.

Try to create into hidden fields all the inputs you are trying to send, and use form.serialize(), that should be enought.

Example:

<tr <?php echo (($x%2) != 0) ? '' : ' class="alternate-row row"' ?>>
<td scope="col"><input type="hidden" value="Somevalue" name"attributeName"/>
<?php echo     $learners['reg_no'];?></td>
</tr>
<?php $x++;}while($learners = $getLearners->fetch());

And your ajax call:

var formData = $("#form1").serialize();
        $.ajax({
            url: "models/process.php",
            type: "POST",
            data: formData,
            success: function(data)
            {
                alert(data);
            }
        });
cralfaro
  • 5,822
  • 3
  • 20
  • 30