Jordan
Jordan

Reputation: 9114

Simple PHP inside HTML SELECT not working

I'm trying to execute the following code:

<label for="gender">Gender*: </label>
<select name="user[test]" id="gender">
<? echo "THISISATEST"?>
<option value="-1"> - Select - </option>
</select>

And it doesn't do anything.

However, this works fine:

<select name="user[test]" id="gender">
<option value="-1"> - Select - </option>
</select>
<? echo "THISISATEST"?>

What's going on?

Thanks!

Edit: I'm an idiot, I tried to simplify the problem for posting on here, turns out the echoing is appearing in the soruce and the line where I try to dynamically create the just isn't working. Derp. I forgot to surround my shorthand if?else: with brackets, which caused the whole line not to echo.

Thanks

Upvotes: 0

Views: 916

Answers (4)

ravi404
ravi404

Reputation: 7309

<select name="user[test]" id="gender">
<option value="<?php
 echo "THISISATEST";
 ?>"><?php
 echo "THISISATEST";
 ?></option>  // This will work
<option value="1"> - Select - </option>

This should work fine. You can enter your desired value as well

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

Simple. Your browser ignores text nodes under a select node. This arrangement is not in the HTML spec, so browsers are free to mutilate it however they like.

Upvotes: 1

Wasim Karani
Wasim Karani

Reputation: 8886

<select name="user[test]" id="gender">
    <? echo "THISISATEST"?> /// You can't echo string inside <select></select>
    <option value="-1"> - Select - </option>
</select>

Rather do

<select name="user[test]" id="gender">
    <option value="0"><? echo "THISISATEST"?></option>  // This will work
    <option value="1"> - Select - </option>
</select>

Upvotes: 2

donatJ
donatJ

Reputation: 3364

Are you entirely certain short tags are enabled on your server? I'd just double check <?php echo "THISISATEST"; ?> and see if that improves your luck. Also, did you view source - because that value may have been hidden on a rendered page when in a select as such.

Upvotes: 1

Related Questions