PDA

View Full Version : Javascript and PHP Get



simsonite
12-26-2008, 02:49 PM
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

magicyte
12-26-2008, 08:40 PM
In PHP:


<?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).

simsonite
12-26-2008, 09:02 PM
Hi, i knew how to do that however i need to transfer the information from the GET through the javascript.

Thanks anyway

magicyte
12-27-2008, 07:27 PM
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=parsing+url+query+in+javascript&btnG=Search

simsonite
12-27-2008, 10:13 PM
Thank you so much for your help :)

simsonite
12-27-2008, 11:22 PM
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.

function send_msg()
{
$.post("server.php",
{ message: $("#message").val()},
function(data){
$("#chatbox").html(data);
$("#message").val("");
}
);
}

Nile
12-28-2008, 12:54 AM
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.

simsonite
12-28-2008, 12:56 PM
Sorry :(

But thanks for your help :)

Nile
12-28-2008, 03:37 PM
I'm glad to help you any time.

diltony
12-31-2008, 12:33 AM
Try this one:
index.html


<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


<?php
echo $_REQUEST['room'];
?>

Nile
12-31-2008, 12:38 AM
I think the problem is solved...

simsonite
12-31-2008, 05:26 PM
Thank you, this may be better i will have a look at it when i have some more time :P