dsy48837 2017-01-04 21:20
浏览 29
已采纳

PHP:isset修改HTML元素

This is my code:

<?php
  $printhename = $_POST['username3'];
  if(isset($_POST['username3']) && $printhename == "Carlos"){
    echo "<h1 class='title123'>Hello $printhename </h1>";    
  }
  else {
    echo "<h1 class='title123'>You don't belong here!</h1>";
  }
?>

What I want to do is, if "username3" is not set, the message "You don't belong here is shown". Otherwise, if the name is set and it's Carlos, display the other message. I must be doing this the wrong way because it is always printing "Hello $printhename", regardless of the name that has been input.

I'd like for someone to shed some light on this. I'm new to PHP.

If it helps, here's my HTML code:

<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
  <div class="centerlogin">
    <form class="formlogin" name="login" method="POST" action="testing.php">
      <input type="text" placeholder="username" NAME="username3">
      <input type="submit" name="login_submit" value="login">
    </form>  
  </div>
</div>
  • 写回答

4条回答 默认 最新

  • dso15221 2017-01-04 21:30
    关注

    There's a few things wrong here.

    Place the POST array that you first assigned for the variable and use the POST array for it to check if it's equal to "Carlos" inside the conditional statement.

    <?php
    //  $printhename = $_POST['username3']; // this throws an undefined variable
      if(isset($_POST['username3']) && $_POST['username3'] == "Carlos"){
    
    $printhename = $_POST['username3'];
        echo "<h1 class='title123'>Hello $printhename </h1>";    
      }
      else {
        echo "<h1 class='title123'>You don't belong here!</h1>";
      }
    
    ?>
    
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
      <div class="centerlogin">
        <form class="formlogin" name="login" method="POST" action="testing.php">
          <input type="text" placeholder="username" NAME="username3">
          <input type="submit" name="login_submit" value="login">
        </form>  
      </div>
    </div>
    

    Remember, "Carlos" and "carlos" are two different animals.

    On an added note; if you want to avoid showing the "You don't belong here!" message, you can check if the submit was set first; just in case your HTML and PHP are also in the same file.

    I.e.:

    <?php
    
      if(isset($_POST['login_submit']) ){
    
      if(!empty($_POST['username3']) && $_POST['username3'] == "Carlos"){
    
        $printhename = $_POST['username3'];
        echo "<h1 class='title123'>Hello $printhename </h1>";    
      }
      else {
        echo "<h1 class='title123'>You don't belong here!</h1>";
      }
    
    }
    
    ?>
    
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
      <div class="centerlogin">
        <form class="formlogin" name="login" method="POST" action="testing.php">
          <input type="text" placeholder="username" NAME="username3">
          <input type="submit" name="login_submit" value="login">
        </form>  
      </div>
    </div>
    

    Sidenote:

    Use !empty() instead of isset() for the text input, it's actually better for those and it checks for both; if it's set and not empty.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题