Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP setting input values with isset

Tags:

php

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.

like image 591
Hanpan Avatar asked Jan 10 '10 23:01

Hanpan


1 Answers

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

like image 184
jspcal Avatar answered Oct 26 '22 09:10

jspcal