Gary
Gary

Reputation: 1011

mixing javascript and php variables in ajax

I have a pretty basic jQuery ajax thing happening, but I want to mix form data that is retrieved by JS with some PHP variables and have them all sent as part of the ajax GET. Should this work?:

var longform = $("input:text").serialize(); 
$.ajax({
    url:    'actions/create.php',
    data:   longform + "domain=<?php echo $domain; ?>&useragent=<?php echo $useragent; ?>&ip=<?php echo $ip; ?>&cookieuser=<?php echo $cookieuser; ?>",

Currently, when create.php tries to echo the variables back, they're empty.

UPDATE

After checking the source as suggested, it comes out like this:

data:   longform + "&domain=example.com&useragent=Mozilla/5.0

Upvotes: 5

Views: 496

Answers (3)

Tommaso Barbugli
Tommaso Barbugli

Reputation: 12031

I would rather put all data in hidden inputs, and then serialize all in once.

Tom

Upvotes: 0

jwueller
jwueller

Reputation: 30986

Everything should be fine if you add PHP's urlencode()-function:

"domain=<?php echo urlencode($domain); ?>&useragent=<?php echo urlencode($useragent); ?>&ip=<?php echo urlencode($ip); ?>&cookieuser=<?php echo urlencode($cookieuser); ?>"

This should prevent syntax errors that could be caused by your data (i.e. if you have backslashes or other special chars in there).

Upvotes: 1

simshaun
simshaun

Reputation: 21466

You need to add an ampersand (&) before domain=. Otherwise, it should be fine.

Do a View Source on the page and make sure the javascript string looks correct as well.

Upvotes: 1

Related Questions