Jump to content

Problem with this little code


tmyonline

Recommended Posts

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.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/154149-problem-with-this-little-code/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.