Jump to content

how do i pass a variable in javascript.


jasonc

Recommended Posts

i have the following code i have got from

 

http://www.dynamicajax.com/fr/AJAX_Driven_Web_Chat-271_290_291.html

 

but having problems passing a username to it from a database.

 

original code

...
...

function getChatText() {
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
receiveReq.open("GET", 'getChat.php?chat=1&last=' + lastMessage, true);
receiveReq.onreadystatechange = handleReceiveChat; 
receiveReq.send(null);
}			
}
....
....
<form id="frmmain" name="frmmain" onsubmit="return blockSubmit();">
<input type="button" name="btn_get_chat" id="btn_get_chat" value="Refresh Chat" onclick="javascript:getChatText();" />
<input type="button" name="btn_reset_chat" id="btn_reset_chat" value="Reset Chat" onclick="javascript:resetChat();" /><br />
<input type="text" id="txt_message" name="txt_message" style="width: 447px;" />
<input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="javascript:sendChatText();" />
</form>
....

 

what i want to do is first off send the username from the form, (later on i will hash this in some way to prevent someone hacking in or something)

but to start with and to better understand this more i only want to send the username which is previously got from the login part of my site.

 

 

 

i thought that this code whould do it, but it does not seem to work....

 

my code

...
...

function getChatText(username) {
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
receiveReq.open("GET", 'getChat.php?chat=' + username + '&last=' + lastMessage, true);
receiveReq.onreadystatechange = handleReceiveChat; 
receiveReq.send(null);
}			
}
....
....
<form id="frmmain" name="frmmain" onsubmit="return blockSubmit();">
<input type="button" name="btn_get_chat" id="btn_get_chat" value="Refresh Chat" onclick="javascript:getChatText('their username');" />
<input type="button" name="btn_reset_chat" id="btn_reset_chat" value="Reset Chat" onclick="javascript:resetChat();" /><br />
<input type="text" id="txt_message" name="txt_message" style="width: 447px;" />
<input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="javascript:sendChatText('their username');" />
</form>
....

 

 

can i get some advise on how to correctly send data.

Link to comment
https://forums.phpfreaks.com/topic/194222-how-do-i-pass-a-variable-in-javascript/
Share on other sites

You are using the correct syntax to pass the argument.

 

 

Javascript functions are discussed here:

http://www.w3schools.com/js/js_functions.asp

 

 

And they specifically show how to pass an argument to a function in this example:

http://www.w3schools.com/js/tryit.asp?filename=tryjs_function2

 

 

I would recommend that you add in a call to alert to make sure that you're getting the correct data.  For example, if you have this line in your code:

 

<input type="button" name="btn_get_chat" id="btn_get_chat" value="Refresh Chat" onclick="javascript:getChatText('foo');" />

 

and you change your javascript function to this:

 

function getChatText(username) {
alert(username)
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
receiveReq.open("GET", 'getChat.php?chat=' + username + '&last=' + lastMessage, true);
receiveReq.onreadystatechange = handleReceiveChat;
receiveReq.send(null);
}         
}

 

 

Do you get the popup that says "foo"?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.