简体   繁体   English

在PHP的输入标记中编写onClick函数时不起作用

[英]onClick function not working when written inside input tag in php

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

when I click the csubmit button ajax_post method is not called, no alert is given and data is not getting inserted inside database which is done inside comments.php 当我单击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>

用。。。来代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM