Sumerechny
Sumerechny

Reputation: 168

PHP - remember input selection option

I'm trying to translate a Java 'calculator' I wrote to PHP/HMTL. To get this to function the initial selection has to be remebered. I know I need to use $_POST['subject'] to get $cc this however causes a problem for me.

I tried several versions but I fear I lack some basics. Is anyone able to push me in the right direction?

Code without remember option:

<form action="" method="post">

    <legend>Make a selection below</legend>
    <br>
    Field of interest:<br>
    <select name="subject">
        <?php
        $subjectarray = array("Please select a subject", "Wavelenght", "Radioactive Decay");

        foreach ($subjectarray as $cc => $subject) {

            echo '<option value="' . $cc . '">' . $subject . '</option>';
        }
        ?>
    </select>

Code with a version of the remember option (clearly not working);

    foreach ($subjectarray as $cc => $subject) {

                echo '<option value="' . $cc . '"
if ($_POST['subject'] == $cc) echo 'selected="selected"';>' . $subject . '</option>';

            }

Another try

foreach ($subjectarray as $cc => $subject) {

            echo "<option value=\"{$cc}\"";
            echo ($_GET['subject'] == $cc) ? 'selected="selected"' :"";
            echo ">" . $subject . "</option>";
        }

Upvotes: 0

Views: 96

Answers (3)

Mohit Kumar
Mohit Kumar

Reputation: 940

Note:- you have to use $_POST['subject'] because method='POST' in form Tag.

You need to foreach through all the options.

Make an array with all the dropdown options, loop through that, and compare with what is stored in $_POST[] & just put the selected attribute on the one which you want to select.

<form action="" method="post">

    <legend>Make a selection below</legend>
    <br>Field of interest:<br>
    <select name="subject">
        <?php
        $subjectarray = array("Please select a subject", "Wavelenght", "Radioactive Decay");

        foreach ($subjectarray as $cc => $subject) {

            if ($_POST['subject'] == $cc) {
                $selected = 'selected';
                } else {
                $selected = '';
                }
   echo '<option value="' . $cc . '" '.$selected.'>' . $subject . '</option>';
        }
        ?>
    </select>
</form>

Upvotes: 0

man0v
man0v

Reputation: 674

Second option is using $_GET and you are using POST in the form. It'll never work.

The first one looks like it'll throw an error... Not a valid syntax in PHP. In PHP you can't have if inside an echo or print.

What I like to do is(rewriting your foreach):

    foreach ($subjectarray as $cc => $subject) {

        $selected = $_POST['subject'] == $cc ? ' selected="selected"' : '';

        echo '<option value="' . $cc . '"'. $selected .'>' . $subject . '</option>';
    }

Upvotes: 0

Mitya
Mitya

Reputation: 34556

How about:

<select name="subject">
    <?php
    foreach(["Please select a subject", "Wavelenght", "Radioactive Decay"] as $i => $val) {
    $sel = empty($_POST['subject']) || $_POST['subject'] != $i ? null : 'selected';
    ?>
    <option <?= $sel ?> value="<?= $i ?>"><?= $val ?></option>
    <?php } ?>
</select>

Upvotes: 1

Related Questions