Ajax代码没有响应

时间:2013-04-27 00:55:54

标签: php javascript mysql sql ajax

嗨,我在使用JavaScript时使用这个ajax代码时遇到了麻烦。该函数被称为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();
    }

    } 

任何人都可以弄清楚为什么这不起作用?

1 个答案:

答案 0 :(得分:0)

我粘贴了这段代码并且有效:

最小错误,你在&lt; button&gt;中有一些杂乱的引号吗? 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>";
?>