嗨,我无法让这个 ajax 代码与 JavaScript 一起工作。该函数被称为 studentReqHandler,带有一个按钮 onclick 函数,所有内容都在 echo 中。这是函数 studentReqHandler 的代码:
echo "<script type='text/javascript'>
function studentReqHandler(action,id,email,elem){
_(elem).innerHTML = 'processing ...';
var ajax = ajaxObj('POST', 'verifying.php');
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText == 'accept_ok'){
_(elem).innerHTML = '<b>User Verified!</b><br />';
} else {
_(elem).innerHTML = ajax.responseText;
}
}
}
ajax.send('action='+action+'&id='+id+'&email='+email);
}
</script>
这是 onclick 按钮:
<button onclick='studentReqHandler(\"accept\",\"".$id."\",\"".$email."\",\"user_info_".$id."\")'>accept</button> or
";
其他 ajax 相关功能:
function ajaxObj( meth, url ) {
var x = new XMLHttpRequest();
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
return true;
}
}
并为此工作的最后一个 php:
if (isset($_POST['action']) && isset($_POST['id'])&& isset($_POST['email'])){
$id = preg_replace('#[^0-9]#', '', $_POST['id']);
$email = $_POST['email'];
if($_POST['action'] == "accept"){
$sql = "UPDATE profile SET verified='1' WHERE id='$id' AND email='$email' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
mysqli_close($db_conx);
echo "accept_ok";
exit();
}
}
谁能弄清楚为什么这不起作用?