0

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?

6
  • What do you mean by "correct way"? What is the code for? What is "workMode"? Commented Dec 15, 2009 at 11:54
  • yes it should work. But you've missed the closing tags for the <input> tags. Commented Dec 15, 2009 at 11:54
  • It's not correctly working. Behind the button, it is showing the php code also. Commented Dec 15, 2009 at 12:08
  • 1
    @Matt Ellen: exactly how will that help? Commented Dec 15, 2009 at 12:21
  • isn't the open tag case sensitive? Commented Dec 15, 2009 at 12:32

6 Answers 6

2

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

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

1 Comment

true, 10 seconds differences for the basic same answer. +1 to you because obviously I agree with this ^^
2

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.

3 Comments

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!
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 (?>)
my bad, %> is only because that's the way you'd do it with rails. I'm fixing it right now
1

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

Comments

0

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'/>";?>

Comments

0

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.

Comments

0

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?

Comments

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.