0

我正在尝试在 textarea 中传递数据并使 php 回调以 retutn 相同并在简单操作后将其显示在 textarea 中以确保传输发生。但我无法让我的 ajax 函数传递数据。

<script type="text/javascript" src="ajax.js"></script>

这是我的表格

<form action="#" method="post" onsubmit="submit_notes_call();return false;">
    <textarea rows="5" cols="30" name="user_notes" id="user_notes"></textarea>
    <input type="submit" name="notes_submit" id="notes_submit" value="UPDATE NOTES"  >
</form>

这是我在 ajax.js 中的 Ajax 函数

function submit_notes_call()
{
    var ajaxVar;
    var user_notes = " Data from ajax call to php callback ";
                      /*document.getElementById("user_notes").value; */
    try
    {
        ajaxVar = new XMLHttpRequest();
    }catch (e)
    {
        try
        {
            ajaxVar = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e)
        {
            try
            {
                ajaxVar = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e)
            {
                alert("Your browser broke!");
                return false;
            }
        }
    }
    ajaxVar.onreadystatechange=function(){
                                            if(ajaxVar.readyState==4)
                                                {
                                                document.getElementById("user_notes").innerHTML = ajaxVar.responseText;
                                                }
                                            };
    ajaxVar.open('POST','notes_submit.php',true);
    ajaxVar.send(user_notes);


}

这是我在 notes_submit.php 中的 php 代码

<?php 
    $data_recieved = "Data from php call back to ajax function+".$_POST['user_notes'];
    echo "Data : " . $data_recieved;
?>

我得到了Data : Data sent from php call back to ajax function+textarea 字段内的输出,这意味着虽然正在发生通信,但 php 回调无法读取 ajax 调用者发送的数据,或者可能是 ajax 函数无法将数据发送到 php 回调。

这里有什么错误??

4

1 回答 1

1

更换连接线

ajaxVar.send(user_notes);

经过

ajaxVar.send('user_notes='+user_notes);

阅读此http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

于 2013-07-12T05:53:00.087 回答