【问题标题】:Ajax runs my insert query twiceAjax 运行我的插入查询两次
【发布时间】:2015-04-24 10:02:46
【问题描述】:

我在 WordPress 中工作,我运行一个函数来使用 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>";    

【问题讨论】:

  • 如果您查看(例如)Google Chrome 开发者工具,ajax 调用是否会快速连续执行两次?

标签: javascript php jquery ajax wordpress


【解决方案1】:

试试这个修改后的代码 - 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

【讨论】:

  • 或者您可以使用布尔值 true false 维护一个标志。
  • Shelim .... 我添加了这段代码,但数据仍然在 mysql 数据库中插入了两次。请提出一个解决方案,并告诉我为什么我只写了 1 个插入查询时它的行为是这样的
  • 哇,Shalim,你是那个人......它奏效了......你是大师......还告诉我为什么会发生这种情况只是为了我的知识,我们可以通过检查或萤火虫检查代码执行了多少次
  • 你在控制台签到了吗?你的ajax请求是两次还是一次?请检查一下?
  • 这是我的荣幸。这是由于事件处理。Jquery off() 在您的情况下销毁所有先前触发的提交事件并释放一个新事件。如果答案真的有帮助,请投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多