2

I am using ajax to send data to a php function. The end goal is to display rows of data based on $tweetdate. Here is my ajax script:

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')
            },
        });
    }
});

Here is my php function (add_action is for WordPress usage):

<?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 is a jQuery event calendar. When the user clicks on a day (dayClick) that day is saved to date. That date is what I am trying to save to "tweetdate" in my ajax.

In chrome, when I use the network tab on the inspector I can see the ajax result and the date clicked on is set to "tweetdate". That isn't getting picked up by my php function. In my php "tweetdate" is not getting a value assigned to it.

Now, if I go into my php function and set "tweetdate" to an actual date instead of $_POST["tweetdate"]; e.g. 2016-06-15 than everything works perfectly.

I'm not quite sure what is going on.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
user715564
  • 1,650
  • 2
  • 25
  • 60
  • `console.log(date)` inside your `dayClick` function to see that it contains the expected value. Post that value here. – mferly Jun 16 '16 at 00:28
  • Are you sure `date` is not just an object? – mferly Jun 16 '16 at 00:33
  • Yes, it does. An example is if I click on June 11th, the returned value is 2016-06-11 – user715564 Jun 16 '16 at 00:33
  • Then there should be no problem, if `date` is set to 2016-06-11. What version of the calendar are you using? – mferly Jun 16 '16 at 00:39
  • Right, that's why I am confused. I am using 2.7.3 – user715564 Jun 16 '16 at 00:40
  • There is no v3.1 only v1 and v2. Is [this](http://fullcalendar.io/docs/) the calendar you're using? – mferly Jun 16 '16 at 00:41
  • I was looking at the wrong thing. I edited my above comment. I'm using 2.7.3. Yes, your link is correct. – user715564 Jun 16 '16 at 00:42
  • Re updated again… i have made a small error: it's `add_action('wp_ajax_nopriv_` instead of `add_action('wp_ajax_no_priv_`. Make sure that all your php code is in the function.php file of your theme. outside this file, in other php files you can call your function… – LoicTheAztec Jun 16 '16 at 05:06
  • Can you try: `echo $query = "SELECT * FROM wp_tweettweet WHERE tweetdate='$tweetdate'";` and run that from your phpmyadmion or workbench? – abhiklpm Jun 16 '16 at 05:53
  • I don't think that would work because $tweetdate gets its value from what date the user clicks on. – user715564 Jun 16 '16 at 12:44

1 Answers1

1

To make it work, you need one more essential thing:

add_action('wp_enqueue_scripts', 'my_custom_scripts'); 
my_custom_scripts(){
    // Here you register your script for example located in a subfolder `js` of your active theme
    wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/script.js', array('jquery'), '1.0', true );
    // Here you are going to make the bridge between php and js
    wp_localize_script( 'ajax-script', 'my_ajax', array( 'ajaxurl' =>   admin_url( 'admin-ajax.php' ) ) );
}

See that 'ajax-script' is in both functions wp_enqueue_script() and wp_localize_script()

Then you will retrieve 'ajaxurl' and 'my_ajax' in your js combined in url:my_ajax.ajaxurl,:

jQuery(document).ready(function($) {

    jQuery('#bootstrapModalFullCalendar').fullCalendar({
        dayClick:  function(date, event, jsEvent, view) {
                var date = date.format();
            jQuery.ajax({
                type: "POST",
                url: my_ajax.ajaxurl,// Here 'my_ajax' & 'ajaxurl' from wp_localize_script()
                data: {
                    'action': 'show_scheduled_tweets',
                    'tweetdate': date
                },
                beforeSend: function() {
                    console.log('before')
                },
                success: function(){
                    console.log('success')
                },
                error: function(){
                    console.log('error')
                },
            });
        }
    });
});

Now you can get the $_POST["tweetdate"];in your php!!!


Update: May be you need to add to your php function (for front end):

add_action('wp_ajax_nopriv_show_scheduled_tweets', 'show_scheduled_tweets');

And to and die();at the end inside your function. so you will have:

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
}

Update 2: important! — It should work this time!

  • I have made a little typing error:
    It's add_action('wp_ajax_nopriv_ … instead of add_action('wp_ajax_no_priv_ …

  • These php codes needs to be on the function.php file of your active theme (or child theme).

Then you will call your function somewhere else or you can hook it with some add_action() hooks.

show_scheduled_tweets();

References:

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks, I really appreciate it but I am afraid it's still not working. In php, $tweetdate = $_POST["tweetdate"]; still isn't getting the data sent from ajax. – user715564 Jun 16 '16 at 04:08
  • Yes, all of that is correct. I'm using a very barebones testing theme. I have other ajax calls e.g. deleting and editing rows that are working fine. It's just this one that is not working as it should. Also, if it matters, this is set up as a plugin. – user715564 Jun 16 '16 at 04:13
  • Nope, that didn't work either. Nothing stands out with the database query does it? $query = "SELECT * FROM wp_tweettweet WHERE tweetdate='$tweetdate'"; – user715564 Jun 16 '16 at 04:23
  • Ugh...still nothing. We both die :) – user715564 Jun 16 '16 at 04:34
  • Hmm still not working. Not sure if this matters but this is a plugin and not a theme. – user715564 Jun 16 '16 at 12:35
  • @user715564 I have already made a plugin with multiple ajax in it… The only difference is the path of your js File if its located in the plugin. – LoicTheAztec Jun 16 '16 at 14:52
  • The "show_scheduled_tweets" function is inside of the admin_page function. Would that cause any issues? – user715564 Jun 16 '16 at 17:28
  • @user715564 admin_page function, is a page from where, your plugin? At this point, I haven't enough information to help you more. You could update your question adding additional information (files structure / location), plugin info and whatever could be useful to help you here on SO. – LoicTheAztec Jun 16 '16 at 19:25