admin管理员组文章数量:1277311
I am having a bit of a weird glitch happening to me and I am not sure how I have produced it or whether it is the norm.
I am developing my own plugin and when a football/soccer team is typed into a box it will check if its in the database already or not.
Here are my lines of code
add_action( 'admin_footer', 'fws_teamcheck_javascript' );
function fws_teamcheck_javascript() { ?>
<script type="text/javascript">
jQuery(document).on('focusout', '.name', function () {
var name = this.value;
//alert(name);
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {"action": "fws_check_name", "name": name},
success: function (data) {
alert(data);
if(data=='true') {
$(".name").css("border", "solid 1px red");
}
if(data=='false') {
$(".name").css("border", "solid 1px green");
}
}
});
});
</script>
<?
}
function fws_check_name(){
global $wpdb;
$dbtable = $wpdb->prefix . 'fws_team_data';
$name = $_POST['name'];
$data = $wpdb->get_results($wpdb->prepare("SELECT * FROM $dbtable WHERE name = $name"));
if($data) { echo 'true'; }else{ echo 'false'; }
}
add_action( 'wp_ajax_fws_check_name', 'fws_check_name' );
add_action( 'wp_ajax_nopriv_fws_check_name', 'fws_check_name' );
When i move away from the input it will bring up the alert as either true0
or false0
is there a reason why it is adding the 0 at the end?
Thanks for your input.
I am having a bit of a weird glitch happening to me and I am not sure how I have produced it or whether it is the norm.
I am developing my own plugin and when a football/soccer team is typed into a box it will check if its in the database already or not.
Here are my lines of code
add_action( 'admin_footer', 'fws_teamcheck_javascript' );
function fws_teamcheck_javascript() { ?>
<script type="text/javascript">
jQuery(document).on('focusout', '.name', function () {
var name = this.value;
//alert(name);
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {"action": "fws_check_name", "name": name},
success: function (data) {
alert(data);
if(data=='true') {
$(".name").css("border", "solid 1px red");
}
if(data=='false') {
$(".name").css("border", "solid 1px green");
}
}
});
});
</script>
<?
}
function fws_check_name(){
global $wpdb;
$dbtable = $wpdb->prefix . 'fws_team_data';
$name = $_POST['name'];
$data = $wpdb->get_results($wpdb->prepare("SELECT * FROM $dbtable WHERE name = $name"));
if($data) { echo 'true'; }else{ echo 'false'; }
}
add_action( 'wp_ajax_fws_check_name', 'fws_check_name' );
add_action( 'wp_ajax_nopriv_fws_check_name', 'fws_check_name' );
When i move away from the input it will bring up the alert as either true0
or false0
is there a reason why it is adding the 0 at the end?
Thanks for your input.
Share Improve this question edited Nov 3, 2021 at 16:54 AdamCarder asked Nov 3, 2021 at 16:49 AdamCarderAdamCarder 11 bronze badge 2- have you considered using the modern REST API for AJAX instead of the old admin-ajax.php API? It's much easier to debug, gives human readable error messages and does a lot of the heavy lifting for you, as well as giving you a pretty URL. – Tom J Nowell ♦ Commented Nov 3, 2021 at 17:11
- @TomJNowell to be honest I haven't even looked at REST API, I am still trying to learn the basics from online tutorials. Do you have any recommendations on where I can learn REST API for wordpress? – AdamCarder Commented Nov 3, 2021 at 17:19
2 Answers
Reset to default 0The following wasn't wrong for me:
$data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $dbtable WHERE name = $name" ) );
It should be:
$data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$dbtable} WHERE name = %s", $name ) );
Then you can just die();
at the end of the function like:
if( count( $data ) >= 1 ){
die( 'true' );
} else {
die( 'false' );
}
I found out when re-reading through the AJAX page found at https://codex.wordpress/AJAX_in_Plugins that you have to put die();
at the end of the function.
My code now looks like this and works as intended.
add_action( 'admin_footer', 'fws_teamcheck_javascript' );
function fws_teamcheck_javascript() { ?>
<script type="text/javascript">
jQuery(document).on('focusout', '.name', function () {
var name = this.value;
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {"action": "fws_check_name", "name": name},
success: function (data) {
if(data=='true') {
$(".name").css("border", "solid 1px red");
}
if(data=='false') {
$(".name").css("border", "solid 1px green");
}
}
});
});
</script>
<?
}
function fws_check_name(){
global $wpdb;
$dbtable = $wpdb->prefix . 'fws_team_data';
$name = $_POST['name'];
$sql = $wpdb->prepare("SELECT * FROM $dbtable WHERE name = '$name'");
$data = $wpdb->get_results($sql);
if(count($data) >= '1'){ echo 'true'; }else{ echo 'false'; }
die();
}
add_action( 'wp_ajax_fws_check_name', 'fws_check_name' );
add_action( 'wp_ajax_nopriv_fws_check_name', 'fws_check_name' );
本文标签: plugin developmentJquery php request is returning a weird result
版权声明:本文标题:plugin development - Jquery php request is returning a weird result 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741235817a2362989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论