tags:

views:

58

answers:

3

Hi,

I was wondering if there is a shorter way of writing the following code:

<input type="text" name="username" value="<?if(isset($_POST['username'])){ echo $_POST['username']; }?>" />

I hate having to do this will all my forms as the isset() check really messes up my HTML and scares away the frontenders.

+2  A: 

You can assign the values in php part and then just echo in html

$username = isset($_POST['username'])?$_POST['username']:'';

<input type="text" name="username" value="<?php echo $username;?>" />
marvin
+3  A: 

you can make a helper:

function req($key, $default = '')
{
  return isset($_REQUEST[$key]) ? $_REQUEST[$key] : $default;
}

<input name="user" value="<?php echo htmlentities(req('user')) ?>" />

@marvin's suggestion is nice for your script as well

regarding the front-end folks, i would say give them some basic php to use, like in this php for designers tutorial: http://www.digital-web.com/articles/php_for_designers/

learning the basic scrips i think is better than using an external templating system

jspcal
You could also put the htmlentities into the req function, perhaps as an option, to simplify things further.
Alex JL
A: 

Why not just

<input type="text" name="username" value="<? echo $_POST['username']; ?>" />

if $_POST['username'] is empty then it will cause a value of "" anyway.

I feel like I am missing something.

Mark