所以我在純PHP中工作得很好。當你點擊提交時,表單被提交併且PHP頁面被加載。所以我試圖實現AJAX,以便在提交表單時不必重新加載頁面,但現在變量永遠不會到達PHP腳本。AJAX請求不起作用
下面是我得到了什麼:
<form method="get" action="sms.php">
<input type="text" name="number" value="Phone number" /><br />
<textarea type="text" name="message" rows="5" cols="20"></textarea><br />
<input type="button" onclick="ajaxFunction()" value="Send" />
</form>
的AJAX JS文件:
function ajaxFunction(){
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('confirm');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var number = document.getElementById('number').value;
var message = document.getElementById('message').value;
var queryString = "?number=" + number + "&message=" + message;
ajaxRequest.open("GET", "sms.php" + queryString, true);
ajaxRequest.send(null);
}
最後的PHP腳本:
<?php
$number = $_GET["number"];
$message = $_GET["message"];
$client = new SoapClient('http://sms2.cdyne.com/sms.svc?wsdl');
$param = array(
'PhoneNumber' => $number,
'LicenseKey' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
'Message' => $message,
);
echo "SMS sent.";
?>
你應該完全放棄並使用jQuery。 – Fosco 2011-06-04 22:58:35
如果您更願意使用[jQuery的AJAX](http://api.jquery.com/jQuery.ajax/)方法,它會讓您的生活更輕鬆 – 8bitme 2011-06-04 23:00:04
@Fosco和@ 8bitme告訴別人使用jQuery不是答案。 – james 2011-06-04 23:01:35