Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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发送数据,但php不接收数据_Javascript_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

Javascript Ajax发送数据,但php不接收数据

Javascript Ajax发送数据,但php不接收数据,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我正在使用ajax将数据发送到php函数。最终目标是基于$tweetdate显示数据行。以下是我的ajax脚本: jQuery('#bootstrapModalFullCalendar').fullCalendar({ dayClick: function(date, event, jsEvent, view) { var date = date.format(); jQuery.ajax({ type: "POST",

我正在使用ajax将数据发送到php函数。最终目标是基于$tweetdate显示数据行。以下是我的ajax脚本:

jQuery('#bootstrapModalFullCalendar').fullCalendar({
    dayClick:  function(date, event, jsEvent, view) {
            var date = date.format();
        jQuery.ajax({
            type: "POST",
            url: ajaxurl,
            data: {
                'action': 'show_scheduled_tweets',
                'tweetdate': date
            },
            beforeSend: function() {
                console.log('before')
            },
            success: function(){
                console.log('success')
            },
            error: function(){
                console.log('error')
            },
        });
    }
});
下面是我的php函数add_action用于WordPress:

<?php
    add_action('wp_ajax_show_scheduled_tweets', 'show_scheduled_tweets');
    function show_scheduled_tweets () {
        global $wpdb;
        $tweetdate = $_POST["tweetdate"];
        $query = "SELECT * FROM wp_tweettweet WHERE tweetdate='$tweetdate'";
        $results = $wpdb->get_results($query, ARRAY_A);

        foreach($results as $result) {
            $tweet2 =  $result[text];
            $recycleOption = $result[recycle];
            $id = $result[id];

            $currentDateTime = $result[time];
            $time = date('h:i A', strtotime($currentDateTime));


        ?>

            <form class="tweetclass form-inline" action="" method="post">
                <input type="checkbox" name="recycle" <?php if($recycleOption == 1){ echo "checked";} ?>>Recycle Tweet?
                <input class="tweetinput" type="text" name="tweetupdate" value="<?php echo $tweet2; ?>">
                <input type="hidden" name="id" value="<?php echo $id; ?>">
                <input type="text" name="timepicker" class="timepicker" value="<?php echo $time; ?>"/>
                <input class="tweetsubmit" type="submit" value="Save">
                <input class="tweetdelete" type="submit" value="delete">
            </form>
        <?php
        }
    }
    show_scheduled_tweets();
?>
fullCalendar是一个jQuery事件日历。当用户单击某一天时,单击该天即保存为最新。这个日期就是我试图在ajax中保存到tweetdate的日期

在chrome中,当我使用inspector上的network选项卡时,我可以看到ajax结果,单击的日期设置为tweetdate。这不是我的php函数所接受的。在我的php中,tweetdate没有获得分配给它的值

现在,如果我进入我的php函数并将tweetdate设置为实际日期,而不是$u POST[tweetdate];e、 g.2016年6月15日,一切都很完美

我不太清楚到底发生了什么。

要让它发挥作用,你还需要一件重要的事情:

请参阅“ajax脚本”在wp_enqueue_script和wp_localize_script两个函数中

然后,您将在您的js中检索'ajaxurl'和'my_ajax',组合在url:my_ajax.ajaxurl,:

jQuerydocument.readyfunction${ jQuery'bootstrapmodallfullcalendar'.fullCalendar{ dayClick:functiondate、event、jsEvent、view{ var date=date.format; jQuery.ajax{ 类型:POST,, url:my_ajax.ajaxurl,//这里是wp_localize_脚本中的'my_ajax'和'ajaxurl' 数据:{ “操作”:“显示计划的推文”, “tweetdate”:日期 }, 发送前:函数{ console.log'before' }, 成功:功能{ console.log'success' }, 错误:函数{ 控制台。记录“错误” }, }; } }; }; 现在您可以获得$u POST[tweetdate];在你的php中

更新:可能需要为前端添加php函数:

add_action('wp_ajax_nopriv_show_scheduled_tweets', 'show_scheduled_tweets');
和死亡;在函数的末尾。因此,您将有:

add_action('wp_ajax_show_scheduled_tweets', 'show_scheduled_tweets'); // backend
add_action('wp_ajax_nopriv_show_scheduled_tweets', 'show_scheduled_tweets'); // fronted
function show_scheduled_tweets () {
    global $wpdb;
    $tweetdate = $_POST["tweetdate"];
    $query = "SELECT * FROM wp_tweettweet WHERE tweetdate='$tweetdate'";
    $results = $wpdb->get_results($query, ARRAY_A);

    foreach($results as $result) {
        $tweet2 =  $result[text];
        $recycleOption = $result[recycle];
        $id = $result[id];

        $currentDateTime = $result[time];
        $time = date('h:i A', strtotime($currentDateTime));


    ?>

        <form class="tweetclass form-inline" action="" method="post">
            <input type="checkbox" name="recycle" <?php if($recycleOption == 1){ echo "checked";} ?>>Recycle Tweet?
            <input class="tweetinput" type="text" name="tweetupdate" value="<?php echo $tweet2; ?>">
            <input type="hidden" name="id" value="<?php echo $id; ?>">
            <input type="text" name="timepicker" class="timepicker" value="<?php echo $time; ?>"/>
            <input class="tweetsubmit" type="submit" value="Save">
            <input class="tweetdelete" type="submit" value="delete">
        </form>
    <?php
    }
    die(); // very important to get it working
}
参考资料:


dayClick函数中的console.logdate可查看它是否包含预期值。在此处发布该值。您确定日期不仅仅是一个对象吗?是的,它是。例如,如果我单击6月11日,返回值为2016-06-11,那么如果日期设置为2016-06-11,则应该没有问题。你用的是什么版本的日历?对,这就是我困惑的原因。我正在使用2.7.3汉克斯,我非常感激,但恐怕它仍然不起作用。在php中,$tweetdate=$_POST[tweetdate];仍然没有从ajax发送数据。是的,所有这些都是正确的。我正在使用一个非常简单的测试主题。我还有其他ajax调用,例如删除和编辑行,这些调用工作正常。只是这一个没有发挥应有的作用。另外,如果有关系的话,这是作为一个插件设置的。不,这也不起作用。数据库查询没有什么特别之处,是吗$query=从wp_tweet中选择*其中tweetdate='$tweetdate';呃…还是没什么。我们都死了:嗯,还是不工作。不确定这是否重要,但这是一个插件,而不是一个主题。
add_action('wp_ajax_show_scheduled_tweets', 'show_scheduled_tweets'); // backend
add_action('wp_ajax_nopriv_show_scheduled_tweets', 'show_scheduled_tweets'); // fronted
function show_scheduled_tweets () {
    global $wpdb;
    $tweetdate = $_POST["tweetdate"];
    $query = "SELECT * FROM wp_tweettweet WHERE tweetdate='$tweetdate'";
    $results = $wpdb->get_results($query, ARRAY_A);

    foreach($results as $result) {
        $tweet2 =  $result[text];
        $recycleOption = $result[recycle];
        $id = $result[id];

        $currentDateTime = $result[time];
        $time = date('h:i A', strtotime($currentDateTime));


    ?>

        <form class="tweetclass form-inline" action="" method="post">
            <input type="checkbox" name="recycle" <?php if($recycleOption == 1){ echo "checked";} ?>>Recycle Tweet?
            <input class="tweetinput" type="text" name="tweetupdate" value="<?php echo $tweet2; ?>">
            <input type="hidden" name="id" value="<?php echo $id; ?>">
            <input type="text" name="timepicker" class="timepicker" value="<?php echo $time; ?>"/>
            <input class="tweetsubmit" type="submit" value="Save">
            <input class="tweetdelete" type="submit" value="delete">
        </form>
    <?php
    }
    die(); // very important to get it working
}
show_scheduled_tweets();