使用 xml 将 javascript 变量发布到服务器

Posting javascript variable to server using xml

本文关键字:服务器 变量 xml javascript 使用      更新时间:2023-09-26

我的第一篇文章在这里。我是Java/AJAX的新手,但我在PHP方面有一些经验。我正在尝试将 html 表单数据传递给 php 文件进行处理。目标是让 php 脚本处理表单数据并返回真/假标志。我正在尝试 AJAX,因为我不想刷新屏幕。根据 php 脚本的响应,弹出窗口将向用户提供信息覆盖现有屏幕。

我的HTML表单代码是:-

<form name="screen3" method="post" action="" id="scr3" />
<input type="image" src="images/proceed.jpg" alt="Proceed" id="proceed1" name="submit" value="Send" />
</form>            

我已经使用 javascript 从表单重定向了提交按钮:-

<script type="text/javascript">
$(document).ready(function() {
$('#proceed1').click(function(e) {
    e.preventDefault();
    x=validateScreen3();
    if (x) {getFormData();}
        })
    });
</script>

到目前为止一切顺利,validateScreen3((被调用并验证用户条目(不会让你厌烦脚本(。 getFormData被调用,但这就是问题所在:-

function getFormData() {
var xmlhttp;
var emailAddress = document.getElementById("emailaddress").value;
var entryCode = document.getElementById("entrycode").value;
var acceptance = document.getElementById("acceptance").value;
var Sel = document.getElementById("sel").value;

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","test1.php", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("emailaddress="+emailAddress);
}

我已经确认变量数据正在传递给函数,但是上面引用的test1.php脚本似乎没有被调用/执行。这是测试1.php文件:-

<?php
$here = $_POST['emailaddress'];
echo '</div></div>';
if (!empty($here)) {
echo '<div style="position:absolute; top:100px; left:300px; width:400px; height:200px; background-color:#CCC; color:#000; z-index:50;">';
echo 'got the variable '.$here;
echo '</div>';
}
else {
echo '<div style="position:absolute; top:100px; left:300px; width:400px; height:200px; background-color:#CCC; color:#000; z-index:50;">';
echo 'DIDNT GET the variable '.$here;
echo '</div>';
}
?>

这些div 都没有出现,从我能想到的每个测试来看,该文件根本没有被调用。任何想法或建议将不胜感激。

你需要为XMLHttpRequestonreadystatechange事件添加一个事件处理程序。当 PHP 发送其响应时,将触发此事件:

xmlhttp.onreadystatechange = function(response)
{
    if (this.readyState===4 && this.status===200)
    {//readyState 4 means request is finished
        document.body.innerHTML += response;//since it's an HTML response...
    }
};

但是由于您使用的是jQ,因此不必担心所有这些标头...只需检查$.ajax.

在某些时候,你可能想放弃jQ,因为你必须支持较旧的浏览器(IE<9,甚至IE<8(。在这种情况下,这可能会有所帮助

澄清一下:
香草 JS:

var xhr =new XMLHttpRequest();
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function()
{
    if (this.readyState === 4 && this.status === 200)
    {//response is xhr property in vanilla JS
         document.body.innerHTML += this.responseText;
    }
    if (this.readyState === 4 && this.status !== 200)
    {//error in request
        console.log(this);//<-- check your console
        document.body.innerHTML += '<h1>Http error: ' + this.status;
    }
};
xhr.open("POST","test1.php", true);
xht.send("emailaddress="+emailAddress);

这应该可以正常工作。如果由于某种原因没有,请尝试使用 jQuery,如下所示:

$.ajax({ url: 'test1.php',
         type: 'POST',
         data: {emailaddress: $('#emailaddress').val()},
         success: function(response)
         {
              document.body.innerHTML += response;
         }
});

如果这对您不起作用,那么可能是您的网址错误,或者运行PHP脚本的服务器在另一个域上,或者您必须发布后续问题。
希望这有帮助,不过

你已经编写了 JavaScript 来发出 HTTP 请求,但你还没有编写任何来使用响应中的数据。

您需要等到响应返回,然后对xmlhttp.responseText执行某些操作。

请参见使用 XMLHttpRequest。