tmyonline Posted April 15, 2009 Share Posted April 15, 2009 Hi guys, I have a problem with this little code: <?php echo '<select onchange="displayTimeOptions(value);">'; if ($_GET['type']) { echo '<option>Please select a period</option>'; echo '<option value="monthly"'; if ($_GET['time'] == "monthly") echo "selected"; echo '>Monthly Report</option>'; echo '<option value="quarterly"'; if ($_GET['time'] == "quarterly") echo "selected"; echo '>Quarterly Report</option>'; echo '<option value="yearly"'; if ($_GET['time'] == "yearly") echo "selected"; echo '>Yearly Report</option>'; } echo '</select>'; ?> As you can see, I'm in PHP. The JavaScript function displayTimeOptions(value) works fine. The problem I'm having is that when I need to pass in another variable (PHP variable) to this function, say: onchange="displayTimeOptions(value, $_GET['type'])" i.e., echo '<select onchange="displayTimeOptions(value' . ',' . $_GET['type'] . ');">'; this function fails. It fails because the first argument "value" is not a PHP variable while the second argument is. That's what I think but I'm pretty sure that's the issue. What would be the syntax to pass in a PHP variable to this function so that it works with the existing "value" argument which is not a PHP variable ? I appreciate any help. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/154149-problem-with-this-little-code/ Share on other sites More sharing options...
xtopolis Posted April 15, 2009 Share Posted April 15, 2009 You need single quotes around the javascript arguments: echo '<select onchange="displayTimeOptions(\'value\'' . ',\'' . $_GET['type'] . '\');">'; comes out to: <select onchange="displayTimeOptions('value','cat');"> Use \' to escape them in your string as shown above Quote Link to comment https://forums.phpfreaks.com/topic/154149-problem-with-this-little-code/#findComment-810317 Share on other sites More sharing options...
tmyonline Posted April 15, 2009 Author Share Posted April 15, 2009 Hi xtopolis, Thanks so much. It's a great help. Quote Link to comment https://forums.phpfreaks.com/topic/154149-problem-with-this-little-code/#findComment-810347 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.