【问题标题】:receive serialize data in php sent via ajax and loop through it在php中接收通过ajax发送的序列化数据并循环遍历它
【发布时间】:2016-03-13 22:32:21
【问题描述】:

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

【问题讨论】:

  • 你不需要做'数据:{formData:数据}'只需做数据:数据。您可以执行普通的 '$_POST['name']' 来检索您发布的值。
  • 是的,您正在传递一个数据数组,因此您需要重命名为数组
  • 我必须做一些循环,因为值是重复的。 @Franco
  • 您现在需要循环遍历 $_POST 数组以获取 php 文件中的值。

标签: php jquery ajax serialization deserialization


【解决方案1】:

除了@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>

【讨论】:

    【解决方案2】:

    您需要重命名您的项目才能发布数组(即称它们为“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)。您已经将 id 作为隐藏字段发布,为什么要执行两次?一个覆盖另一个(因为名称必须是唯一的)。

    【讨论】:

    • 我希望每个用户都有一个 diff id
    • 好的,这很好,它现在给我完整的回复,但是你想告诉我为什么我们必须把[]放在名字前面
    • 如前所述,名称必须是唯一的,因此您需要发送一个数组。因此,它归结为 name[0]、name[1] 等等,它们也是唯一的。
    • 我将如何遍历结果?简单的 foreach 循环不起作用
    • @Adamnick:会看到我更新的答案和最后的 PHP 部分。
    猜你喜欢
    • 1970-01-01
    • 2018-01-14
    • 2015-09-21
    • 2013-08-21
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    相关资源
    最近更新 更多