dtbl1231 2017-12-25 22:15
浏览 32

是否允许回显NULL值是一种好的做法?

So I'm trying to save input values from a form in case the page reloads and I'm setting the initial value at the start of the script like like so:

$username = "";
$password = "";
$password_confirmation = "";
$email = "";
$email_confirmation = ""; 
$business_name = "";

Then if the form has been submitted and the server request method has been set to "POST" I set every individual variable to its corresponding value like so:

$username= filter_input(INPUT_POST, "username", FILTER_SANITIZE_STRING);
etc...

Then on each input element I'm echoing the corresponding value:

<input type="text" id="username" name="username" placeholder="Username" value="<?php echo $username; ?>">

So my question is, if for some reason filter_input fails and returns a NULL value and if I echo that NULL value will I have any trouble?

  • 写回答

1条回答 默认 最新

  • douche1936 2017-12-26 04:08
    关注

    You should set a ternary method to get the value of the page on reload.

    Sample:

    your code

    View index page

    <form action="index.php" method="post">
           <input type="text" id="username" name="username" placeholder="Username" value="<?php echo $username; ?>">
    <input type="submit" value="submit">
    </form>
    

    If you want to reload the value of the textbox after you submit you dont need to place a null value, instead check if the textbox has been posted or not. You can do this,

    <form action="index.php" method="post">
           <input type="text" id="username" name="username" placeholder="Username" value="<?php isset($_POST["username"]) ? echo $username : echo ""; ?>">
    <input type="submit" value="submit">
    </form>
    

    it means if the username has been posted, it already exist so it is set, therefore you will re take the value, else it will just display an empty box.

    Hope this help for cleaner and more efficient coding.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决