Using AJAX and WordPress
-
All,
Currently I have some JS in an external JS file called external_js.js.In here I have the following code:
$("#submit_name").click(function() { category = $("#category").val(); search_term = $("#search_term").val(); $.post("http://localhost/wordpress/wp-content/search.php", { category: category, search_term: search_term }, function(results){ $("#search_results").html(results); }); });This is basically an AJAX request to this file:
$themePath_config = ABSPATH; set_include_path($themePath_config); include "wp-config.php"; $category = mysql_real_escape_string($_POST['category']); $search_term = mysql_real_escape_string($_POST['search_term']); $qry = "Select wp_posts.ID, wp_posts.post_name, wp_posts.post_title, from wp_posts join wp_term_relationships on wp_posts.ID=wp_term_relationships.object_id where wp_posts.post_title like '%$search_term%' and wp_term_relationships.term_taxonomy_id='$category'"; $result = mysql_query($qry); $resultrows = mysql_num_rows($result); echo $resultrows;Here is my page inside of WordPress that I’m trying to call my jQuery/AJAX with:
[tabs] [tab title="I choose as a vendor..."] <div id="choose_past_vendor"> <form id="past_vendor_form" name="past_vendor_form" method="post"> As my vendor for my <?php $qry = "Select wp_terms.name, wp_terms.term_id from wp_terms Join wp_term_taxonomy on wp_term_taxonomy.term_id=wp_terms.term_id where wp_term_taxonomy.taxonomy='category' order by wp_terms.name ASC"; $result = mysql_query($qry); ?> <select name="past_vendor_category" id="past_vendor_category" data-placeholder="Choose Vendor Type" class="chzn-select"> <option value=""></option> <?php while($resultset = mysql_fetch_array($result)){ echo '<option value="'.$resultset['term_id'].'">'.$resultset['name'].'</option>'; } ?> </select> I choose: <span id="past_vendor_selected_name"></span><br><br> Enter in the vendor's name below: <input type="text" name="past_vendor_name" id="past_vendor_name"> [button id="submit_past_vendor_name" style="gray"]Find Vendor[/button] </form> <div id="past_vendor_search_results"></div> </div> [/tab] [tab title="I recommend as additional vendors..."] Tab Content [/tab] [/tabs] <script type="text/javascript"> $(".chzn-select").chosen(); $(".chzn-select-deselect").chosen({allow_single_deselect:true}); </script>I’ve read some articles that say this should be moved to admin-ajax.php but I’m not sure how to go about making these changes.
I was trying to use this article but I’m just not sure how to implement this. My button for the submit_name button is on a page that I created from within WordPress. Here is the article:
http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/
Can anyone point me in the right direction?
Thanks in advance!
The topic ‘Using AJAX and WordPress’ is closed to new replies.