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?
$_POSTin myscript.php to$_GET, and you go directly tomyscript.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",?