ajax到php,javascript – 如何ajax POST到PHP

我似乎无法弄清楚如何使用ajax发布.我做了一个愚蠢的形式来尝试它,甚至在将它一直削减到只有两个值之后,仍然无法获得任何工作.我的

HTML是这样的:

Test this</title>

First Name:

Last Name:

然后,到目前为止,我的外部JavaScript只是一个函数:

function postStuff(){

// Create our XMLHttpRequest object

var hr = new XMLHttpRequest();

// Create some variables we need to send to our PHP file

var url = "processForm.php";

var fn = document.getElementById("fname").value;

var ln = document.getElementById("lname").value;

var vars = "firstname="+fn+"&lastname="+ln;

hr.open("POST", url, true);

hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

// Access the onreadystatechange event for the XMLHttpRequest object

hr.onreadystatechange = function() {

if(hr.readyState == 4 && hr.status == 200) {

var return_data = hr.responseText;

document.getElementById("status").innerHTML = return_data;

}

}

// Send the data to PHP now... and wait for response to update the status div

hr.send(vars); // Actually execute the request

document.getElementById("status").innerHTML = "processing...";

}

虽然我的PHP只是回复了这些东西:

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

echo $firstname ." - ". $lastname ."
";

?>

我在firebug或者chrome的工具中找不到任何错误.

任何人,我,我做错了什么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值