Vinod VT
Vinod VT

Reputation: 7149

How to assign a selectbox option value to a php variable?

I have two select boxes like,

<select name="template" id="template">
        <option value="" selected>--Select--</option>
            <option value="HOME">Home</option>
            <option value="HELP">How We Help</option>
            <option value="CONTACT">Contact Us</option>
            <option value="ABOUT">About Us</option>
</select>

And

<select name="location" id="location"">
    <option value="" selected>--Select--</option>
            <option value="header">Header</option>
            <option value="center">Center</option>
            <option value="footer">Footer</option>
    </select>

The problem is that, in second select box the center location is needed only for Contact Us template. How to hide the center option for all other template ?

Upvotes: 1

Views: 4793

Answers (4)

Ali Seyfollahi
Ali Seyfollahi

Reputation: 2872

Try this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script>
    $(document).ready(function(){
        $('#template').change(function(){
            var template = $(this).val()
                ,option = $('#container > select > option');
            if(template=="CONTACT")
                $('#location > option[value="header"]').after(option.clone());
            else
                $('#location > option[value="center"]').remove();
        }).change();
    });
</script>
<select name="template" id="template">
    <option value="" selected>--Select--</option>
    <option value="HOME">Home</option>
    <option value="HELP">How We Help</option>
    <option value="CONTACT">Contact Us</option>
    <option value="ABOUT">About Us</option>
</select>
<select name="location" id="location">
    <option value="" selected>--Select--</option>
    <option value="header">Header</option>
    <option value="footer">Footer</option>
</select>
<div id="container" style="display: none">
    <select>
        <option value="center">Center</option>
    </select>
</div>

Upvotes: 1

mn.
mn.

Reputation: 846

1) hiding a center options: jsfiddle here

2) setting a options value in php:

  • replace <option value="header"></option> with <option value="<?php include('myScript.php') ?>"></option>
  • then in myScript.php, echo "newValue";

Upvotes: 1

fullybaked
fullybaked

Reputation: 4127

Without knowing the rest of your code, but assuming its just simple PHP (no frameworks)

<select name="location" id="location"">
<option value="" selected>--Select--</option>
        <option value="header">Header</option>
        <?php if ($template == 'contact_us'):?>
        <option value="center">Center</option>
        <?php endif;?>
        <option value="footer">Footer</option>
</select>

Wrapping the center option in a simple if block that tests for the current template type, will allow you to show or hide the value based on which page is loaded.

There are possibly better ways to do this if you are in a framework, but what I've suggested will work if you are just using plain PHP.

Upvotes: 4

Jhonathan H.
Jhonathan H.

Reputation: 2713

Just replace the static value of your <option> into the desired variable

value="<?php echo $variable;?>"

While in hiding it you must set a condition sample for contact page you set a variable $page='Contact_Us' means that page is the contact_us.Then create a condition inside your option that will make the option center only show when for the $page='Contact_Us' only

//Define variable `$page` in contact_us page
$page='Contact_Us';

<select name="location" id="location"">
    <option value="" selected>--Select--</option>
            <option value="header">Header</option>
            <?php if($page == 'Contact_US'){?>
            <option value="center">Center</option> 
            <?php }?>
            <option value="footer">Footer</option>
    </select>

Upvotes: 1

Related Questions