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

标签 php javascript xml ajax

我的第一篇文章在这里。我是 Java/AJAX 的新手,但我在 PHP 方面有一些经验。我正在尝试将 html 表单数据传递给 php 文件进行处理。目标是让 php 脚本处理表单数据并返回 true/false 标志。我正在尝试 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 脚本似乎正在调用/执行。这是 test1.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,因此您不必担心所有这些 header ...只需 check $.ajax .

在某些时候,您可能想要放弃 jQ,因为您必须支持较旧的浏览器(IE<9,甚至 IE<8)。在那种情况下 this might prove helpful

澄清:
Vanilla 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 脚本的服务器在另一个域中,或者您必须发布后续问题。
不过希望这会有所帮助

关于php - 使用 xml 将 javascript 变量发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16980197/

相关文章:

javascript - 如何删除javascript数组元素?

php - 从 PHP 手动复制和编程生成的源代码中读取 XML 的区别

c# - 如何检查匿名对象属性是否不为空 C#

php - 在 CSS 中设置 RSS 提要样式

PHP: Empty/Null 和 if 逻辑查询

php - MySQL如何从一个月开始逐日存储数据?

java - Jackson XML 列表元素匹配它们的根名称

php - 重音符号 (`)(不是单引号)在 PHP 中代表什么?

javascript - facebook 喜欢和分享按钮 javascript 更新我的网页内容,例如重新加载页面

javascript - 无法在 angular2 中循环 json 对象