Ajax运行我的插入查询两次

时间:2015-04-24 10:02:47

标签: javascript php jquery ajax wordpress

我在WordPress工作,我运行一个函数,用ajax在数据库中插入一行。 Ajax运行但由于某种原因插入操作执行了两次。

以下是我的代码 Ajax的

jQuery(function ($) {
    $(document).on("submit","#myvoteform", function(e) {
    //form is intercepted
    e.preventDefault();
        //serialize the form which contains secretcode
        var sentdata = $(this).serializeArray();

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

        //set sentdata as the data to be sent
        $.post(yes.ajaxurl, sentdata, function (res) { //start of funciton
            //$("#myresult").append(res.l);
        //  $("#myresult").html("");
            $("#myresult").html(res);


 //$.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

    }); //vote function ends here

PHP代码 插入功能

    add_action( 'wp_ajax_votes', 'votes' );
add_action( 'wp_ajax_nopriv_votes', 'votes');

add_shortcode('sketchfamvotes','voteus');

function voteus(){  
// register & enqueue a javascript file called globals.js
wp_register_script( 'votess', get_stylesheet_directory_uri() . "/js/ajaxinsert.js", array( 'jquery' ) ); 
wp_enqueue_script( 'votess' );

// use wp_localize_script to pass PHP variables into javascript
wp_localize_script( 'votess', 'yes', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}

function votes ()

{//echo json_encode("pakistan zindabad"); die();

       $cat =$_POST['category'];
       $comp = $_POST['competition'];   
       $uid= $_POST['uid'];


       global $wpdb;

        $userid = $_POST['userid'];
        $myvote = 1;

    if($wpdb->insert(
        'votes',
        array(
                'votes' => 1,
                'competition' => $comp,
                'uid' => $uid
            )
) == false) {wp_die(json_encode('Database Insertion failed')); die();} else {echo json_encode('your vote was successfully recorded');die();}

}

下面是我的表格,这是另一个ajax调用的结果。我没有粘贴完整的ajax php函数,只是显示我的表单是什么样的。

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

            $form = "";
            foreach( $results as $result ) {

            $form .= '<form id="myvoteform" 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='uid' type='text' value='$result->uid'>";
            $form .= "<input name='competition' type='hidden' value='$result->competition'>";
            $form .= "<input name='userid' value'$result->uid' type='text'>".'<br>';
            $form .= $result->category.'<br>';
            $form .= $result->uid.'<br>';
            $form .= $result->votessum.'<br>';
            $form .= "<input style='margin-bottom:30px;' id='votebutton' value='vote' name='submit' type='submit'/></form>";    

1 个答案:

答案 0 :(得分:0)

尝试使用此修改后的代码 - jquery .off()

jQuery(function ($) {
    $(document).off().on("submit","#myvoteform", function(e) {
    //form is intercepted
    e.preventDefault();
        //serialize the form which contains secretcode
        var sentdata = $(this).serializeArray();

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

        //set sentdata as the data to be sent
        $.post(yes.ajaxurl, sentdata, function (res) { //start of funciton
            //$("#myresult").append(res.l);
        //  $("#myresult").html("");
            $("#myresult").html(res);


 //$.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

    }); //vote function ends here