0

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>

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Very good catch! Yes, this sounds exactly like what the problem is. To the OP, I'd encourage you to always use <?php for every opening tag, even if it is convenient to sometimes use the short or echo versions, for this exact reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.