How to add ajax to a Wordpress theme

I have a problem that I’ve been stuck for several days ... I am trying to use a simple ajaxPOST function to send data to a MySQL database (not a WP database).

This code is inside the "single-post.php" topic in the thread, because it needs to be checked before every post.

$.ajax({ url: 'library/functions/admin_checkuser.php', data: {action: userID}, type: 'post', success: function(output) { alert(output); } }); 

I just send the variable to the "admin_checkuser.php" script, which in turn calls another script that takes action in the database.

This is the code for "admin_checkuser":

 $userid = $_POST['action']; echo $userid;//for testing $oMySQL = new MySQL(); $query = "Select * FROM videotable WHERE uid = '$userid'"; $oMySQL->ExecuteSQL($query); $bb = $oMySQL->iRecords; $aa = $oMySQL->aResult; echo $bb; if ($bb == 0){ $query = "INSERT INTO videotable VALUES ('','$userid','true')"; $oMySQL->ExecuteSQL($query); echo 'true'; exit(); }else{ $sharing = mysql_result($aa,0,"share"); echo $sharing; exit(); } 

But I don't think the calls go to the script. These scripts were tested outside of WordPress and worked, so there should be something in WordPress that blocks the ajax call. By the way, I tried to place "admin_checkuser.php" in many different folders, but nothing worked.

Thanks in advance.

+7
source share
2 answers

You must check your url for your ajax call.

Perhaps use the full URL instead of the relative.

Perhaps because of the location of your theme, the URL is incorrect. I assume your ajax code is in the theme folder.

There are some wordpress features that get a theme catalog.

For example, if you are on this page http://yourwebpage/test/ , then the ajax call will go here http://yourwebpage/test/library/functions/admin_checkuser.php . I guess this is the wrong location.

This is why you need to add an absolute url to your script. And if this is in your topic, you can use this get_template_directory_uri() method to get the template directory.

See here: http://codex.wordpress.org/Function_Reference/get_template_directory_uri

+2
source

You are much better off using the built-in WordPress AJAX request.

So, in your functions.php themes, add a function to be called, for example:

 function checkUser() { $userid = $_POST['user']; //validation also :) $oMySQL = new MySQL(); $query = "Select * FROM videotable WHERE uid = '$userid'"; $oMySQL->ExecuteSQL($query); $bb = $oMySQL->iRecords; $aa = $oMySQL->aResult; echo $bb; if ($bb == 0){ $query = "INSERT INTO videotable VALUES ('','$userid','true')"; $oMySQL->ExecuteSQL($query); echo 'true'; exit(); } else { $sharing = mysql_result($aa,0,"share"); echo $sharing; exit(); } } 

After that, you add your hook using connections to the embedded AJAX system

 add_action('wp_ajax_check_user', 'checkUser'); add_action('wp_ajax_nopriv_check_user', 'checkUser'); 

wp_ajax_nopriv_%s allows it to be called from the front end.

And then, in your javascript file, you just run your ajax request.

 $.post(ajaxurl, { action: 'check_user', user: userId }, function(output) { alert(output); }); 

If ajaxurl is undefined, you will need to create it in the template file, something like this should work, but there are other ways.

 add_action('wp_head','ajaxurl'); function ajaxurl() { ?> <script type="text/javascript"> var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>'; </script> <?php } 

Backend wordpress does the rest.

It takes the action passed in the AJAX request, looks for the corresponding hook `wp_ajax(_nopriv)_%s , and then calls the function assigned to the hook.

It will also be passed in either $_POST or $_GET depending on the type of AJAX request.

You can read a little more about using AJAX inside Wordpress .

+24
source

All Articles