接收通过ajax发送的php中的序列化数据并循环通过它

时间:2015-12-08 20:32:47

标签: php jquery ajax serialization deserialization

我通过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>

2 个答案:

答案 0 :(得分:1)

你需要重命名你的项目以便能够发布数组(即称之为“what”+“[]”并在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)。您已经将id作为隐藏字段发布,为什么要将其作为两次?一个覆盖另一个(因为名称必须是唯一的)。

答案 1 :(得分:0)

除了@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>
相关问题