Chud37
Chud37

Reputation: 5027

quick Jquery .load chat not working

I have the following jquery:

var msg = $("#newmessage").val();
var user = $("#userchat").val();

var filename = "/inc/chat.php?msg="+msg+"&user="+user; 
alert(filename);
$("#chatData").load(filename);

when 'msg' does not have a space in it, the #chatData loads fine and posts the variable.

When it does have a space in it, I just get a blank div. With no information in it whatsoever.

if I load up the php file that inserts the data into the DB, and manually type the same GET data, it works fine.

Whats going on?

Upvotes: 0

Views: 154

Answers (5)

iambriansreed
iambriansreed

Reputation: 22261

Also consider:

$("#chatData").load('/inc/chat.php', 
    { 'msg' : $("#newmessage").val(), 'user' : $("#userchat").val() }
);

URI encoding is done, if needed, by jQuery.

You don't have to worry about URI encoding as the POST method is used since data is provided as an object (source).

In this case POST may be better than GET anyways.

If you were using $_GET in your php file you will need to use either $_REQUEST or $_POST.

Upvotes: 2

Matt
Matt

Reputation: 7040

The space could be causing an issue - try javascript's encodeURIComponent():

var msg = encodeURIComponent($("#newmessage").val());
var user = encodeURIComponent($("#userchat").val());

Upvotes: 0

Austin
Austin

Reputation: 6034

You can use either escape, encodeURI or encodeURIComponent, but escape is the only method supported by every browser, although most modern browsers support the latter.

Reference

Take a look at this document, which does a good job of explaining all three.

Upvotes: 0

oezi
oezi

Reputation: 51817

you have to encode your message before sending using encodeURIComponent() and decode on server-site using urldecode().

doing this will escape/encode special characters that aren't allowed in an url or that will break your query-string otherwise (like a & in your message that would otherwise start a new argument).

Upvotes: 0

Lixus
Lixus

Reputation: 296

Try using

encodeURIComponent(msg)

Upvotes: 2

Related Questions