Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.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
通过AJAX调用后将PHP变量发送回JS_Php_Json_Ajax - Fatal编程技术网

通过AJAX调用后将PHP变量发送回JS

通过AJAX调用后将PHP变量发送回JS,php,json,ajax,Php,Json,Ajax,*更新了完整的代码示例,在阅读了评论之后,我想我应该发布我仍在挣扎的示例代码。基本上,我希望单击html中的链接,将表单提交到called.php,并将其值返回到html页面上的div中 <!DOCTYPE html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <

*更新了完整的代码示例,在阅读了评论之后,我想我应该发布我仍在挣扎的示例代码。基本上,我希望单击html中的链接,将表单提交到called.php,并将其值返回到html页面上的div中

    <!DOCTYPE html>
<head>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>

        function dSubmit(){

            $.ajax({
                type: "POST",
                url: "called.php",
                data: $("#projectAddition").serialize(),    

                success: function(data) {
                    //here id where you want display
                    $('#id1').val(data.var1); //here is value1
                }
            }

}

        </script>

</head>

<body>

    <form id="projectAddition" action="#" method="POST">
    <input type="text" name="projectName">
    </form>
    <a href="#" onclick="dSubmit()">Submit Form</a>
    <br>Output<br>
    <div id="id1"></div>

</body>

首先,像这样将变量放入数组中

$array = array(
    'var1'=>'value1',
    'var2'=>'value2'
);
echo json_encode($array);
然后

在ajax中,您会遇到这种情况

$.ajax({
    type: "POST",
    url: "url",
    data: $("#projectAddition").serialize(),    
    success: function(data) {
        //here id where you want display
        $('#id1').val(data.var1); //here is value1
        $('#id2').val(data.var2); //here is value2
    }
}

经过进一步修改,工作的HTML如下所示:

// url, formName passed in through function

$.ajax({
       type: "POST",
       url: url,
       data: $("#" + formName).serialize(), // serializes the form's elements.
       success: function(data)
       {
                $("#AtoZ").append('<div id="success">' + data + '</div>');}

只是想知道不使用JSON是否是个问题?我传递的唯一内容是一个字符串,例如“failed”

使用您的数据对象访问返回的数据,并在quickprojectadd.php文件中确保它返回您的数据感谢您的帮助,我知道我正在努力实现的理论。任何示例代码都会非常有用,因为我尝试过的每件事似乎都会导致被低估。类似的答案可以在这里找到。在发布之前,我已经阅读了类似的答案以及其他各种类似的问题,但我仍在努力解决。更新了上面的非工作示例。感谢您的帮助,我现在已经更新了原始问题,以包含完整的代码,因为它仍然不能正常工作。如果你能进一步帮助我,那就太好了!
// url, formName passed in through function

$.ajax({
       type: "POST",
       url: url,
       data: $("#" + formName).serialize(), // serializes the form's elements.
       success: function(data)
       {
                $("#AtoZ").append('<div id="success">' + data + '</div>');}
echo $return="Project Added, Reference: $id";