简体   繁体   中英

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 ?

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.

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>

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";

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM