6

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 Answers 2

5

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

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

1 Comment

You could also put the htmlentities into the req function, perhaps as an option, to simplify things further.
3

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

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.