Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax运行我的插入查询两次_Javascript_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

Javascript Ajax运行我的插入查询两次

Javascript Ajax运行我的插入查询两次,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我在WordPress中工作,我使用ajax运行一个函数在数据库中插入一行。Ajax会运行,但由于某些原因,插入操作会执行两次 下面是我的代码 阿贾克斯 PHP代码 插入函数 add_action( 'wp_ajax_votes', 'votes' ); add_action( 'wp_ajax_nopriv_votes', 'votes'); add_shortcode('sketchfamvotes','voteus'); function voteus(){ // regi

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

下面是我的代码 阿贾克斯

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>";    
if(is_数组($results)&&count($results)>0){
$form=“”;
foreach($results作为$result){
$form.='';
$form.=”;
$form.=“路径'宽度='150'高度='150'>””

; $form.=”; $form.=”; $form.=''。
'; $form.=$result->category.“
”; $form.=$result->uid.
; $form.=$result->votessum.
; $form.=”;
尝试使用修改后的代码-jquery.off()


或者你可以用布尔值true false来维护一个标志。Shelim…我添加了这段代码,但数据仍然在mysql数据库中插入了两次。请建议一个解决方案,并告诉我为什么在我编写了1个insert query Only时会这样做。Shalim你是谁…它起作用了…你是大师…也告诉我为什么这样做就我所知,我们可以通过inspect或firebug检查代码执行了多少次吗?您在控制台中检查过了吗?您的ajax请求执行了两次还是一次?请检查一下?这是我的荣幸。这是由于事件处理。Jquery off()销毁您案例中所有以前触发的提交事件,并发布一个新事件。如果答案确实有用,请向上投票。如果您查看(例如)Google Chrome Developer工具,是否会快速连续执行两次ajax调用?
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>";    
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