Guest User

Untitled

a guest
Mar 17th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. var j$ = jQuery;
  2. var search_text = j$(".cv__text").val();
  3. j$.post({
  4. url: "<?php echo admin_url('admin-ajax.php'); ?>",
  5. data: ({
  6. action: "search_cv", search_text: search_text
  7. }),
  8. success: function (response){
  9. console.log(response);
  10. j$("#search_results").html(response);
  11. }
  12. });
  13.  
  14. function search_cv(){
  15. $search_text = ucfirst($_POST["search_text"]);
  16.  
  17. //Creating New DB Connection
  18.  
  19. $database_name = "employee_cv";
  20.  
  21. $mydb = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST);
  22. $mydb -> show_errors();
  23.  
  24. if ($_POST["search_text"] != null){
  25.  
  26. $result = $mydb -> get_results(
  27. $mydb -> prepare(
  28. 'SELECT * FROM employee
  29. WHERE Experience = %s',
  30. $search_text
  31. ) );
  32.  
  33. }
  34.  
  35. foreach ($result as $employee){
  36. //Here I just echo html blocks (divs, paragraphs etc.)
  37. }
  38.  
  39. die();
  40. }
  41.  
  42. add_action( 'wp_ajax_search_cv', 'search_cv' );
  43. add_action( 'wp_ajax_nopriv_search_cv', 'search_cv' );
  44.  
  45. <form class="searchCV_form" role="form" action="">
  46. <div>
  47. <input type="text" id="search_text" name="search_text" class="cv__text">
  48. <span class="input-group-btn">
  49. <button type="submit" class="btn btn-default btn-primary cv__button search--form-btn">SUBMIT</button>
  50. </span>
  51. </div>
  52. </form>
  53. <div id="search_results"></div>
  54.  
  55. $("form.searchCV_form").submit(function(event){
  56. event.preventDefault();
  57.  
  58. alert( "We're not going anywhere now..." );
  59. // j$.post...
  60. });
  61.  
  62. $("form.searchCV_form").submit(function(e){
  63. alert( "We're not going anywhere now..." );
  64. // j$.post...
  65.  
  66. return false;
  67. });
  68.  
  69. <form class="searchCV_form" role="form" action="">
  70. <div>
  71. <input type="text" id="search_text" name="search_text" class="cv__text">
  72. <span class="input-group-btn">
  73. <button type="submit" class="btn btn-default btn-primary cv__button search--form-btn">SUBMIT</button>
  74. </span>
  75. </div>
  76. </form>
  77. <div id="search_results"></div>
  78.  
  79. <script>
  80. // wrap everything in a closure
  81. (function($){
  82.  
  83. // get our references
  84. var $form = $('form.searchCV_form'),
  85. $search_field = $('#search_text'),
  86. $results = $('#search_results');
  87.  
  88. // AJAX search call
  89. function do_search() {
  90.  
  91. // grab the query value from the search field
  92. var search_text = $search_field.val();
  93.  
  94. // do a POST ajax call
  95. $.ajax({
  96. type: "POST",
  97. url: '<?php echo admin_url('admin-ajax.php'); ?>',
  98. data: ({
  99. action: "search_cv",
  100. search_text: search_text
  101. }),
  102. success: function (response){
  103. console.log(response);
  104. $results.html(response);
  105. }
  106. });
  107. }
  108.  
  109. // on submit, do the search but return false to stop page refresh
  110. $form.submit(function(e) {
  111. do_search();
  112. return false;
  113. });
  114.  
  115. })(jQuery);
  116. </script>
  117.  
  118. function search_cv()
  119. {
  120. // get the search query
  121. $search_text = ucfirst($_POST["search_text"]);
  122.  
  123. // clean it up
  124. $search_text = sanitize_text_field( $search_text);
  125.  
  126. // ... do stuff with it
  127.  
  128. // output the HTML which will be consumed by $.html()
  129. ?><div>You searched for <?php echo $search_text; ?> and we found... </div><?php
  130.  
  131. // stop doing stuff
  132. die();
  133. }
  134.  
  135. add_action( 'wp_ajax_search_cv', 'search_cv' );
  136. add_action( 'wp_ajax_nopriv_search_cv', 'search_cv' );
  137.  
  138. url: "<?php echo admin_url('admin-ajax.php'); ?>",
  139.  
  140. // Register the script
  141. wp_register_script( 'some_handle', 'path/to/myscript.js' );
  142.  
  143. // Localize the script with new data
  144. $translation_array = array(
  145. 'myajax_url' => admin_url('admin-ajax.php'),
  146. );
  147. wp_localize_script( 'some_handle', 'object_name', $translation_array );
  148.  
  149. // Enqueued script with localized data.
  150. wp_enqueue_script( 'some_handle' );
  151.  
  152. url: some_handle.myajax_url,
  153.  
  154. // Register the script - located in functions.php (usually)
  155. wp_register_script( 'some_handle', 'path/to/myscript.js' );
  156.  
  157. //In the file you need the ajax results
  158.  
  159. // Enqueued script
  160. wp_enqueue_script( 'some_handle' );
  161.  
  162. // Localize the script with new data
  163. wp_localize_script( 'some_handle', 'object_name', array('ajax_url' => admin_url( 'admin-ajax.php' ) );
  164.  
  165. // the ajax call in the js file
  166. jQuery.ajax({
  167. url : object_name.ajax_url,
  168. type : 'post',
  169. data : {
  170. action:'search_cv',
  171. search_text: search_text
  172. }
  173. success : function( response ) {
  174. response = jQuery.parseJSON(response);
  175. //use the response variable to print what you need on the webpage. Basically instead of running the foreach in the php function run it here with js
  176. }
  177. });
  178.  
  179. echo json_encode($results);
Add Comment
Please, Sign In to add comment