Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var j$ = jQuery;
- var search_text = j$(".cv__text").val();
- j$.post({
- url: "<?php echo admin_url('admin-ajax.php'); ?>",
- data: ({
- action: "search_cv", search_text: search_text
- }),
- success: function (response){
- console.log(response);
- j$("#search_results").html(response);
- }
- });
- function search_cv(){
- $search_text = ucfirst($_POST["search_text"]);
- //Creating New DB Connection
- $database_name = "employee_cv";
- $mydb = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST);
- $mydb -> show_errors();
- if ($_POST["search_text"] != null){
- $result = $mydb -> get_results(
- $mydb -> prepare(
- 'SELECT * FROM employee
- WHERE Experience = %s',
- $search_text
- ) );
- }
- foreach ($result as $employee){
- //Here I just echo html blocks (divs, paragraphs etc.)
- }
- die();
- }
- add_action( 'wp_ajax_search_cv', 'search_cv' );
- add_action( 'wp_ajax_nopriv_search_cv', 'search_cv' );
- <form class="searchCV_form" role="form" action="">
- <div>
- <input type="text" id="search_text" name="search_text" class="cv__text">
- <span class="input-group-btn">
- <button type="submit" class="btn btn-default btn-primary cv__button search--form-btn">SUBMIT</button>
- </span>
- </div>
- </form>
- <div id="search_results"></div>
- $("form.searchCV_form").submit(function(event){
- event.preventDefault();
- alert( "We're not going anywhere now..." );
- // j$.post...
- });
- $("form.searchCV_form").submit(function(e){
- alert( "We're not going anywhere now..." );
- // j$.post...
- return false;
- });
- <form class="searchCV_form" role="form" action="">
- <div>
- <input type="text" id="search_text" name="search_text" class="cv__text">
- <span class="input-group-btn">
- <button type="submit" class="btn btn-default btn-primary cv__button search--form-btn">SUBMIT</button>
- </span>
- </div>
- </form>
- <div id="search_results"></div>
- <script>
- // wrap everything in a closure
- (function($){
- // get our references
- var $form = $('form.searchCV_form'),
- $search_field = $('#search_text'),
- $results = $('#search_results');
- // AJAX search call
- function do_search() {
- // grab the query value from the search field
- var search_text = $search_field.val();
- // do a POST ajax call
- $.ajax({
- type: "POST",
- url: '<?php echo admin_url('admin-ajax.php'); ?>',
- data: ({
- action: "search_cv",
- search_text: search_text
- }),
- success: function (response){
- console.log(response);
- $results.html(response);
- }
- });
- }
- // on submit, do the search but return false to stop page refresh
- $form.submit(function(e) {
- do_search();
- return false;
- });
- })(jQuery);
- </script>
- function search_cv()
- {
- // get the search query
- $search_text = ucfirst($_POST["search_text"]);
- // clean it up
- $search_text = sanitize_text_field( $search_text);
- // ... do stuff with it
- // output the HTML which will be consumed by $.html()
- ?><div>You searched for <?php echo $search_text; ?> and we found... </div><?php
- // stop doing stuff
- die();
- }
- add_action( 'wp_ajax_search_cv', 'search_cv' );
- add_action( 'wp_ajax_nopriv_search_cv', 'search_cv' );
- url: "<?php echo admin_url('admin-ajax.php'); ?>",
- // Register the script
- wp_register_script( 'some_handle', 'path/to/myscript.js' );
- // Localize the script with new data
- $translation_array = array(
- 'myajax_url' => admin_url('admin-ajax.php'),
- );
- wp_localize_script( 'some_handle', 'object_name', $translation_array );
- // Enqueued script with localized data.
- wp_enqueue_script( 'some_handle' );
- url: some_handle.myajax_url,
- // Register the script - located in functions.php (usually)
- wp_register_script( 'some_handle', 'path/to/myscript.js' );
- //In the file you need the ajax results
- // Enqueued script
- wp_enqueue_script( 'some_handle' );
- // Localize the script with new data
- wp_localize_script( 'some_handle', 'object_name', array('ajax_url' => admin_url( 'admin-ajax.php' ) );
- // the ajax call in the js file
- jQuery.ajax({
- url : object_name.ajax_url,
- type : 'post',
- data : {
- action:'search_cv',
- search_text: search_text
- }
- success : function( response ) {
- response = jQuery.parseJSON(response);
- //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
- }
- });
- echo json_encode($results);
Add Comment
Please, Sign In to add comment