开发者

mixing javascript and php variables in ajax

开发者 https://www.devze.com 2023-01-27 07:50 出处:网络
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?:

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


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.


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


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

Tom

0

精彩评论

暂无评论...
验证码 换一张
取 消