Two action forms and two submit buttons, how to keep the values?

617 Views Asked by At

I have 2 action forms in the page and 2 submit buttons.

1 is zoom-level

1 is refresh rate in seconds

When I hit submit of zoom, the refresh rate is reset When I hit submit of refresh rate, the zoom level is reset

What to do to keep the value of the other when one dropdown is set to a certain value?

I think this is a common problem where noobs struggle with, so it would be nice to have some good explanation from experts with nice guidance here...

code is:

<?php
        $submittedValue = "";
        $value0 = 1;
        $value1 = 1.2;
        $value2 = 1.5;
        $value3 = 2;
        if (isset($_POST["FruitList"])) {
            $submittedValue = $_POST["FruitList"];
        }
        ?>
        <form action="example3b2vandaag.php" name="fruits" method="post">
        <select project="FruitList" id="FruitList" name="FruitList">
         <option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo "off"; ?></option>
         <option value = "<?php echo $value1; ?>"<?php echo ($value1 == $submittedValue)?" SELECTED":""?>><?php echo $value1; ?></option>
         <option value = "<?php echo $value2; ?>"<?php echo ($value2 == $submittedValue)?" SELECTED":""?>><?php echo $value2; ?></option>
         <option value = "<?php echo $value3; ?>"<?php echo ($value3 == $submittedValue)?" SELECTED":""?>><?php echo $value3; ?></option>
        </select>
        <input type="submit" name="submit" id="submit" value="Set zoom level" />
        </form>






        <?php
        $submittedValue = "";
        $value0 = 1000;
        $value1 = 20000;
        $value2 = 30000;
        $value3 = 90000;
        if (isset($_POST["FruitList2"])) {
            $submittedValue = $_POST["FruitList2"];
        }
        ?>
         <form action="example3b2vandaag.php" name="fruits2" method="post">
        <select project="FruitList2" id="FruitList2" name="FruitList2">
         <option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo $value0; ?></option>
         <option value = "<?php echo $value1; ?>"<?php echo ($value1 == $submittedValue)?" SELECTED":""?>><?php echo $value1; ?></option>
         <option value = "<?php echo $value2; ?>"<?php echo ($value2 == $submittedValue)?" SELECTED":""?>><?php echo $value2; ?></option>
         <option value = "<?php echo $value3; ?>"<?php echo ($value3 == $submittedValue)?" SELECTED":""?>><?php echo $value3; ?></option>
        </select>
        <input type="submit" name="submit" id="submit" value="Set refresh milliseconds" />
        </form>
1

There are 1 best solutions below

0
On BEST ANSWER

I don't see any reason why you would want two forms, you can put the two select boxes into one form. And please don't use unintelligent variable names such as $value0, $submittedValue and $Fruitlist, because this isn't about fruits.

Here are the two select boxes into one form:

<?php

    $zoom = "";
    $zoom0 = 1;
    $zoom1 = 1.2;
    $zoom2 = 1.5;
    $zoom3 = 2;
    $refresh = "";
    $refresh0 = 1000;
    $refresh1 = 20000;
    $refresh2 = 30000;
    $refresh3 = 90000;

    if (isset($_POST["zoom"])) {
        $zoom = $_POST["zoom"];
    }
    if (isset($_POST["refresh"])) {
        $refresh = $_POST["refresh"];
    }
    ?>
    <form action="testForm.php" name="controlpanel" method="post">
    ZoomLevel:<br />
    <select project="ControlPanel" id="ControlPanel" name="zoom">
     <option value = "<?php echo $zoom0; ?>"<?php echo ($zoom0 == $zoom)?" SELECTED":""?>><?php echo "off"; ?></option>
     <option value = "<?php echo $zoom1; ?>"<?php echo ($zoom1 == $zoom)?" SELECTED":""?>><?php echo $zoom1; ?></option>
     <option value = "<?php echo $zoom2; ?>"<?php echo ($zoom2 == $zoom)?" SELECTED":""?>><?php echo $zoom2; ?></option>
     <option value = "<?php echo $zoom3; ?>"<?php echo ($zoom3 == $zoom)?" SELECTED":""?>><?php echo $zoom3; ?></option>
    </select>
    <br />
    Refresh rate:<br />
    <select project="ControlPanel" id="ControlPanel" name="refresh">
     <option value = "<?php echo $refresh0; ?>"<?php echo ($refresh0 == $refresh)?" SELECTED":""?>><?php echo $refresh0; ?></option>
     <option value = "<?php echo $refresh1; ?>"<?php echo ($refresh1 == $refresh)?" SELECTED":""?>><?php echo $refresh1; ?></option>
     <option value = "<?php echo $refresh2; ?>"<?php echo ($refresh2 == $refresh)?" SELECTED":""?>><?php echo $refresh2; ?></option>
     <option value = "<?php echo $refresh3; ?>"<?php echo ($refresh3 == $refresh)?" SELECTED":""?>><?php echo $refresh3; ?></option>
    </select><br />

    <input type="submit" name="submit" id="submit" value="Set configurations" />
    </form>

To use the data you get from the form you can use $_POST["zoom"] and $_POST["refresh"], because that's what the name properties of the select boxes are. See http://www.w3schools.com/php/php_form_complete.asp for more information on html forms and how to use the $_POST data.