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
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"?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.