【问题标题】:Ajax code unresponsiveAjax 代码无响应
【发布时间】:2013-04-21 06:20:30
【问题描述】:

您好,我无法将此 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>

这是点击按钮:

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

    } 

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

【问题讨论】:

  • 始终清理用户输入! bobby-tables.com/php.html
  • 您使用的是萤火虫/开发者工具吗?什么不工作? php是否返回错误?你检查过你的日志吗?您是否遇到客户端错误?
  • 现在发生的只是元素更改为accept_ok。 php 不返回 accept_ok

标签: php javascript mysql sql ajax


【解决方案1】:

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

最小错误,你在

<?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>";
?>

【讨论】:

    猜你喜欢
    • 2012-03-02
    • 2013-09-08
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2020-11-12
    相关资源
    最近更新 更多