Results 1 to 8 of 8

Thread: ajax and php string

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2002
    Location
    Philippines
    Posts
    877

    ajax and php string

    Hi,

    is it possible to use Ajax send request using php string as an Input?

    like this:

    Code:
    function ajaxFunction() {
      var getdate = new Date();l
      if(xmlhttp) { 
      //	var txtname = document.getElementById("txtname");
        xmlhttp.open("POST","testing.php",true); 
        xmlhttp.onreadystatechange  = handleServerResponse;
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send("txtname=" + $test); // im not sure if this is correct to call the php string
      }
    }
    
    <?php
    
    $test = "ABCDEF";
    
    ?>
    i want to use php string '$test' as an input. is this really possible?

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2002
    Location
    Philippines
    Posts
    877

    Re: ajax and php string

    @Admin/Mod

    Sorry please move to the right section.

    Thanks!

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: ajax and php string

    Thread moved from the 'CodeBank PHP' forum to the 'PHP' forum

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: ajax and php string

    no, you can't do it like that (just because of the way you've set it up would be just the wrong way to go about it) -- but you could do it in general. however, once that variable is echoed out into your JavaScript function, it will -not- be able to be changed. HTML and JavaScript are static once they are loaded in the browser.

    this would work:
    PHP Code:
    <?php

    $test 
    "ABCDEF";

    ?>

    <script language="javascript">
    function ajaxFunction() {
      var getdate = new Date();l
      if(xmlhttp) { 
      //    var txtname = document.getElementById("txtname");
        xmlhttp.open("POST","testing.php",true); 
        xmlhttp.onreadystatechange  = handleServerResponse;
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send("txtname=" + <?php echo $test?>); // im not sure if this is correct to call the php string
      }
    }
    </script>
    but, note the jump into 'PHP' in the middle of your JavaScript function -- this would make your JavaScript function display as this to your browser:
    Code:
    <script language="javascript">
    function ajaxFunction() {
      var getdate = new Date();l
      if(xmlhttp) { 
      //	var txtname = document.getElementById("txtname");
        xmlhttp.open("POST","testing.php",true); 
        xmlhttp.onreadystatechange  = handleServerResponse;
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send("txtname=ABCDEF"); // im not sure if this is correct to call the php string
      }
    }
    </script>
    if that's what you wanted, then that would work fine. you just have to make sure you set the value of $test before you are echoing this function to the browser.

    if you need something more dynamic, you could try having your function take in a string parameter and then set that parameter during the event that you're calling the function.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2002
    Location
    Philippines
    Posts
    877

    Re: ajax and php string

    @kows

    Thanks for your help

    i am going to try it later and post feedback.

    B.R,

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2002
    Location
    Philippines
    Posts
    877

    Re: ajax and php string

    Hi kows,

    seems it is not working on the code you've provided the first one

    i tried this:

    Code:
    <?php
    
    $test = "ABCDEF";
    
    ?>
    
    <html>
    <head>
    <title>PHP and AJAX</title>
    
    <script type="text/javascript">
     
    var time_variable;
     
    function getXMLObject()  //XML OBJECT
    {
       var xmlHttp = false;
       try {
         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
       }
       catch (e) {
         try {
           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
         }
         catch (e2) {
           xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
         }
       }
       if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
         xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
       }
       return xmlHttp;  // Mandatory Statement returning the ajax object created
    }
     
    var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object
     
    function ajaxFunction() {
      var getdate = new Date();  //Used to prevent caching during ajax call
      if(xmlhttp) { 
        xmlhttp.open("POST","test2.php",true);
        xmlhttp.onreadystatechange  = handleServerResponse;
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send("txtname=" + <?php echo $test; ?>); // This line is not working
       // xmlhttp.send("txtname=ABCDEF"); // This is working fine
      }
    }
     
    function handleServerResponse() {
       if (xmlhttp.readyState == 4) {
         if(xmlhttp.status == 200) {
           document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element 
         }
         else {
            alert("Error during AJAX call. Please try again");
         }
       }
    }
    </script>
    
    <form name="myForm">
    <table>
     <tr>
      <td colspan="2"><input type="button" value="Submit" onClick="ajaxFunction();" /></td>
     </tr> 
    </table>
    <div id="message" name="message"></div> 
    </form>
    
    </head>
    </html>

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: ajax and php string

    err, I merely copied and pasted what you had and fixed the way PHP would be input to the browser. I did reflect on what WOULD work in my example, but didn't edit the PHP stuff. anyway -- I guess that's partly my fault, but you should really learn the syntax of whichever languages you're playing around with before attempting to do something such as ajax. javascript, like I explained before, is static code once it's sent to the browser. so, when in doubt, you could just check the source of whatever you're making.

    this line of PHP:

    xmlhttp.send("txtname=" + <?php echo $test; ?>); // This line is not working

    creates this line of JavaScript:

    xmlhttp.send("txtname=" + ABCDEF); // This line is not working

    of course, since you haven't defined ABCDEF as a variable in JavaScript, this would simply send an empty string. so, you need to change the PHP to the following, so that JavaScript knows you're sending a string and not trying to reference a variable:

    xmlhttp.send("txtname=<?php echo $test; ?>");

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2002
    Location
    Philippines
    Posts
    877

    Re: ajax and php string

    Hi,

    Kows,

    Code:
    xmlhttp.send("txtname=<?php echo $test; ?>");
    This is working fine now. i am going to practice ajax and jscript

    Thank you

Posting Permissions

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



Click Here to Expand Forum to Full Width