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. 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'). I am using a Google Browser. I am not big on jsQuery, javascript or AJAX. PHP and HTML is my stronger side.
In my HTML code:
<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:
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!
You are passing action
as a parameter in your code. You are checking func
as in $_POST
.
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:
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.