dthok9648 2015-04-22 16:49
浏览 60

Ajax只从数据库中提取一次数据

I am working in wordpress and I have a drop down box and a search button. And I have used ajax to fetch data after selecting my choice from drop down box. Now my problem is that search button fetches data only once and second time if I select drop down other value and press search the refreshed data is 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 is intercepted
        e.preventDefault();
//$("#displayimage").change(function (){

alert("hello");
        //serialize the form which contains secretcode
        var sentdata = $(this).serializeArray();

        //Add the additional param to the data        
        sentdata.push({
            name: 'action',
            value: 'display'
        })

        //set sentdata as the data to be 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 the dataType as json, so you will get the parsed data in the 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 a 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 to pass PHP variables into 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 FROM competition ORDER BY cid DESC LIMIT 1");

        $sql = "SELECT 1user.uid, 1user.username, 1user.competition, 1user.path, 1user.category, Sum(votes.votes) AS votessum FROM 1user LEFT JOIN votes on 1user.uid=votes.uid GROUP BY 1user.uid, 1user.username, 1user.competition, 1user.path, 1user.category HAVING 1user.category = '$category' && 1user.competition = '$comp' ORDER BY votessum";

        $results = $wpdb->get_results($wpdb->prepare($sql)) or die(mysql_error());

        if( is_array($results) && count($results) > 0  ) {

            $form = "";
            foreach( $results as $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 a category"." </h1>";

        echo json_encode($response);
        die();
    }  
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作