Niall Harkiss
Niall Harkiss

Reputation: 3

PHP if else inside select form

Putting together the simplest of web forms with a select drop down, yet for some reason, this one relentlessly seems to select the ELSE each time the $teamtypeid is equal to 1. I am using a print to ensure $teamtypeid does actually equal 1 and it displays it correctly in that print statement on the live site, yet the select shows option 2 still shows as selected in the drop down each time.

I feel like this must be something incredibly obvious that I am missing, but can't spot it!

<label for="select01">Team: <?= print $teamtypeid ?></label>
<select class="form-control" id="select01" name="teamtype">
    <?php
    if($teamtypeid == 1)
        {
        echo"
        <option value=1 SELECTED>First Team 1</option>
        <option value=2>Reserves/Youth</option>
        ";
        }
    else
        {
        echo"
        <option value=1>First Team</option>
        <option value=2 SELECTED>Reserves/Youth</option>
        ";
        }
    ?>
</select>

Upvotes: 0

Views: 72

Answers (1)

Hendrik
Hendrik

Reputation: 821

I'm guessing that $teamtypeid isn't actually set.

<?= print $teamtypeid ?>
// <?= is same as <?php echo
// so its same as
<?php echo print $teamtypeid ?>
// print doesn't print anything because $teamtypeid isn't set
// and echo is echoing the return value (1) of print

Right now you are just echoing the return value of print here, which is always 1.

The major differences to echo are that print only accepts a single argument and always returns 1. https://www.php.net/manual/en/function.print.php

Upvotes: 1

Related Questions