Here's what I'm working with...
Some javascript (let's call it 'apples.js') is executed client-side, and one of the things I'd like to do within that javascript is use Ajax to run a php script in the background. That much is easy enough.
The php script I'm working with was based off of a php page with data fields and a call to a js function at the end which saves the data. (Let's call this other js file 'bananas.js') I would like to populate the fields of the background php file by passing data from apples.js to the php file. My dillema is that my php file needs to execute a function within bananas.js at the end, but I don't think this will work if the php file is running in the background. Obviously I have to call the bananas.js function by some method other than an onclick, but even so, I don't think javascript can be run in the background. Is that correct? Is there a simple way of handling this?
I guess worst-case-scenario, I'll have to adapt the function from bananas.js into apples.js and skip the PHP intermediary script. I'm hoping I can avoid this because I'd prefer to work within the existing framework of this software and keep things simple (also I'm not terribly concerned programming efficiency; this software is being developed as part of my company's private portal).
Why don't you just use a callback on your ajax method.
If you're using jQuery...
apples.js
$.ajax({
type: 'POST',
data: {
val1: 'one',
val2: 'two'
},
success: function (data) { //data is what is being returned by the .php script
//now call your bananas.js function and pass in your data
callBananas(data);
},
error: function (jqXHR) {
console.log(jqXHR);
}
});
bananas.js
var callBananas = function(data) {
//Do something...
}
In general, calling JavaScript from PHP is not worth the trouble.
Just reimplement the function in PHP.