php - Ajax fetches data from database only once -
i working in wordpress , have drop down box , search button. , have used ajax fetch data after selecting choice drop down box. problem search button fetches data once , second time if select drop down other value , press search refreshed data not pasted.
html
[showmyimage] <form id="mydispimage" action="" method="post"> <select name="category" id="category" style="width:250px; background-color:lightgrey;">'; <option value="" disabled="disabled" selected="selected" ">select category</option>'; <option value="cutie pie">cutie pie</option>'; <option value="chubby">chubby</option>'; <option value="dimples">dimples</option>'; </select>; <input type="submit" id="displayimage" name="displayimage" value="search" style="margin-left:15px; margin-bottom:15px;"> </form> <div id="myresult"></div>
ajax code
jquery(function ($) { $("#mydispimage").submit(function (e) { //form intercepted e.preventdefault(); //$("#displayimage").change(function (){ alert("hello"); //serialize form contains secretcode var sentdata = $(this).serializearray(); //add additional param data sentdata.push({ name: 'action', value: 'display' }) //set sentdata data sent $.post(yes.ajaxurl, sentdata, function (res) { //start of funciton //$("#myresult").append(res.l); $("#myresult").append(res.form); //$.parsejson(data); return false; } //end of function , 'json'); //set datatype json, parsed data in callback }); // submit end here }); // change function });
php code
add_action( 'wp_ajax_display', 'display' ); add_action( 'wp_ajax_nopriv_display', 'display'); add_shortcode('showmyimage','showimage'); function showimage(){ // register & enqueue javascript file called globals.js wp_register_script( 'displayimg', get_stylesheet_directory_uri() . "/js/ajaxinsert.js", array( 'jquery' ) ); wp_enqueue_script( 'displayimg' ); // use wp_localize_script pass php variables javascript wp_localize_script( 'displayimg', 'yes', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } function display() { $response = array(); if(isset($_post['category'])) { $category = $_post['category']; $response['l'] = '<a href="" style="margin-top:30px !important; position:absolute; z-index:50; font-size:20px;"> participate </a>'; $response['ujicountdown'] = do_shortcode('[ujicountdown id="photos contest" expire="2015/04/30 00:00" hide="true" url="" subscr="sdf" recurring="" rectype="second" repeats=""]'); global $wpdb; $votes = 1; $response["category"] = $category; //get current competition value $comp = $wpdb->get_var("select competition competition order cid desc limit 1"); $sql = "select 1user.uid, 1user.username, 1user.competition, 1user.path, 1user.category, sum(votes.votes) votessum 1user left join votes on 1user.uid=votes.uid group 1user.uid, 1user.username, 1user.competition, 1user.path, 1user.category having 1user.category = '$category' && 1user.competition = '$comp' order votessum"; $results = $wpdb->get_results($wpdb->prepare($sql)) or die(mysql_error()); if( is_array($results) && count($results) > 0 ) { $form = ""; foreach( $results $result ) { $form .= '<form action="" method="post">'; $form .= "<input name='category' type='hidden' value='$result->category'>"; $form .= "<img src='$result->path' width='150' height='150' >" . '<br><br>'; $form .= "<input name='id' type='hidden' value='$result->uid'>"; $form .= "<input name='comp' type='hidden' value='$result->competition'>"; $form .= $result->username.'<br>'; $form .= $result->votessum.'<br>'; $form .= "<input style='margin-bottom:30px;' value='vote' name='submit' type='submit'/></form>"; }//end of foreach $response['form'] = $form; } echo json_encode($response); die(); }//end of isset else { $respons["status"] = false; $response["message"] = "<h1 style='font-family:satisfy,cursive; font-size:normal;background-color:pink;'>"."please select category"." </h1>"; echo json_encode($response); die(); } }
Comments
Post a Comment