If this is possible, why is it not working? It is too much code, I will only put pseudo code and do not tell me that it is a syntax error because all the elements work in isolation, the problem is when integrating them together.
I have:
SET UP:
1) - An html form with various inputs and a single submit button. Like this:
<form action="" method="POST">
<input type="submit"></input>
</form>
2) - PHP code that works on submit and gets form input values and it is working. For example:
<?php if($_SERVER['REQUEST_METHOD'] == 'POST') {
// get values here, which is working //
// database inserts, which are working too //
?>
3) - A Javascript js_function() that runs a client based test and prints the output in the console.log. For example:
<script>
(function(w) {
// code that runs //
// prints results on console.log() //
}
</script>
4) - An Ajax function (inside the Javascript main function, at the end before the closing tag) that gets the variable that prints in the console.log and posts it to the database in the fly, like this:
var JSoutput = "js_function_return";
function loadXMLDoc(){
// check browser, status and response, etc //
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
// use a div to print response, which works all the time
}
xmlhttp.open("GET","boguspage.php?JSoutput="+JSoutput,true);
xmlhttp.send();
}
5) - A bogus php page where the GET results of the Ajax query are collected and passed to the database. This works.
WHAT WORKS:
If I put my javascript function to load on the body body onload="js_function()"
and the Ajax function onclick="loadXMLDoc()"
on the "submit" button, everything works seamlessly, there is no problem. So, it is NOT a syntax issue, please read well before answering.
WHAT SEEMS IMPOSSIBLE:
1) - I need to have either both the Javascript function and the Ajax function execute simultaneously on a single POST or,
2) - Move the Javascript to the onclick
event of the button and have the Ajax function wait for the Javascript function to execute and do an automatic POST
or GET
to the database on its own, when done.
I do not know how to accomplish either or. I have tried for 3 days and while every piece works separately or loaded in the body, joining them on submit causes the first Javascript function to execute too fast so the Ajax call returns empty and I get a PHP undefined variable error. This does not happen however if I have the Javascript to execute first in the body.
But I have a requirement to make it all happen on a single submit. How can I accomplish this?