$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>jquery - Search query with Ajax|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Search query with Ajax

matteradmin9PV0评论

I'm running a $wpdb select query and getting it’s parameter from a textbox. Result shows as soon as I press the search button, but I want to do the same with Ajax without refreshing the page. I have tried everything but the page is still refreshed.

jQuery code:

jQuery(document).ready(function() {      
    jQuery("#Submityourskill").click(function(){       
        jQuery.post(yes.ajaxurl,{action : 'doit'},
            function( response) {//start of funciton
                // alert(response);
                //jQuery("#searchtextbox").val(response);
                jQuery("#result").append(response);
                jQuery("#textarea").html(response);
                return false;
            } //end of function
        );
    }); // click button function finishing here
}); //end of main loop 

HTML:

<form action="" method="post">
    <div><input maxlength="200" name="secretcode" size="200" type="text" value="" placeholder="Type Here !" />
    <input id="Submityourskill" name="Submit" type="submit" value="Search Record" /></div>
</form>
<div id="result"></div>

PHP:

function doit() {
    if(isset($_POST['secretcode'])!= ''){
        if($_POST['Submit']) {
            $secretcode=$_POST['secretcode'];
            global $wpdb;
            $sql = "SELECT * FROM wp_store_locator WHERE sl_description='$secretcode'";
            $results = $wpdb->get_results($sql) or die(mysql_error());
            foreach( $results as $result ) {
                echo $result->sl_description;
            }
            exit();
        }
    }
}
add_action( 'wp_ajax_nopriv_doit', 'doit');
add_action( 'wp_ajax_doit', 'doit' );

function add_myjavascript(){  
    wp_register_script( 'globals', get_stylesheet_directory_uri() . "/js/ajax-implementationn.js", array( 'jquery' ) ); 
    wp_enqueue_script( 'globals' );
    // use wp_localize_script to pass PHP variables into javascript
    wp_localize_script( 'globals', 'yes', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}  
add_action( 'init', 'add_myjavascript' );

I'm running a $wpdb select query and getting it’s parameter from a textbox. Result shows as soon as I press the search button, but I want to do the same with Ajax without refreshing the page. I have tried everything but the page is still refreshed.

jQuery code:

jQuery(document).ready(function() {      
    jQuery("#Submityourskill").click(function(){       
        jQuery.post(yes.ajaxurl,{action : 'doit'},
            function( response) {//start of funciton
                // alert(response);
                //jQuery("#searchtextbox").val(response);
                jQuery("#result").append(response);
                jQuery("#textarea").html(response);
                return false;
            } //end of function
        );
    }); // click button function finishing here
}); //end of main loop 

HTML:

<form action="" method="post">
    <div><input maxlength="200" name="secretcode" size="200" type="text" value="" placeholder="Type Here !" />
    <input id="Submityourskill" name="Submit" type="submit" value="Search Record" /></div>
</form>
<div id="result"></div>

PHP:

function doit() {
    if(isset($_POST['secretcode'])!= ''){
        if($_POST['Submit']) {
            $secretcode=$_POST['secretcode'];
            global $wpdb;
            $sql = "SELECT * FROM wp_store_locator WHERE sl_description='$secretcode'";
            $results = $wpdb->get_results($sql) or die(mysql_error());
            foreach( $results as $result ) {
                echo $result->sl_description;
            }
            exit();
        }
    }
}
add_action( 'wp_ajax_nopriv_doit', 'doit');
add_action( 'wp_ajax_doit', 'doit' );

function add_myjavascript(){  
    wp_register_script( 'globals', get_stylesheet_directory_uri() . "/js/ajax-implementationn.js", array( 'jquery' ) ); 
    wp_enqueue_script( 'globals' );
    // use wp_localize_script to pass PHP variables into javascript
    wp_localize_script( 'globals', 'yes', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}  
add_action( 'init', 'add_myjavascript' );
Share Improve this question edited Feb 21, 2015 at 18:21 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Feb 21, 2015 at 17:15 ZeeshanZeeshan 1712 gold badges5 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This is a javascript issue. You have to prevent form submission to stop the page from reloading. See event.preventDefault() in jQuery docs.

jQuery("#Submityourskill").click(function(event){
    event.preventDefault();
    // the rest of your code
});

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far