Vitamin Posted October 23, 2010 Share Posted October 23, 2010 I'm not sure if this is a javascript or php question so I'm just posting it here. I'm trying to send a php variable to java script. I can't get it to work. function mapchange(chosen, mapnames) { var box1 = document.filter.map if (chosen == '1') { box1.options.length = 0; box1.options[box1.options.length] = new Option('first choice - option one','oneone'); } } echo '<tr><td><input type="radio" name="gametype" value="1v1" onClick="toggle1v1()" onChange="mapchange(1,' . $test . ')" />1v1</td>'; The combo box in this test should just be recreated with just 'first choice - option one' displayed in it. If I remove the $test from the php it works fine. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/216624-send-a-php-string-to-javascript/ Share on other sites More sharing options...
trq Posted October 23, 2010 Share Posted October 23, 2010 just like in php, string in Javascript need to be surrounded in quotes. echo '<tr><td><input type="radio" name="gametype" value="1v1" onClick="toggle1v1()" onChange="mapchange(1,\'' . $test . '\')" />1v1</td>'; Quote Link to comment https://forums.phpfreaks.com/topic/216624-send-a-php-string-to-javascript/#findComment-1125486 Share on other sites More sharing options...
Vitamin Posted October 23, 2010 Author Share Posted October 23, 2010 HAHA Thank you kind sir. I have been just using the string 'hi' for testing, but now when I create the real string that I am sending that has a (') within the string how do I escape that? The string that I am creating is being pulled from a database. Quote Link to comment https://forums.phpfreaks.com/topic/216624-send-a-php-string-to-javascript/#findComment-1125493 Share on other sites More sharing options...
trq Posted October 23, 2010 Share Posted October 23, 2010 addslashes Quote Link to comment https://forums.phpfreaks.com/topic/216624-send-a-php-string-to-javascript/#findComment-1125494 Share on other sites More sharing options...
Vitamin Posted October 23, 2010 Author Share Posted October 23, 2010 thanks again worked like a charm Quote Link to comment https://forums.phpfreaks.com/topic/216624-send-a-php-string-to-javascript/#findComment-1125496 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.