来自php变量的jQuery帖子

I'm in the process of creating a 'drag-and-drop' ordering list, and I grabbed some code for an existing one and I need to modify it to fit my needs. However I do not fully understand exactly what the code is doing in order to modify it correctly.

Basically I have a php variable '$draft_id' that I need to pass into my updateList.php. If I just define $draft_id=0 within updateList.php it works fine, but I need to be able to pass this value in.

The code looks like:

(index.php)

<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
include("connect.php");
$draft_id='0';
$query  = "SELECT id, text FROM sort WHERE draft_id =" . $draft_id . " ORDER BY listorder ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{   
   $id = stripslashes($row['id']);
   $text = stripslashes($row['text']);
   echo "<li id='arrayorder_" . $id . "'>" . $text;
   echo "<div class='clear'></div>";
   echo "</li>";
}
?>

(updateList.php)

<?php 
include("connect.php");
$array  = $_POST['arrayorder'];
if ($_POST['update'] == "update"){
     $count = 1;
        //------------------------------------
        $draft_id=$_POST['draft_id'];
        //------------------------------------
        foreach ($array as $idval) {
        $query = "UPDATE sort SET listorder = " . $count . " WHERE id = " . $idval . " AND draft_id=" . $draft_id;
        mysql_query($query) or die('Error, insert query failed');
        $count ++;  
    }
    echo 'Draft Order Saved!';
    }
?>

So basically in the 'updateList.php' I'd like 'draft_id' to be taken from the $_POST array, but I'm not sure how to pass it correctly. Any help would be appreciated.

After some research I've figured out how pass this how it needs to be passed, so I figured I'd share in case anyone else needs it in the future. So my main issue was that I did not understand exactly how the $_POST array was being set.

In the Javascript there is a $.post that is using a variable 'order' which is defined above it. This is where the $_POST array is being passed. So by adding an additional condition you can pass additional variables in the $_POST array.

So I changed:

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

to:

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

This fixed my issue, hopefully this can help someone out in the future if they run into this same issue.