开发者

Disabling Text field with Javascript when value in drop down box is from mysql

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> 


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

Also, $wah is used before initialized.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜