1

嗨,我无法让这个 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();
    }

    } 

谁能弄清楚为什么这不起作用?

4

1 回答 1

0

我粘贴了这段代码,它可以工作:

最小的错误,是你在 <button> javascript 字符串中有一些凌乱的引号。

<?php
/* using this at the top to check what happens when it posts. */

if (isset($_POST['action']) && isset($_POST['id'])&& isset($_POST['email'])){
    $id = preg_replace('#[^0-9]#', '', $_POST['id']);
    $email = $_POST['email'];


    if($_POST['action'] == "accept"){

    /* I commented out the query, so you must make sure that works right */    
    $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();
    }

 } 

/* some default values. You need to make sure you get these from somewhere.*/
$id=3;
$email="oo@oooo.com";

/* ELEMENT changes when I click the button */ 
echo "Div Element <div id='element'>ELEMENT</div>  area<br><br>";
?>
<script type='text/javascript'>
function studentReqHandler(action,id,email,elem) {

    /* I used the basic document element locator */
    document.getElementById('element').innerHTML = 'processing ...';

    /* I posted to itself this time. */
    var ajax = ajaxObj('POST', 'ajax.php');
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            if(ajax.responseText == 'accept_ok'){
                document.getElementById('element').innerHTML = '<b>User Verified!</b><br />';
            } else {
                document.getElementById('element').innerHTML = ajax.responseText;
            }
        }
    }
    ajax.send('action='+action+'&id='+id+'&email='+email);
}

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;    
    }
}


</script>

/* There were too many quotes and \'s in the original button. */ 
<br>The Button<br>
<?php 
    echo "<button onclick=\"studentReqHandler('accept','$id','$email','user_info_$id') \">accept</button>";
?>
于 2013-04-27T02:24:48.183 回答