Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Javascript and PHP Get

  1. #1
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Javascript and PHP Get

    Hi, i am creating a php and ajax chat application for my website and i want to have multiple rooms that can be accessed using the php get format.
    For example room 1 will be
    chat.php?room=1.

    The problem is that i need to pass this through my javascript script and then into the script that pulls this information from my database. However im not sure how i can get the GET info from the chat page through my javascript and into my database page.

    I hope this was clear enough and thanks in advance

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    In PHP:

    PHP Code:
    <?php

    $roomNum 
    $_GET['room']; // if your.html?room=1, $roomNum = 1

    ?>
    I'm not sure how to do it in JavaScript (you're supposed to get the url and split the string by the question mark, then get the value of room).

  3. The Following User Says Thank You to magicyte For This Useful Post:

    simsonite (12-27-2008)

  4. #3
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Hi, i knew how to do that however i need to transfer the information from the GET through the javascript.

    Thanks anyway

  5. #4
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    I think you'll find this page interesting:

    http://www.idealog.us/2006/06/javascript_to_p.html

    If you want more results, this is for Google:

    http://www.google.com/search?hl=en&q...pt&btnG=Search

  6. #5
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Thank you so much for your help

  7. #6
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Hi, im sorry to ask for help again
    However im not sure how to implement this code into my code and then how to use it in my php script.
    Here is part of the JS file.
    Code:
    function send_msg() 
        {                     
            $.post("server.php", 
            { message: $("#message").val()},
            function(data){    
            $("#chatbox").html(data); 
            $("#message").val("");
            }
            );
        }

  8. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Here ya' go, you should've told us you were using jQuery:
    http://docs.jquery.com/Get

    It's basically the same method your using now, just with .get instead of .post.
    Jeremy | jfein.net

  9. #8
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Sorry

    But thanks for your help

  10. #9
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I'm glad to help you any time.
    Jeremy | jfein.net

  11. The Following User Says Thank You to Nile For This Useful Post:

    simsonite (12-28-2008)

  12. #10
    Join Date
    Dec 2008
    Location
    Nigeria
    Posts
    95
    Thanks
    3
    Thanked 8 Times in 8 Posts

    Default

    Try this one:
    index.html
    Code:
    <html>
    <head>
    <title>Ajax Post</title>
    <script>
    function ajaxRequest(){
     var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
     if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
      for (var i=0; i<activexmodes.length; i++){try{return new ActiveXObject(activexmodes[i])} catch(e){alert("Failed");}}
     } else if (window.XMLHttpRequest) {return new XMLHttpRequest()} else {return false}
    return;
    }
    
    function loadURL(url) {
    	//to prevent browser from caching data
    	var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
    	
    	mygetrequest=new this.ajaxRequest()
        mygetrequest.onreadystatechange=function(){
        if (mygetrequest.readyState==4){
         if (mygetrequest.status==200){
          processData(mygetrequest.responseText);  //download successful
        }
    	else{
    	  alert('download failed.');
    	 }
    	  //ajax request would have been completed at this point
    	}
    	}
    	mygetrequest.open("POST", url+bustcacheparameter, true);
    	mygetrequest.send(null);		
    }
    
    function processData(responseText) {
    document.getElementById("txtHint").innerHTML=responseText;
    }
    
    function init() {
    loadURL("chat.php?room=1");
    }
    onload=init;
    </script>
    </head>
    <body>
    <div id="txtHint"></div>
    
    </body>
    </html>

    chat.php
    Code:
    <?php
    echo $_REQUEST['room'];
    ?>

Tags for this Thread

Bookmarks

Posting Permissions

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