[英]How do I loop serialize data append in formData sent via AJAX in PHP?
[英]receive serialize data in php sent via ajax and loop through it
我通過ajax調用向php腳本發送表單數據。 我正在序列化ajax中的數據,並在php腳本中要遍歷該數據以提取值。 這是我的ajax電話
$("#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);
}
});
});
而在attendance.php
我做
print_r(($_POST['formData']));//prints the entire serialize data
當我這樣做
parse_str($_POST['formData'], $searcharray);
print_r(($searcharray));//prints only last user and all radio buttons
我想提取值,以便可以將其保存在db中。 這是我的表格
<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>
您需要重命名項目以能夠發布數組(即稱它們為“ whatever” +“ []”並在PHP中循環它們),例如:
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>
稍后在PHP中:
foreach ($_POST["formData"]["name"] as $name)
echo "Wow, $name is a really pretty name!";
此外,我不確定present
和absent
的含義以及為什么它們應該具有相同的名稱(ID)。 您已經將ID作為隱藏字段發布了,為什么要進行兩次? 一個優先於另一個(因為名稱必須唯一)。
除了@Jan答案,我還按照以下步驟獲取完整的數據並遍歷它
解析傳入的數據
parse_str($_POST['formData'], $searcharray);
然后遍歷數組
for ($i = 0 ; $i <= sizeof($searcharray) ; $i++){
$name = $searcharray['name'][$i];
$email= $searcharray['email'][$i];
$class = $searcharray['class'][$i];
$present= ($searcharray['present'][$i]);
}
我的表單代碼是
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.