我正在開發一個Ajax的PHP的事件日曆和形式PHP echo語句添加事件我有AJAX:阿賈克斯 - 不工作
function PostData() {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
throw new Error("Ajax is not supported by this browser");
}
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200){//&& xhr.status < 300) {
if(xhr.responseText == 1){
document.getElementById('div1').innerHTML = "SUCCESS";
//window.location = "form2.php";
}
else{
document.getElementById('div1').innerHTML = xhr.responseText;
}
}
}
}
var a = document.getElementById("aaa").value;
var b = document.getElementById("bbb").value;
var c = document.getElementById("ccc").value;
xhr.open('POST', 'addEvent.php');
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("xxx=" + a+"&yyy="+b+"&zzz="+c);
}
</script>`:
和我的服務器端的PHP代碼應該呼應的結果在加入活動,如成功或者數據庫錯誤或者這樣:
<?php
include ("connect.php");
$eventDate = $_POST["xxx"];
$eventTime = $_POST["yyy"];
$eventDesc = $_POST["zzz"];
if($eventDate != '' && $eventDesc != '' && $eventTime != ''){
// $timestamp = strtotime($eventTime);
//$eventTime = date("H:i", $timestamp);
$sql = "INSERT INTO events(evdate,evtime,evdesc,username) VALUES ('$eventDate','$eventTime','$eventDesc','usr')";
if(mysqli_query($conn, $sql))
echo 1;
else
echo mysqli_error();
}
else
echo 'you must not leave it blank';
mysqli_close($conn);
?>
隨着輸出,如果有跡象表明是在事件的PHP文件作品的底部添加形式echo語句填充字段。但是如果插入一行,echo語句不起作用。另外,如果在插入記錄時出現錯誤,仍然不起作用。
'如果(條件){//語句}其他{//回聲}' – aldrin27
看看在開發工具的網絡選項卡中的實際響應。 – Barmar