I have this drop down list and my idea is when i click on videos that i disable other drop down list.This is not working.Do i need something else?
<select name="Type" id="type">
<option value="0">Please Select</option>
<option name="image" value="I" >Images</option>
<option name="video "value="V">Videos</option>
<?php
$value=$_POST['image'];
?>
</select>
<select id="text-one" <?php if($value=="V"){ ?> disabled="disabled"<?php }?> >
<option selected value="base">Please Select</option>
<option value="Dig">Digital</option>
</select
I have this drop down list and my idea is when i click on videos that i disable other drop down list.This is not working.Do i need something else?
<select name="Type" id="type">
<option value="0">Please Select</option>
<option name="image" value="I" >Images</option>
<option name="video "value="V">Videos</option>
<?php
$value=$_POST['image'];
?>
</select>
<select id="text-one" <?php if($value=="V"){ ?> disabled="disabled"<?php }?> >
<option selected value="base">Please Select</option>
<option value="Dig">Digital</option>
</select
Share
Improve this question
asked Oct 18, 2012 at 0:15
svensonsvenson
393 silver badges7 bronze badges
2
- 1 Yeah, you need JavaScript instead of PHP. – hjpotter92 Commented Oct 18, 2012 at 0:18
- As The DOCTOR pointed this out - you want to have something happening in the web, on the client side, and not on the server that generates static page that is being then shown to the user. Check jquery site for javascript and nice support of things you need. – Grzegorz Commented Oct 18, 2012 at 0:25
2 Answers
Reset to default 2You can do this with PHP, but that's going to require submitting form the data so you can then access $_POST
, and then loading a new page, that's basically the same as the original, just with text-one disabled.
Remember, PHP runs server side, and once it's rendered in the browser, nothing else can be done via PHP. For example, you've got a line in your sample code that show's you tring to assign $_POST['image']
to $value
- until you submit a form, $_POST
will be empty.
Most likely, you want to do this client side and without a reload, and this can be done using javascript.
As a basic overview:
- monitor the
onChange
event handler for yourtype
select element - check the value of the
type
select element, and set thedisabled
attribute on thetext-one
as needed
Another option (maybe simpler?):
attach an onclick
attribute to the video input, that will run a javascript function that disables text-one
jQuery will make some of this easier, but you could write all of the above in plain javascript, without any libraries.
try this code :
<script>
function disable(val)
{
if(val=="V")
document.getElementById("text-one").disabled=true;
else
document.getElementById("text-one").disabled=false;
}
</script>
<select name="Type" id="type" onchange="disable(this.value)">
<option value="0">Please Select</option>
<option name="image" value="I" >Images</option>
<option name="video "value="V">Videos</option>
</select>
<select id="text-one" >
<option selected value="base">Please Select</option>
<option value="Dig">Digital</option>
</select>