1

I want to get data(time) by comparing userid and date against a post. But for testing I am simply calling php function by ajax. I made a separate php file (myscript.php) in which I made a function and echo something like that.

function my_action(){
    echo "dasdasasdaaddad";
    $date = $_POST['date'];
    echo $date;
    return $date;
}

Now when I click on a button I get the date and userid.

global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
add_action( 'the_content', 'my_action_javascript' );
function my_action_javascript() { 
$current_user = wp_get_current_user();
$uid = $current_user->ID;
?>
<script type="text/javascript" >
    jQuery(".date").click(function(){

        clicked = this; 
    var dates= jQuery(clicked).closest("ul").find(".getdate").val();
    var item= jQuery(this).closest("li.lia");
    var date = jQuery(item).find("input.getdate").val();
    //var dates = jQuery(item).find("input.getdate").val();


    alert(date);
    jQuery.ajax({

        type:"post",
        url: "<?php $current_url;?>/myscript.php",

    data : {
        'action': 'my_action',
        'date': date,
        'userid': "<?php echo $uid?>"
    },

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    success: function(data) {
            successmessage = 'Data was succesfully captured';
            $("label#successmessage").text(successmessage);
        },
        error: function(data) {
            successmessage = 'Error';
            $("label#successmessage").text(successmessage);
        },
    });
});
</script> 
<?php
}

I gave the URL of that file in ajax url. Now it should print data from myscript.php file but I am getting this result in response from ajax.

Got this from the server:0

I have checked the network also and it seems good to me. ajax is getting userid and date but response is 0.Don't know why?

1
  • Start by working out if the problem's in myscript.php or your ajax. If you change the $_POST in myscript.php to $_GET, and you go directly to myscript.php?date=test, does the output look right? If so, does that URL match the URL generated by this line: url: "<?php $current_url;?>/myscript.php",? Commented Jan 4, 2017 at 11:23

2 Answers 2

0

on myscript.php page remove function my_action().. because you are not calling this.

if you want to call this before that use my_function();

<?php
my_function();
function my_action(){
 echo "dasdasasdaaddad";
$date = $_POST['date'];
echo $date;
return $date;
}

?>
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks but the result is same. Got this from the server:0
can you write on php page print_r($_POST); and check
still nothing. :/
have you tried with print_r($_POST); on myscript.php page
yes i have tried it. print_r($_POST); my_function(); function my_action(){ echo "dasdasasdaaddad"; $date = $_POST['date']; echo $date; return $date; }
|
0

You are not calling your my_action function in myscript.php file. Call your function you will get the your desired result.

<?php

function my_action(){
    echo "dasdasasdaaddad";
    $date = $_POST['date'];
    echo $date;
    return $date;
}

my_action();

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.