Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将php值返回到jQuery post_Php_Jquery - Fatal编程技术网

如何将php值返回到jQuery post

如何将php值返回到jQuery post,php,jquery,Php,Jquery,我就是想不起来。我需要在php文件中写些什么才能将某些内容返回到$.post函数 <script type="text/javascript"> function SubmitToDatabase(uName, uComment) { $.post("write_something_to_mysql_b.php", {name: uName, comment: uComment}); return false; } </s

我就是想不起来。我需要在php文件中写些什么才能将某些内容返回到$.post函数

<script type="text/javascript">
    function SubmitToDatabase(uName, uComment)
    {
        $.post("write_something_to_mysql_b.php", {name: uName, comment: uComment});
        return false;
    }
</script> 

这是可行的,但是PHP文件没有返回值。首先,确保jQuery正在访问您的PHP文件。使用Chrome或Firefix中的开发者工具,转到“网络”选项卡,在执行JavaScript函数SubmitToDatabase时,查看任何网络活动。如果它访问了错误的PHP页面,它将显示404

如果它正在访问正确的PHP页面,请检查Firebug以查看返回的值。要在PHP中为JavaScript调用返回值,需要确保使用的是echo而不是return

json只是需要后端数据的可能性之一。其他有:xml、json、脚本、文本、html。对于这些格式中的每一种,您必须以合适的方式从后端返回数据。例如,对于文本,只需回显“hello!!!”!!!;退出

见。如果要对PHP结果执行某些操作,必须添加一个success函数

function SubmitToDatabase(uName, uComment)
{
    $.post("write_something_to_mysql_b.php", {name: uName, comment: uComment},
        function(data) { alert(data); });
    return false;
}

这与PHP无关;这是AJAX请求异步性质的基本属性;仔细阅读。有大量的工作示例。请使用完整回调。。。如文档中所述,请阅读如何操作是的!哇!非常感谢你@不客气。其他的答案也是很好的贡献…有些东西是不经常写页面的人猜不到的。我已经学习了好几个小时了。@wubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewubbewub是的,这就是为什么我们有!但首先是自学。好!+1获取生命之水的链接:+1获取有关使用firebug进行调试以检查php响应的重要建议
function SubmitToDatabase(uName, uComment)
{
    $.post("write_something_to_mysql_b.php", {name: uName, comment: uComment},
        function(data) { alert(data); });
    return false;
}