从ajax调用单独文件中的php函数不起作用 [英] calling to php function in seprate file from ajax not working

查看:70
本文介绍了从ajax调用单独文件中的php函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将用户ID和日期与帖子进行比较来获取数据(时间).但是对于测试,我只是通过ajax调用php函数.我制作了一个单独的php文件(myscript.php),在其中创建了一个函数并回显了类似的内容.

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;
}

现在,当我单击按钮时,我会得到日期和用户ID.

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
}

我用ajax url给出了该文件的URL.现在它应该从myscript.php文件中打印数据,但是我收到来自Ajax的响应.

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

我也检查了网络,对我来说似乎很好.Ajax正在获取用户ID和日期,但响应为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?

推荐答案

您没有在myscript.php文件中调用my_action函数.调用您的函数,您将获得所需的结果.

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();

?>

这篇关于从ajax调用单独文件中的php函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆