开发者

How to create HTML button based on condition?

开发者 https://www.devze.com 2022-12-14 14:04 出处:网络
I am using following code: <?PHP if ($_Session[\'WorkMode\'] == \'New\' || $_Session[\'WorkMode\'] == \"\")

I am using following code:

<?PHP
      if ($_Session['WorkMode'] == 'New' || $_Session['WorkMode'] == "") 
    echo "<input id='Submit' name='Submit' value='Submit' type='button'>";
      else
    echo "<input id='Update' name='Update' valu开发者_StackOverflowe='Update' type='button'>";
?>

Is this the correct way to render buttons?


If it works, then yes it's one correct method. Another way, using a ternary if statement might be:

<?PHP $button = $_SESSION['WorkMode'] == 'New' 
     || $_SESSION['WorkMode'] == '' ? "Submit" : "Update"; ?>
<input id="<?php echo $button;?>" name="<?php echo $button;?>" 
     value="<?php echo $button;?>" type='button' />

Really it's a matter of personal preference and clarity. I prefer to write HTML as HTML (not as a PHP string) and echo variables into that HTML using PHP tags. Some might not like to use this method. If you have PHP short tags switched on you can even use <?=$button;?>


This is totally valid and readable.

If the number of lines of code is important to you, you could also do:

<?php $action = ($_Session['WorkMode'] == 'New' || $_Session['WorkMode'])? "Submit" : "Update" %>
<input id="<?php echo $action ?>" name="<?php echo $action ?>" value="<?php echo $action ?>" type="button" />

My PHP is a bit rusty so I might be off on the syntax, but you get the idea.

If you use a framework such as cakephp or symphony you can use their helpers to generate buttons more easily.


Basically, yes you can do it this way. Depending on the size of your application and on your programming experience you might want to consider to start using a templating system (e.g. Smarty). This way you could seperate php code and html markup.

Best wishes,
Fabian


It should be

<?PHP      
if ($_Session['WorkMode'] == 'New' || $_Session['WorkMode'] == "")     
      echo "<input id='Submit' name='Submit' value='Submit' type='button'/>";      
else    
      echo "<input id='Update' name='Update' value='Update' type='button'/>";?>


The better way of doing this is using a template system, ou simply separate the layout code from logic:

view.php:

<?php if ($new): ?>
  <input id='Submit' name='Submit' value='Submit' type='button'>
<?php else: ?>
  <input id='Update' name='Update' value='Update' type='button'>
<?php endif; ?>

So, $new variable is sended by the logical code (controller):

controller.php:

$new = ($_Session['WorkMode'] == 'New' || $_Session['WorkMode'] == "");
include("view.php")

This is a better way to doing what you want :). Don't forget that is a good practice to also support internacionalization on view.


Depending on the behaviour you want from your buttons, you might consider changing their type to submit.

EDIT

It seems from the responses that your code is functional. However, you say that the PHP code is coming out behind the buttons.

This implies to me that the file is not being parsed by the PHP parser.

RPK: Is your web server serving other PHP files correctly?

0

精彩评论

暂无评论...
验证码 换一张
取 消