简体   繁体   English

使用AJAX在另一个文件中执行PHP函数

[英]Executing a PHP function in another file using AJAX

I've been looking through past responses and stealing code but nothing seems to work! 我一直在浏览过去的响应并窃取代码,但似乎无济于事! When a script loads, I'm trying to execute a PHP function in an seperate PHP file. 加载脚本时,我正在尝试在单独的PHP文件中执行PHP函数。 I get the alert message that it was submitted BUT looking at the console, none of my console messages are appearing. 我看到警告消息,它是在控制台上提交的,但没有任何控制台消息出现。

I read that the AJAX function requires a response back, that is why I added the echo('Text'). 我读到AJAX函数需要返回响应,这就是为什么我添加echo('Text')的原因。 I am using a Google Browser. 我正在使用Google浏览器。 I am not big on jsQuery, javascript or AJAX. 我对jsQuery,javascript或AJAX并不了解。 PHP and HTML is my stronger side. PHP和HTML是我的强项。

In my HTML code: 在我的HTML代码中:

<body onload = "Start()">

<script>
    var Start = function() {
        $(document).ready(function(){
            $.ajax({
            type: "POST",
            url: 'MainRoutine.php?action=Startfunc',
            data: {func: "Startfunc"},
            success: function(response) {
                $('.result_Postme').text(response);
            }
        })
        alert("Form submitted successfully.\n"); // this is working!
    });
}
</script>

In my MainRoutine.php file: 在我的MainRoutine.php文件中:

if (isset($_POST['func']) && ($_POST['func'] == 'Startfunc')) { 
    ChromePhp::log("Start Handle"); // the expected console message
    echo('Text'); // not sure why I'm doing this...
    Start(); // this is the function I'm trying to execute!
}else{
    ChromePhp::log("Error"); // not even this one displays
}

function Start() {
    echo('Text');  // not sure why I'm doing this either
}

I'm not trying to pass any variables to the PHP "Start()" function, I just want to execute it! 我没有尝试将任何变量传递给PHP“ Start()”函数,我只是想执行它!

You are passing action as a parameter in your code. 您正在将action作为代码中的参数传递。 You are checking func as in $_POST . 您正在像$_POST一样检查func

Please see the below is correct code: 请看下面是正确的代码:

<script>
    var Start = function() {
        $(document).ready(function(){
            $.ajax({
            type: "POST",
            url: 'MainRoutine.php?action=Startfunc',
            data: {func: "Startfunc"},
            success: function(response) {
                $('.result_Postme').text(response);
            }
        })
        alert("Form submitted successfully.\n"); // this is working!
    });
}
</script>

And in your MainRoutine.php file: 在您的MainRoutine.php文件中:

if(isset($_REQUEST['action']) && $_REQUEST['action']=='Startfunc'){ 
    ChromePhp::log("Start Handle"); // the expected console message
    echo('Text'); // not sure why I'm doing this...
    Start(); // this is the function I'm trying to execute!
}else{
    ChromePhp::log("Error"); // not even this one displays
}

function Start() {
    echo('Text');  // not sure why I'm doing this either
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM