View Full Version : Passing from PHP to AJAX

11-14-2007, 02:59 PM
How would I pass values from PHP the JavaScript with AJAX? I would like to use the callback function and make an alert with the serverData. How would I send the PHP data to AJAX so that it could be picked up by the serverData in the callback function?

11-14-2007, 04:23 PM
can you elaborate more?
maybe try usin echo.

11-14-2007, 06:50 PM
You might try the following way to pass PHP values to JavaScript, assuming the JS isn't in an external JS file:

var phpValue = <?php echo someValue; ?>;

If it is inside an external file, you could try changing the JS file to a PHP file, and creating a separate PHP include file, for that script. Since it would be a PHP file, it would be processed as PHP, so there'd be no issues (hopefully).

11-15-2007, 12:30 AM
Im going to resort to rpgfan's solutin if I can't figure out how to do it the other way.

can you elaborate more?

Ok, so theres a function:

function callback(serverData, serverStatus) { // Called automatically when we get data back from server

That gets called automatically when the server replies to XMLHTTP request. I know that if I put
alert(serverData) it will pop up an alert with the data from the server. My question is how do I use PHP to "tell" AJAX what the serverData is?

11-15-2007, 01:28 AM
you don't have to use php use simple javascript, unless your'e reloading the page then I would suggest using GET or POST.
so maybe try that the responce text will be in a variable named serverData:

if (xmlHttp.readyState==4){

11-15-2007, 01:32 AM
I know but what I need to know is the PHP code for making the responseText. I'm going to try return("Blah blah") -- in the php -- and see if that works.

11-15-2007, 01:44 AM
but where does the phpget's the value? GET? POST?

11-15-2007, 01:51 AM
I am already using AJAX to make requests (actually to pass a var to PHP) with GET on the same page.

11-15-2007, 01:58 AM
so your'e getting with ajax a value from the server then refresh the page with "?value=value" and then put it in javascript?
if you want me to help you better show a source code.

11-15-2007, 02:03 AM

I'm using AJAX to pass a variable to the server. That's it. No page refreshing.

I want to know how to make the server (PHP) send info BACK to AJAX, which is picked up in the callback() function as the serverData variable. If this can't be done, tell me. All I need to know is how to do that, it's not specific to my situation, or my source code. Just in general.

11-15-2007, 02:06 AM
ok can you show me the script where the ajax passes a variable to the server?
try this:

function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!

// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
serverResponse = ajaxRequest.responseText;
ajaxRequest.open("GET", "server.php", true);
everything you echo in the php file will be sent to ajax.

11-15-2007, 02:22 AM
var xmlDoc;
function add()
//load xml file
// code for IE
if (window.ActiveXObject)
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
alert('Your browser cannot handle this script');

alert('now executing addUser func');

function addUser()
var newPHPXML = '%3C?xml%20version=%221.0%22%20encoding=%22ISO-8859-1%22?%3E';
var users = parseFloat(xmlDoc.getElementsByTagName("excalo")[0].getAttribute("users"));
var users2 = parseFloat(xmlDoc.getElementsByTagName("excalo")[0].getAttribute("users"))+1;
var encPass = encrypt(document.register.password.value, 'aouenxlcjh');

newPHPXML+='%3Cexcalo%20users=%22' + users2 + '%22%3E';
var i = 0;
for(i=0; i<users; i++)
var x = i

newPHPXML+='%3Cuser%20username=%22' + xmlDoc.getElementsByTagName("user")[x].getAttribute("username") + '%22%3E';
newPHPXML+='%3Cname%3E' + xmlDoc.getElementsByTagName("name")[x].childNodes[0].nodeValue + '\%3C/name%3E';
newPHPXML+='%3Cage%3E' + xmlDoc.getElementsByTagName("age")[x].childNodes[0].nodeValue + '\%3C/age%3E';
newPHPXML+='%3Cpassword%3E' + xmlDoc.getElementsByTagName("password")[x].childNodes[0].nodeValue + '\%3C/password%3E';
newPHPXML+='%3Cemail%3E' + xmlDoc.getElementsByTagName("email")[x].childNodes[0].nodeValue + '\%3C/email%3E';
newPHPXML+='%3Cwebsite%3E' + xmlDoc.getElementsByTagName("website")[x].childNodes[0].nodeValue + '\%3C/website%3E';
newPHPXML+='%3Cuser%20username=%22' + document.register.username.value + '%22%3E';
newPHPXML+='%3Cname%3E' + document.register.name.value + '\%3C/name%3E';
newPHPXML+='%3Cage%3E' + document.register.age.value + '\%3C/age%3E';
newPHPXML+='%3Cpassword%3E' + encPass + '\%3C/password%3E';
newPHPXML+='%3Cemail%3E' + document.register.email.value + '\%3C/email%3E';
newPHPXML+='%3Cwebsite%3E' + document.register.website.value + '\%3C/website%3E';
alert('newPHPXML configured');

/////////////////////////////// AJAX Send the Data

function callback(serverData, serverStatus) { // Called automatically when we get data back from server
alert('Registration successfull!');

function ajaxRequest() {
var AJAX = null; // Initialize the AJAX variable.
if (window.XMLHttpRequest) { // Does this browser have an XMLHttpRequest object?
AJAX=new XMLHttpRequest(); // Yes -- initialize it.
} else { // No, try to initialize it IE style
AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
} // End setup Ajax.
if (AJAX==null) { // If we couldn't initialize Ajax...
alert("Your browser doesn't support AJAX."); // Sorry msg.
return false // Return false, couldn't set up ajax
AJAX.onreadystatechange = function() { // When the browser has the request info..
if (AJAX.readyState==4 || AJAX.readyState=="complete") { // see if the complete flag is set.
callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
} // End Ajax readystate check.
var url='login-out.php?newxml=' + newPHPXML; // This is the URL we will call.
AJAX.open("GET", url, true); // Open the url this object was set-up with.
AJAX.send(null); // Send the request.


Here's my code so far...(still under construction) Thank you for your last post, I haven't tried it yet but it sounds like it will work.

PS: Basically my script reads an XML file, rewrites the existing users, and adds one. (its like a register thing) I have it URL encoded and stored in one var so I can send it with GET. I just realized the escape() func so later I'll simply use an escape() on it, instead of URL encoding it in the JS. THnx!

11-15-2007, 02:40 AM
no problem!.
if you still have problems you can PM me anytime.
and your'e the first one I have helped with programming so I'm happy!.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum