PHP sets input values ​​with isset

I was wondering if there is a shorter way to write the following code:

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

I am very sorry that these are all my forms, since isset () validation really messed up my HTML and frightens the frontmen off.

+6
php
source share
3 answers

you can do 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 front-end users, I would say give them the basic php to use, as in this php for designers: http://www.digital-web.com/articles/php_for_designers/

Learning the main scenarios that, in my opinion, are better than using an external template system

+5
source share

You can assign 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;?>" /> 
+3
source share

Why not just

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

if $ _POST ['username'] is empty, in any case this will result in a value of "".

I feel something is missing.

+1
source share

All Articles