tags:

views:

82

answers:

6

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' value='Update' type='button'>";
?>

Is this the correct way to render buttons?

+1  A: 

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.

marcgg
lol you beat me with almost the same example by 10 seconds, which I probably spent formatting mine so that the code block didn't need scrolling!
Andy E
ps: I'd +1 for it, except asp-style tags (`%>`) are a PHP setting that needs to be switched on in php.ini. Best to suggest the standard closing tags (`?>`)
Andy E
my bad, %> is only because that's the way you'd do it with rails. I'm fixing it right now
marcgg
+1 then :-) `comment padding...`
Andy E
+1  A: 

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

halfdan
+2  A: 

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;?>

Andy E
true, 10 seconds differences for the basic same answer.+1 to you because obviously I agree with this ^^
marcgg
A: 

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'/>";?>
solairaja
It is displaying php code behind the button.
RPK
A: 

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?

Matt Ellen
A: 

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.

Filipe Costa