PHP Shorthand for isset form POST value

25,383

Solution 1

You can define variables in beginning of your script before HTML output, for example:

$name = isset($_POST['submit']) ? $_POST['name'] : null;

in your html section you can print $name without worrying it was not defined

<p><input type="text" name="name" value="<?php echo $name ?>" /></p>

Also if $_POST['submit'] does not contain any value you are more likely to receive FALSE statement. To avoid such issues use array_key_exists

Solution 2

Like Nazariy said, you should avoid as much PHP in the template as possible.
In fact, you should have in your template already prepared variables only.

So, in your code have something like this

$FORM = array();
$form_fields = array('name','sex');
foreach($form_fields as $fname) {
  if (isset($_POST[$fname])) {
    $FORM[$fname] = htmlspecialchars($_POST[$fname]);
  } else {
    $FORM[$fname] ='';
  }
}

and then you have smooth and neat template:

<p><input type="text" name="name" value="<?=$FORM['name']?>" /></p>

Solution 3

Without going into the reasons not to use <p> to structure your forms, there's not much that can be done besides removing the else.

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

Solution 4

Shorthand <?=$_POST['name'] ?: ''?>

Share:
25,383

Related videos on Youtube

Drew
Author by

Drew

My name is Drew and I am a front end web developer always looking to learn more!

Updated on April 26, 2020

Comments

  • Drew
    Drew almost 4 years

    I am creating a form and am just looking for more efficient ways to do things. What I have so far is:

    <p><input type="text" name="transmission" value="" /></p>
    <p><input type="text" name="model" value="<?=$model;?>" /></p>
    

    So some of them will have a value already set, and some will be blank. What I want to do is see if the form has been set, and if it has then use $_POST['name'] as the value, if not then use either blank or use the previous variable I have.

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

    But there has to be a shorter way to do that.

    IF anyone could point me in the direction I would really appreciate it.

    Thank you!

  • animuson
    animuson over 12 years
    If the form hasn't been submitted yet, that will throw a notice.
  • Nazariy
    Nazariy over 12 years
    I agree with you, building own form generator it is path that every developer should come through, for those who don't want to invent his own wheel, I would recommend to use something from Zend Framework arsenal.
  • Your Common Sense
    Your Common Sense over 12 years
    but that short open tag deprecation nonsense.
  • Decent Dabbler
    Decent Dabbler over 12 years
    +1 for answer in general and using htmlspecialchars() as well.
  • lsl
    lsl over 12 years
    Whats the point in this? Seems longer to me. You don't even specify charset, so protecting against XSS can't be the reason.
  • Drew
    Drew over 12 years
    I checked out HTML Purifier but it doesn't support the HTML 5 doctype yet :(
  • lsl
    lsl over 12 years
    I would take the whitelist approach for the time being. If you're taking a name, it should really only have a few characters present anything else should be discarded completely. a-z, A-Z, -, and ' should be the only things a name needs in 99.99% of cases.
  • Eduard Luca
    Eduard Luca over 9 years
    This will still throw a notice, because it's not the same as isset. It checks whether $_POST['name'] is empty or not, but if the key isn't there, it will throw a notice.
  • Christian
    Christian over 9 years
    Not if you set error_reporting(E_ALL & ~E_NOTICE) Otherwise, as you say, you will get something like [8] Undefined variable: name. But I always report all errors and deal with them accordingly :)
  • Eduard Luca
    Eduard Luca over 9 years
    Setting error_reporting to hide your notices under the rug is never the solution. Like a saying goes: "Always code as if the person that will code after you is a serial killer who knows where you live". Ie. don't hide errors, just prevent them.
  • frumbert
    frumbert over 2 years
    If you don't want to see the notice you can pretty safely ignore it in this way: $id = @$_POST['id'] ?: '';

Related