ajax and php collide

Discussion in 'JavaScript' started by 7th Hokage, Aug 16, 2009.

  1. #1
    i have a concept but nowhere to start really as far as ideas.

    I have an php object. lets call it a gun object. this object has attributes and a description, including a description of it firing.

    how would i use ajax to output to a page using the php i've already coded?

    for instance to write to a page the description of a 9mm handgun and how many bullets it holds.

    thanks in advance!
     
    7th Hokage, Aug 16, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Your description is somewhat confusing, since the way you described it you wouldn't even require AJAX since you would be outputing the data from PHP.

    Can you be much more clear?
     
    kblessinggr, Aug 16, 2009 IP
  3. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    create a php file and include your php class in it. when you call the php file, you can send parmeters to it via the querystring. Lets say the php page is test.php. you could do something like test.php?a=1&b=2. you just passed 2 variables to the test page.

    In the php page, use the variables passed to get the response you need out of your php class. Then simply print out the php class response using print or echo commands.

    Then call the php page and pass variables to it using an ajax function. You'll then be able to do whatever you want with the result that is returned.
     
    NoamBarz, Aug 16, 2009 IP
  4. 7th Hokage

    7th Hokage Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    okay i follow what your saying. can you give a small demonstration or explanation of how to use the parameters? and also do parameters have to be numbers or can they be anything such as words?

    or could you do something like


    
    
    if(a==1)
    {
    echo stuff here.
    }
    
    Code (markup):

    also to elaborate for the other reply.

    i have a javascript code that will update the webpage without refreshing(called addNode) it by adding a text node to the page. since the update to the page is written in javascript i'm looking for a way to transfer the information from the php object to be outputted when the addNode() function is called.

    what i'm trying to avoid is writing multiple versions of the script.

    for instance :

    i have a button called "describe" that when clicked would simply output the description. but it would refresh the whole page upon doing so. by using addNode i would like to update the page using the describe button. Does that give you a better more clear idea of what I'm trying to do?
     
    Last edited: Aug 17, 2009
    7th Hokage, Aug 17, 2009 IP
  5. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #5
    in your php page, you first need to retrieve the variables sent:

    $a = $_GET["a"];
    $b = $_GET["b"];

    then do whatever you want:

    if($a==1) echo "1";
    else echo "2";

    variables sent via querystring can be either numbers or text. you can check:

    if(is_numeric($_GET["a"])) $a = $_GET["a"];
    else $a = false;
     
    NoamBarz, Aug 17, 2009 IP
  6. 7th Hokage

    7th Hokage Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    okay. i got everything you said. i tried it but it didn't work for me.

    maybe it's because I want this to work on click. either way I'm going to post my php code. that seems to be the problem since everything else works.:confused:


    
    <script type="text/javascript">
    
    function addNode(inText)
    {
    newGraf=document.createElement("p");
    newText=document.createTextNode(inText);
    newGraf.appendChild(newText);
    docBody=document.getElementsByTagName("body").item(0)//change body to td for table
    docBody.appendChild(newGraf);//and item to 6
    return false;
    }
    Code (markup):
    
    function Xp()
    {
    var xmlhttp;//create a  var for the obj
    if (window.XMLHttpRequest)//if requesting an obj...
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    else
      {
      alert("Your browser does not support XMLHTTP!");
      }
    xmlhttp.onreadystatechange=function()
    {
    if(xmlhttp.readyState==4)
      {
    return addNode('You suck');
      }
    }
    xmlhttp.open("GET","testatt.php?a=1",true);
    xmlhttp.send(null);
    }
    </script>
    Code (markup):

    
    <html>
    <head>
    
    </head>
    <body>
    
    
    <button onclick="return Xp()">Comment</button></br>
    </body>
    </html>
    
    
    HTML:


    
    
    <?php
    
    $a=$_GET["a"];
    
    
    
    if($a==1)
    {
    echo "Hi";
    }
    else
    {
    echo "STFU UP!!";
    
    }
    ?>
    
    
    Code (markup):
     
    7th Hokage, Aug 18, 2009 IP
  7. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #7
    try this:

    
    
    function Xp()
    {
    var xmlhttp;//create a  var for the obj
    if (window.XMLHttpRequest)//if requesting an obj...
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    else
      {
      alert("Your browser does not support XMLHTTP!");
      }
    xmlhttp.onreadystatechange=function()
    {
    if(xmlhttp.readyState==4)
      {
           //return addNode('You suck');
          var text = xmlhttp.responseText;
          alert(text);
      }
    }
    xmlhttp.open("GET","testatt.php?a=1",true);
    xmlhttp.send(null);
    }
    
    
    Code (markup):
     
    NoamBarz, Aug 19, 2009 IP
  8. 7th Hokage

    7th Hokage Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks it works like a charm. i was even able to modify it so that i could control WHERE the text shows up on the page. this has been a BIG help. using this simle script i can take a big step towards fixing my website. i thank you for your assistance
     
    7th Hokage, Aug 20, 2009 IP
  9. jasso

    jasso Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    excellent .. i like it... everyone is so cool... :)
     
    Last edited: Aug 22, 2009
    jasso, Aug 22, 2009 IP