views:

427

answers:

2

Hi

I have a simple script in HTML, using a dropdown menu. When the value 1 is selected, the user can write in the text field, if value 2 is selected, it disables the text field.

However, i changed the values of the dropdown menu, so that one value was from a mysql table(using PHP) and the other remained 'option value='1''. Yet now neither text field is disabled. Below is the code.

`<script type="text/javascript">
   function findselected() { 

      if (document.form.selmenu.value == <?php echo $id; ?>) {
      document.form.txtField.disabled=true;
          //      return false;  // not sure this line is needed
       } else {
      document.form.txtField.disabled=false;
          //      return false;  // not sure this line is needed
       }

} `

And the PHP section

if(mysql_num_rows($SQL) == 1)
{
echo "<select name='selmenu' onChange='findselected()'>"; 
echo "<label>TCA_Subject</label>";

while ($row=mysql_fetch_array($SQL)) { 
    echo "<option value='$id'>$thing</option>";
       echo "<option value='2'>Choice 2</option>"; 
    } 

    }
   echo "<option value=$userid>'Choice 1'</option>";
?>  <option value='2'>Choice 2</option>";
</select> 

I have tried taking the second option value out of the loop, putting it into html, editing the variable in the javascript function. There is not a fault with the PHP as it is retrieving the right results and displaying it, yet the text field doesnt become disabled.

Does anyone know of a possible solution?

Thanks

Changes made to test simple php variable

<script type="text/javascript">
    function findselected() { 
   if (document.form.selmenu.value == <?php echo $wah;?>) {
      document.form.txtField.disabled=true;
//      return false;  // not sure this line is needed
   } else {
      document.form.txtField.disabled=false;
//      return false;  // not sure this line is needed
   }
} 
</script>

<?php $wah = 'hello'; 
echo $wah;
?>

<form action="dump.php" method="POST" name="form"> 
<select name="selmenu" onChange="findselected()"> 
<option value="1">Choice 1</option> 
<option value="<?php $wah;?>">Choice 2</option> 
</select> 
A: 

You are missing an echo in the HTML and some quotes around the echo in the javascript.

Also, $wah is used before initialized.

Tgr
Thanks for the reply, iv tried changing the PHP Variable in the javascript to <?php echo '$wah';?> and when that didnt work to <?php echo "$wah";?>Both times with the <option value="<?php echo $wah;?>">Choice 2</option>
SteveJ313
Sorry for the double post, iv tried declaring the PHP variable above the JavaScript function but still no luck. iv even moved the function below the form and it still doesnt work
SteveJ313
A: 

Try this out:

<?php $wah = 'hello'; 
  echo $wah;
?>
<script type="text/javascript">
  function findselected() { 
    if (document.form.selmenu.value == '<?php echo $wah;?>') {
      document.form.txtField.disabled=true;
      // return false;  // not sure this line is needed
     } else {
       document.form.txtField.disabled=false;
      // return false;  // not sure this line is needed
     }
  } 
</script>

<form action="dump.php" method="POST" name="form"> 
<select name="selmenu" onChange="findselected()"> 
  <option value="1">Choice 1</option> 
  <option value="<?php echo $wah;?>">Choice 2</option> 
</select> 
Shiki
Thanks. It works with a pre-set variable, but if i attempt to call the database and fill the option value with that, instead of $wah it doesnt work. Im guessing id need to make the database call at the beginning of the script and store the variables result, before placing it in the option value.Thanks for getting it working
SteveJ313
I guess the problem might be in PHP generating the HTML then.
Shiki