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?
You might try the following way to pass PHP values to JavaScript, assuming the JS isn't in an external JS file:
Code:
var phpValue = <?php echo someValue; ?>;
alert(phpValue);
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).
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:
Code:
if (xmlHttp.readyState==4){
serverResponse=xmlHttp.responseText;
callback(serverResponse);
}
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.
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.
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.
Last edited by binaryWeapon; 11-15-2007 at 01:04 AM..
Reason: unadressed problem
ok can you show me the script where the ajax passes a variable to the server?
try this:
Code:
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
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;
callback(serverResponse);
}
}
ajaxRequest.open("GET", "server.php", true);
ajaxRequest.send(null);
}
everything you echo in the php file will be sent to ajax.
var xmlDoc;
function add()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("users.xml");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("users.xml");
}
else
{
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+='%3C/user%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';
newPHPXML+='%3C/user%3E';
newPHPXML+='%3C/excalo%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.
}
ajaxRequest();
}
addUser();
}
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!