Reputation: 214
I want to link a tag in option tag value. select,sample and add is my option tag values. If I click add option I want to link client.php page. onChange="window.location.href=this.value" will open in same page as well as it will display page not found error for fetched value from database.
I affixed my code.
echo '<select name="c" onChange="window.location.href=this.value" style="width:160px;">';
echo '<option selected>select</option>';
$op="select * from client";
$op1=mysql_query($op); while ($row = mysql_fetch_array($op1))
{
echo '<option value='.$row["c_name"].'>'.$row["c_name"].'</option>';
}
echo '<option value="/client.php">Add new client</option>';
echo '</select>';
Upvotes: 0
Views: 100
Reputation: 115
I Don't know if i understand what you need but if it was me i would just create a javascript function that would allow me to perform a custom action based on the option value tag, if it is an hyperlink or an option value. I was thinking on something like this:
<script type="text/javascript">
document.getElementById('c').onchange = function(){
var test = (this.value.indexOf('/')===0) ? true : false;
if(test)
{
window.location.href = this.value;
}
}
</script>
And on the php code you only have to change this:
echo '<select name="c" id="c" style="width:160px;">';
Just tell me if it doesn't do what you need
Upvotes: 1
Reputation: 23958
Its because, you are not sending the current page url to it.
echo '<select name="c" onChange="window.location.href=this.value" style="width:160px;">';
Should be
echo '<select name="c" onChange="window.location.href=<?php echo $_SERVER[\'PHP_SELF\'];?>&val=this.value" style="width:160px;">';
Where val
is the drop down value.
Upvotes: 1