-3

I want to use selected option with condition. if $type is room then bed will be selected and if it is bathroom then toilet will be selected. what is wrong with this code. how to correct it????

<html>
  <body>
    <?php
      $type="bathroom";
    ?>
    <form method="post" action="<?php $_PHP_SELF ?>">
      <select name="bedtype">
      <% if($type=="bathroom"){ %>
       <option value="room">BED</option>
       <option value="bathroom" selected>TOILET</option>
      <% } else { %>
       <option value="room" selected>BED</option>
       <option value="bathroom" >TOILET</option>
      <% } %>
      </select>
    </form>
  </body>
</html>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Akash Nil
  • 693
  • 10
  • 26
  • Like that, but remember to turn on [error handling](http://www.sitepoint.com/error-handling-in-php/) - Your code looks correct assuming your are serving this from a web server and it is [rendered as PHP](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page) – mplungjan Mar 12 '16 at 07:52
  • output shows all the four options. condition is not working – Akash Nil Mar 12 '16 at 07:59
  • 2
    View source. I am guessing you will see `<% if($type=="bathroom"){ %> <% } else { %> <% } %>` - e.g. all the PHP code – mplungjan Mar 12 '16 at 08:01

2 Answers2

1

You are mixing ASP and PHP tags.

While ASP tags were allowed in PHP 5, as of PHP 7, they have been deprecated and removed. You should switch to strictly <?php ?> and <?= ?>.

http://php.net/manual/en/language.basic-syntax.phptags.php

You also have $_SERVER['PHP_SELF'] wrong.

Ternary conditionals would simplify the code greatly. They are in the form if true statement ? do truth : do falsehood. Providing an echo statement before the ternary conditional would echo out the true or false result.

<html>
<body>
  <?php $type = "bathroom"; ?>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="bedtype">
      <option value="room" <?php echo $type == "room" ? 'selected' : ''; ?>>BED</option>
      <option value="bathroom" <?php echo $type == "bathroom" ? 'selected' : '' ?>>TOILET</option>
    </select>
  </form>
</body>
</html>
SacWebDeveloper
  • 2,563
  • 2
  • 18
  • 15
1

Your code is correct in PHP < 7.0.

You use optional ASP tags ( <% (...) %> ) to embed PHP code.

To activate it, you have to modify your php.ini file turning this line:

asp_tags = Off

in:

asp_tags = On

Please note that ASP tags are rarely used and completely removed on PHP 7.0.

To avoid future incompatibility issue, use standard php tags <?php (...) ?>

As alternative, to echo something, you can use short tags <?= (...) ?>.

So, this:

<?php echo 'Hello World'; ?>

is the same as:

<?= 'Hello World'; ?>

or (you can omit closing semicolon):

<?= 'Hello World' ?>

For the records, there is another short-tag style ( <? (...) ?> ) that must be enabled in php.ini. However, the use of this short-tag is discouraged and substantially abandoned (this style is primary used to identify XML).

fusion3k
  • 11,568
  • 4
  • 25
  • 47