【问题标题】:getting data posted by ajax into php foreach loop将ajax发布的数据获取到php foreach循环中
【发布时间】:2017-04-20 06:09:44
【问题描述】:

为了清楚起见,我正在更新这个问题。我正在使用可排序的 jquery 和 AJAX(参见下面的代码)来排序和保存图像网格。一切正常,我收到以下成功消息 - 在我的脚本和 firebug 网络控制台上。

            //firebug params and success message output
             item[]=2&item[]=1&item[]=3&item[]=4&item[]=5&item[]=6&item[]=7&item[]=8&item[]=9&item[]=10

            //request header
                Host: example.com
                User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0
                Accept: */*
                Accept-Language: en-US,en;q=0.5
                Accept-Encoding: gzip, deflate
                Referer: http://example.com/PropertyIndex/property_gallery/draggable.php
                Content-Type: application/x-www-form-urlencoded; charset=UTF-8
                X-Requested-With: XMLHttpRequest
                Content-Length: 90
                Cookie: PHPSESSID=b1lr9he4l2hbcnlkcsebfq2134
                Connection: keep-alive
 //request body
 item[]=1&item[]=3&item[]=2&item[]=4&item[]=5&item[]=6&item[]=7&item[]=8&item[]=9&item[]=10

看来数据已成功发布。然而,经过数小时的搜索,我无法弄清楚如何将发布的数据——item[] 数组——返回给 PHP,以便它可以被 FOREACH 循环处理,也显示在下面的代码中:非常感谢任何帮助。

            filename: draggable.php

                <!doctype html>
                <html lang="en">
                <head>
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=device-width, initial-scale=1">
                  <title>jQuery UI Sortable - Display as grid</title>
                  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
                  <link rel="stylesheet" href="/resources/demos/style.css">
                  <style>
                  #sortable { list-style-type: none; margin: 0; padding: 0; width: 450px; }
                  #sortable li { margin: 3px 3px 3px 0; padding: 1px; float: left; width: 100px; height: 90px; font-size: 4em; text-align: center; }
                  </style>
                  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
                  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
                  <script>
                  $( function() {
                    $( "#sortable" ).sortable();
                    $( "#sortable" ).disableSelection();
                  } );
                  </script>
                </head>
                <body>

                <?php
                <!-----------get media_urls from database----------------->

                 <div id="element">
                <ul id="sortable">


                <li id="item-1" class="ui-state-default">     <img class="image" src="<?php echo $media_url1 ?>" width="100px" height="100px">       </li>
                <li id="item-2" class="ui-state-default">     <img class="image" src="<?php echo $media_url1 ?>" width="100px" height="100px">       </li>
                <li id="item-3" class="ui-state-default">     <img class="image" src="<?php echo $media_url2 ?>" width="100px" height="100px">       </li>
                <li id="item-4" class="ui-state-default">     <img class="image" src="<?php echo $media_url3 ?>" width="100px" height="100px">       </li>
                <li id="item-5" class="ui-state-default">     <img class="image" src="<?php echo $media_url4 ?>" width="100px" height="100px">       </li>
                <li id="item-6" class="ui-state-default">     <img class="image" src="<?php echo $media_url5 ?>" width="100px" height="100px">       </li>
                <li id="item-7" class="ui-state-default">     <img class="image" src="<?php echo $media_url6 ?>" width="100px" height="100px">       </li>
                <li id="item-8" class="ui-state-default">     <img class="image" src="<?php echo $media_url7 ?>" width="100px" height="100px">       </li>
                <li id="item-9" class="ui-state-default">     <img class="image" src="<?php echo $media_url8 ?>" width="100px" height="100px">       </li>
                <li id="item-10" class="ui-state-default">     <img class="image" src="<?php echo $media_url9 ?>" width="100px" height="100px">       </li>


                </ul>
                //test results
                Query string: <span></span>
                <p class ="result"></p>

                </div>

                //post the data using ahax
                <script>

                $('ul').sortable({

                    update: function (event, ui) {
                        var data = $(this).sortable('serialize');

                         $('span').text(data);      //test success 2

                        $.ajax({
                            data: data,
                            type: 'POST',
                            url: 'draggable.php',
                            success:function(result){
                            $(".result").html(data);}
                        });  

                    }
                });

                //$(window).resize(resize);

                </script>

                <?php

                print_r()//an empty array--but why?

                $i = 0;

                //this loop is failing to echo the success result ie item[]
                foreach ($_POST['item'] as $value) {
                    echo "each".$value;
                    $i++;
                }
                ?>

                </body>
                </html>

【问题讨论】:

  • php和javascript没有直接连接。您不能使用 javascript 来修改 php,因为 php 在您选择运行时 javascript 的用户输入之前很久就已经执行了。您的 ajax 调用必须使用新值将结果写回 div(希望这是有道理的。我认为您需要将失败的代码放在您的 draggable.php 文件中
  • 您发布的结果保存在哪里 - 数据库、会话、文件?
  • 感谢快乐。请再帮我一点好吗?我不太确定我应该怎么做才能实施您的解决方案。我在我的问题中添加了有关请求标头数据的更多信息。我怀疑我在这里遗漏了明显的东西
  • 目前 Jeff 未保存发布的结果。这就是我试图通过向 foreach 循环添加更新数据库例程来实现的目标。但是现在我在 foreach 循环中看不到 $_POST['item'] 数组的回显 $values,所以我还没有尝试更新数据库。一旦我看到回显的值,我会将数据库更新 sql 添加到 foreach 循环中
  • 快乐——我想我明白了你的意思。我的 ajax url 发布到与 ajax 例程相同的页面,即 daraggable.php。那么我应该将 ajax 发布到新页面,例如 draggable_posted_data.php,然后将 foreach 循环放在该新页面脚本上,以便 PHP 在 ajax 发布之后执行?

标签: javascript php jquery ajax foreach


【解决方案1】:

嗯.. 使用 $.post 可以正常工作。我还建议在另一个 php 文件中处理新序列的数据。

$.post("draggable-processing.php",{
   data:data
},
function(data){
   $(".result").html(data);
});

数据不是一个对象,而是一个字符串,你必须手动分解:

// file draggable-processing.php:
<? 
$aSequence = explode('&',$_POST['data']);
for($i = 0; $i<count($aSequence);$i++){
    preg_match('/=([a-z0-9]*)$/', $aSequence[$i], $treffer);
    echo($treffer[1]."\n");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多