简体   繁体   English

JQuery ajax 发布问题与 PHP

[英]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, 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["v_type"]."\",\"".$rows["v_id"]."\");'>Play</a>

below is passing values to jquery function:以下是将值传递给 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);"问题来了,“$('#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:总是在源代码中返回一整页而不是只保留值,我想要的只能发布“data1”和“data2”并分配给 php 变量和 mysql 查询,如下所示:

$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?如何使用 JSON 将这些数据传递给 mysql 查询以检索最终结果,有人可以帮忙吗?

Thanks very much.非常感谢。

your script mtv.php will recieve them in the $_POST global.您的脚本 mtv.php 将在 $_POST 全局中接收它们。

you should be able to get the params with $_POST['var1'], $_POST['var2']您应该能够使用 $_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.哦,如果你想从 php 返回一些数据,你可以返回 XML 或 JSON,只需 alert(data) 即可查看刚刚返回的内容。

In mtv.php, use json_encode() and echo it, this will get returned as 'data' which you can then do something liek this:在 mtv.php 中,使用 json_encode() 并回显它,这将作为“数据”返回,然后您可以执行以下操作:

Example return JSON: {'thing': 1234}示例返回 JSON: {'thing': 1234}

var myObject = $.parseJSON(data);    

alert(myObject.thing);

The alert would display 1234.警报将显示 1234。

How do you retrieve the data in php?您如何检索 php 中的数据? You should be able to fetch it like so:您应该能够像这样获取它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM