Beginner question about AJAX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asmon
    New Member
    • Jun 2007
    • 4

    Beginner question about AJAX

    I'm a PHP programmer but i'm pretty messed up at this moment with the following script.
    I have a page with a 'TEXTAREA' and a 'PLAY' text.
    when i click PLAY, values are being displayed in the TEXTAREA with a delay of a second.
    the values are stored in array, if for example i have an array with the values (2,5,7,8) then it will first show 2, after a second 4 and so on...

    the values are being set using PHP, then i convert the php array into JS array so i can execute the script above.
    the problem is i need those values to be stored in the database only after
    the user has pressed 'PLAY'.
    see where the problem is?

    I've just started reading about ajax so i don't really know how it works and what i am able to do with it. just sending back the JS array and then executing the query is a bad idea because the values can be easily changed by the client .

    i guess i first need to only put the TEXTAREA and the PLAY button.
    then when the user press PLAY, i have to run a php code with all the calculations and stuff and only then sending the JS script back to the user.
    i am not sure how it works, i know the data you retrieve using AJAX is XML type or something like that. am i able to send a new JS code directly back to the user?

    I know the question looks like a big mess with my English, sorry about that...
  • johnhjohn
    New Member
    • Dec 2006
    • 43

    #2
    Here is the javascript necessary to perform this function

    Code:
    var xmlHttp;
    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
     {
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     }
    catch (e)
     {
     // Internet Explorer
     try
      {
      xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
      }
     catch (e)
      {
      xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
      }
     }
    return xmlHttp;
    }
    
    
    function send_request()  // call this function to send information
    {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
      {
      alert ('Browser does not support HTTP Request');
      return;
      } 
    
    var url='yourpage.php';  // use the php page you are going to send information to
    
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open('POST',url,true);  // uses post method
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send('yourpostvariable='+javascriptvariable);  // will send value as POST (use $_POST['yourpostvariable'] to get this value)
    }
    
    function stateChanged() 
    { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=='complete')  // used for determining if AJAX function is complete
     { 
     var response=xmlHttp.responseText;  // response from php page
     alert(response);  // this will alert the response from the php page (you may use eval or an if statement to perform functions
     } 
    }

    Here is possible php code:
    [PHP]
    <?php

    $variable = $_POST['yourpostvariab le']; // get post variable from javascript
    if($variable == "somevalue" )
    {
    echo "one"; // this will be return to the javascript and will be alerted
    } else {
    echo "two"; // this will be return to the javascript and will be alerted
    }

    Hope I helped :)
    ?>
    [/PHP]

    Comment

    • asmon
      New Member
      • Jun 2007
      • 4

      #3
      ahmm for some reason i can't make it work.
      in my page i put

      <script type="text/javascript" src="something. js">
      send_request()
      </script>


      and changing 'var url' in something.js
      I think it's enough for maknig your example work, what else am i missing?

      There is something else i'm wondering about. in the guide i'm reading right now it says i need to send the response as XML.
      [PHP]<?php
      header('Content-Type: text/xml');
      echo '<?xml version="1.0" encoding="UTF-8" standalone="yes "?>';
      echo '<response>';
      ......
      echo '</response>';
      ?>
      [/PHP]

      by your example i can see i don't have to do that, can you please explain about this?

      thank you.

      Comment

      • johnhjohn
        New Member
        • Dec 2006
        • 43

        #4
        I have heard about the use of xml, but I discovered this way of completing this task, so I decided not to learn the xml way of doing this.

        Are the functions not working?
        If so, tell me what's happening and I can try to ix the problem.

        Thanks.

        Comment

        Working...