Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts

    Passing from PHP to AJAX

    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?

  • #2
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    can you elaborate more?
    maybe try usin echo.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    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).
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #4
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    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:
    Code:
    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
    Code:
    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?

  • #5
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    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);
    }

  • #6
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    but where does the phpget's the value? GET? POST?

  • #8
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    I am already using AJAX to make requests (actually to pass a var to PHP) with GET on the same page.

  • #9
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    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.

  • #10
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    No!

    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

  • #11
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    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.
    Last edited by shedokan; 11-15-2007 at 01:13 AM.

  • #12
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Code:
    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!

  • #13
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    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!.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •