开发者

JQuery ajax post problem with PHP

I have a list of records which loop from mysql table, for each record i had assigned an onclick function in order to pass the data for mysql query use to avoid page refresh whenever it was call,

<a href='javascript:void(0)' onclick='PlayMV(\"".$rows开发者_Go百科["v_type"]."\",\"".$rows["v_id"]."\");'>Play</a>

below is passing values to jquery function:

<script type="text/javascript">
function PlayMV(data1, data2){
$.post("mtv.php", { var1: "data1", var2: "data2" },
    function(data){
    $('#result').html(data);
});
}
</script>

here comes the problem, the "$('#result').html(data);" was always returned me a whole page in source code instead of held only the values, what I want is only able to post 'data1' and 'data2' and assigned into a php variable and mysql query like below:

$var1 = data1;
$var2 = data2;
$q = mysql_query("SELECT * FROM table WHERE mvtype='".$var1."' AND mvid='".$var2."'");

how to use JSON to pass those data into mysql query to retrieve the final result, can anyone help?

Thanks very much.


your script mtv.php will recieve them in the $_POST global.

you should be able to get the params with $_POST['var1'], $_POST['var2']

Script should be:

<script type="text/javascript">
    function PlayMV(data1, data2){
        $.post("mtv.php", { var1: data1, var2: data2 },
            function(data){
                alert("Data Loaded: " + data);
        });
    }
</script>

You had quotes round the values.

EDIT

Oh, if you want some data BACK from php, you can return XML or JSON, simply alert(data) to see what IS being returned just now.

In mtv.php, use json_encode() and echo it, this will get returned as 'data' which you can then do something liek this:

Example return JSON: {'thing': 1234}

var myObject = $.parseJSON(data);    

alert(myObject.thing);

The alert would display 1234.


How do you retrieve the data in php? You should be able to fetch it like so:

$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜