在php中输入标签内部写入onClick函数不起作用

时间:2018-03-27 14:36:34

标签: javascript onclick

echo "<input type=\"button\" id=\"csubmit\" value=\"SUBMIT\" onClick=\"<script type='text/javascript'>ajax_post();</script>\">";
  

当我点击csubmit按钮时,没有调用ajax_post方法,没有给出警报,数据也没有插入到在comments.php里面完成的数据库中。

<script>
function ajax_post(){
    // Create our XMLHttpRequest object
    alert("button clicked");
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "comments.php";
    var bid='<?php echo $b; ?>';
    var userid='<?php echo $userid; ?>';
    var cm = document.getElementById("latest_comment").value;
    var vars = "your_comment="+cm+"&bookid="+bid+"&uid="+userid;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            document.getElementById("comment").innerHTML = return_data;
        }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("comment").innerHTML = "processing...";
}
 </script>

1 个答案:

答案 0 :(得分:0)

替换为

echo "<input type=\"button\" id=\"csubmit\" value=\"SUBMIT\" onClick=\"ajax_post()\">";