接收通过 ajax 发送的 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

我想提取值以便将其保存在数据库中。 这是我的表格

<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>

您需要重命名您的项目才能 post 数组(即称它们为 "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!";

此外,我不确定 presentabsent 是什么意思,以及为什么它们应该具有相同的名称(id)。您已经 post 将 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>