来自php变量的jQuery帖子

时间:2015-07-12 16:41:17

标签: php jquery-plugins

我正在创建一个'拖放'排序列表,我抓了一些现有的代码,我需要修改它以满足我的需求。但是我并没有完全理解代码正在做什么才能正确地修改它。

基本上我有一个PHP变量'$ draft_id',我需要传递给我的delete[]。如果我只是在delete中定义updateList.php,它可以正常工作,但我需要能够传递此值。

代码如下:

(的index.php)

$draft_id=0

(updateList.php)

updateList.php

所以基本上在'updateList.php'中我想从$ _POST数组中取出'draft_id',但我不确定如何正确传递它。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

经过一些研究,我已经弄清楚如何通过它如何通过它,所以我想我将分享以防其他人需要它。所以我的主要问题是我不确切地知道如何设置$ _POST数组。

在Javascript中有一个$ .post,它使用在它上面定义的变量'order'。这是传递$ _POST数组的地方。因此,通过添加附加条件,您可以在$ _POST数组中传递其他变量。

所以我改变了:

<script type="text/javascript">
$(document).ready(function(){
function slideout(){
  setTimeout(function(){
    $("#response").slideUp("slow", function () {});
}, 2000);}
$("#response").hide();
$(function() {
    $("#list ul").sortable({ opacity: 0.8, cursor: 'move', update:function() {
        var order = $(this).sortable("serialize") + '&update=update';
        $.post("updateList.php", order, function(theResponse){
            $("#response").html(theResponse);
            $("#response").slideDown('slow');
            slideout();
        });
    }
  });
  });
});
</script>

为:

<?php   //this will later be pulled from another page, but for now
$draft_id='0';  //I just set it up for testing purposes.
$_POST['draft_id']=$draft_id;
?>
<script type="text/javascript">
$(document).ready(function(){
function slideout(){
  setTimeout(function(){
    $("#response").slideUp("slow", function () {});
}, 2000);}
$("#response").hide();
$(function() {
    $("#list ul").sortable({ opacity: 0.8, cursor: 'move', update:function() {

    //we will add &draft_id='the php variable $draft_id to the order variable'

        var order = $(this).sortable("serialize") + '&update=update' + '&draft_id=<?php echo $draft_id; ?>';
        $.post("updateList.php", order, function(theResponse){
            $("#response").html(theResponse);
            $("#response").slideDown('slow');
            slideout();
        });
    }
  });
  });
});
</script>

这解决了我的问题,希望如果他们遇到同样的问题,这可以帮助将来的人。

相关问题