John
John

Reputation: 1594

Update multiple php variables using multiple select drop downs on the same page

I have two php variables which are initialized with the default values shown below:

<?php $SelectedColor = ' no color selected'; $SelectedNumber = ' no number selected'; ?>

I also have two php forms each containing a select drop down. Each form also echoes the respective php variable's value above the select.

<?php

echo '<form action="" method="post">';
echo '<label>Selected Color: </label>';
echo $SelectedColor ;
echo '<br/>';
echo '<select name="colorSelect">';
echo '<option value="red">red</option>';
echo '<option value="blue">blue</option>';
echo '<option value="yellow">yellow</option>';
echo '</select>';
echo '</form>';
?>
<br/>

<?php

echo '<form action="" method="post">';
echo '<label>Selected Number: </label>';
echo $SelectedNumber ;
echo '<br/>';
echo '<select name="numberSelect" >';
echo '<option value="1">One</option>';
echo '<option value="2">Two</option>';
echo '<option value="3">Three</option>';
echo '</select>';
echo '</form>';
?>

As this code is now the value of the php variables never changes they simply print their default values regardless of what the selection of the select menus are.

Without using AJAX, Is there a way to upon changing either drop down select submit the respective form reloading the page changing the value of the php variable to what was selected and keeping that selection on reload?

Upvotes: 0

Views: 901

Answers (1)

FuzzyTree
FuzzyTree

Reputation: 32392

If I'm understanding you correctly you're trying to retain your selections after the page is posted? If so try this. Not tested but it should give you most of what you need. Updated to post to self and submit on drop down change.

<?php    
    //read in selected value
    $SelectedColor = $_POST['colorSelect'] ? $_POST['colorSelect'] : ' no color selected';
    $SelectedNumber = $_POST['numberSelect'] ? $_POST['numberSelect'] : ' no number selected';

    echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">'; 
    echo '<label>Selected Color: </label>';
    echo $SelectedColor ;
    echo '<br/>';
    echo '<select onchange="this.form.submit()" name="colorSelect">';

    $color_options = array('red','blue','yellow');

    foreach($color_options as $option) {
    //retain selected value
    $selected = ($option == $SelectedColor) ? 'SELECTED' : ''; 
    echo "<option $selected value='$option'>$option</option>";
    }

    echo '</select>';
    echo '</form>';    
?>

<br/>

<?php

    $number_options = array(1 => 'one', 2 => 'two', 3 => 'three');

    echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">'; 
    echo '<label>Selected Number: </label>';
    //if a number was posted get the corresponding string
    echo is_numeric($SelectedNumber) ? $number_options[$SelectedNumber] : $SelectedNumber;
    echo '<br/>';
    echo '<select onchange="this.form.submit()" name="numberSelect" >';    

    foreach($number_options as $option => $value) {
    //retain selected value
    $selected = ($value == $SelectedNumber) ? 'SELECTED' : ''; 
    echo "<option $selected value='$value'>$option</option>";
    }

    echo '</select>';
    echo '</form>';
?>

Upvotes: 1

Related Questions