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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.