Reputation: 2415
I am sending a form data via ajax call to a php script. I am serializing the data in ajax and on the php script I want to loop through that data to extract the values. This is my ajax call
$("#submitAttendance").click(function(){
var data = $('form#attendanceForm').serialize();
$.ajax({
url: 'save-attendance.php',
method: 'post',
data: {formData: data},
success: function(data){
console.log(data);
alert(data);
}
});
});
and in the attendance.php
I am doing
print_r(($_POST['formData']));//prints the entire serialize data
when I do this
parse_str($_POST['formData'], $searcharray);
print_r(($searcharray));//prints only last user and all radio buttons
I want to extract values so I can save it in db. This is my form
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
while($row = $result->fetch_assoc()){ ?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
<td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
</tr>
<?php }
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>
Upvotes: 0
Views: 2963
Reputation: 2415
In addition to @Jan answer I did following to get the complete data and loop through it
parse the incoming data
parse_str($_POST['formData'], $searcharray);
then loop through the array
for ($i = 0 ; $i <= sizeof($searcharray) ; $i++){
$name = $searcharray['name'][$i];
$email= $searcharray['email'][$i];
$class = $searcharray['class'][$i];
$present= ($searcharray['present'][$i]);
}
and my form code is
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
$i=0;
while($row = $result->fetch_assoc()){
?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name[]" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email[]" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class[]" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="present[<?php echo $i; ?>]" checked></td>
<td><input type="radio" value="absent" name="present[<?php echo $i; ?>]"></td>
</tr>
<?php $i++;
}
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>
Upvotes: 0
Reputation: 43169
You need to rename your items to be able to post arrays (that is call them "whatever" + "[]" and loop over them in PHP), e.g.:
HTML:
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
while($row = $result->fetch_assoc()){ ?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name[]" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email[]" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class[]" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
<td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
</tr>
<?php }
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>
Later in PHP:
foreach ($_POST["formData"]["name"] as $name)
echo "Wow, $name is a really pretty name!";
Additionally, I am not sure what present
and absent
are meant to do and why they should have the same name (an id). You are already posting the id as an hidden field, why should it be done twice? One overrides the other one (as the names have to be unique).
Upvotes: 1