[英]Executing a PHP function in another file using AJAX
我一直在瀏覽過去的響應並竊取代碼,但似乎無濟於事! 加載腳本時,我正在嘗試在單獨的PHP文件中執行PHP函數。 我看到警告消息,它是在控制台上提交的,但沒有任何控制台消息出現。
我讀到AJAX函數需要返回響應,這就是為什么我添加echo('Text')的原因。 我正在使用Google瀏覽器。 我對jsQuery,javascript或AJAX並不了解。 PHP和HTML是我的強項。
在我的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>
在我的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
}
我沒有嘗試將任何變量傳遞給PHP“ Start()”函數,我只是想執行它!
您正在將action
作為代碼中的參數傳遞。 您正在像$_POST
一樣檢查func
。
請看下面是正確的代碼:
<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>
在您的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.