[英]Ajax can't work with wpdb
I have Simple code i had Created in Ajax,PHP and Mysql this code work fine in all Conditions Except one this one is wpdb Code to insert Data to table 我有我在Ajax,PHP和Mysql中创建的简单代码,该代码在所有条件下都能正常工作,除了一个是wpdb之外,它是将数据插入表中的代码
<script type="text/javascript">
function _(id){ return document.getElementById(id); }
function submitForm(){
_("status").innerHTML = 'please wait ...';
var formdata = new FormData();
formdata.append( "m", _("m").value );
var ajax = new XMLHttpRequest();
ajax.open( "POST", "<?php bloginfo('template_directory'); ?>/ajax-insert.php" );
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
if(ajax.responseText == "empty"){
_("status").innerHTML = '<div class="error"><p>Please Fill all of the Fields</p></div>';
}
if(ajax.responseText == "Done"){
_("status").innerHTML = '<div class="success"><p>Done</p></div>';
}
if(ajax.responseText == "error"){
_("status").innerHTML = '<div class="success"><p>Problem</p></div>';
}
}
}
ajax.send( formdata );
}
</script>
<form action="" onsubmit="submitForm(); return false;">
<div id="status"></div>
<label>Your Message</label><br>
<textarea cols="50" rows="10" type="text" name="m" id="m"></textarea>
<br>
<input name="submit" type="submit" value="Send">
</form>
ajax-insert.php i had make code to insert data with some Conditions this is the code ajax-insert.php我有一些条件插入代码以插入数据,这是代码
<?php
if(isset($_POST['m'])){
$message = $_POST['m'];
date_default_timezone_set("Africa/Cairo");
$date = date("Y/m/d h:i:sa");
}if(empty($message))
echo "empty";
elseif(!preg_match("~^[0-9a-z\-'\s\p{Arabic}]{1,60}$~iu", $message)){
echo "error";
}else{
echo "Done";
$insert = $wpdb->insert( 'chat_support', array( 'id' => '1', 'message' => $message , 'date' => $date, 'user' => $user_ID));
}
?>
if i try submit the form while textarea is Empty it give me Response that it is Empty but the Problem When i Write any thing to submit in database the code after Else dosen't work The response be Please wait..... and This Response Still more thime without Doing and Thing 如果我尝试在textarea为空时提交表单,则会给我答复为空,但问题是当我在Else无法工作后在数据库中编写任何要提交的代码时,答复为Please wait ..... and this响应无需做任何事情,还有更多的丁香
It is Highly advisable to use WordPress Ajax actions, you need to hookup your function to be called to wp_ajax and it would be available throughout the wp environment 强烈建议使用WordPress Ajax操作,您需要将要调用的函数连接到wp_ajax,并且它将在整个wp环境中可用
Details about how to create and call ajax functions can be viewed from here https://codex.wordpress.org/AJAX_in_Plugins 有关如何创建和调用ajax函数的详细信息,可以从这里https://codex.wordpress.org/AJAX_in_Plugins查看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.