Search code examples
phphtmljoomla3.1

Altering PHP Variable Based Off Selection


I have a html select that holds values for all four of our offices, or the text All. If the user selects All, I want to echo "All Offices", but if the user selects a specific office, I want to echo that number. My problem is that when I run the syntax below, All remains All instead of All Offices.

Did I set this up the incorrect way?

Display Data For Which Office:
<select name="office">
    <option value="All">All...</option>
    <option value="one">One Quarter</option>
    <option value="two">Two Quarter</option>
    <option value="three">Three Quarter</option>
    <option value="four">Four Quarter</option>
</select>


<?php
    if ($officename != 'All') {
        $officename = $_POST['officename'];
    } else {
        $officename = "All Offices";
    }

    echo $officename;
?>

Solution

  • You never initialized the $officename variable, so it should be null. As a result, won't $officename != 'All' always be true, so $officename = $_POST['officename']; will always be executed?

    I think what you want instead is something like:

    $officename = $_POST['officename'];
    
    if ($officename == 'All') {
        $officename = "All Offices";
    }
    
    echo $officename;