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

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>

图片转代码服务由CSDN问答提供 功能建议

这是我的代码:

 &lt;?php \  n $ printhename = $ _POST ['username3']; 
 if(isset($ _ POST ['username3'])&amp;&amp; $ printhename ==“Carlos”){
 echo“&lt; h1 class ='title123  '&gt; Hello $ printhename&lt; / h1&gt;“;  
} 
其他{
 echo“&lt; h1 class ='title123'&gt;你不属于这里!&lt; / h1&gt;”; 
} 
?&gt; 
  <  / pre> 
 
 

我想要做的是,如果未设置“username3”,则会显示“您不属于此处”的消息。 否则,如果设置了名称并且它是Carlos,则显示另一条消息。 我必须以错误的方式执行此操作,因为它始终打印“Hello $ printhename”,无论输入的名称是什么。

我希望有人能够解释一下 这个。 我是PHP的新手。

如果有帮助,这是我的HTML代码:

 &lt; div class =“col-xs  -12 col-sm-12 col-md-12 col-lg-12“&gt; 
&lt; div class =”centerlogin“&gt; 
&lt; form class =”formlogin“name =”login“method =”  POST“action =”testing.php“&gt; 
&lt; input type =”text“placeholder =”username“NAME =”username3“&gt; 
&lt; input type =”submit“name =”login_submit“value =  “login”&gt; 
&lt; / form&gt;  
&lt; / div&gt; 
&lt; / div&gt; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论
  • dsa80833 2017-01-04 21:29

    So $_POST['username3'] will always be set because it's a field in your HTML form and will always be submitted by it. However, if the visitor did not fill the form field with a value, $_POST['username3'] will be empty.

    So you probably want to test with empty($_POST['username3']) instead of isset($_POST['username3'])...

    And since you are compare "not empty AND equals to 'Carlos'" you can totally drop the "not empty" condition. (If it's equal to Carlos, it's not empty!)

    To recap, with the comment from Fred -ii-:

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

    Hope this helps!

    打赏 评论
  • dongshuo6185 2017-01-04 21:32

    Change your PHP to this:

    <?php
    
    if(isset($_POST['username3']))
    {
    $printhename = $_POST['username3'];
    }
    else
    {
    /* If it is not set, redirect client */
    header('Location: /login.php');
    }
    
      if($printhename == "Carlos"){
        echo "<h1 class='title123'>Hello $printhename </h1>";    
      }
      else {
        echo "<h1 class='title123'>You don't belong here!</h1>";
      }
    ?>
    
    打赏 评论
  • dporb84480 2017-01-04 21:39

    Thank you so much for your replies — they really helped me a lot and I couldn't be more thankful.

    I don't know why I changed the method to GET, it was POST before! To achieve the look I wanted, I had to work the code like this.

    Inbetween the "head" tags:

    <?php
      $printhename = $_POST['username3'];
    ?>
    

    The body:

    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
      <div class="centerlogin">
      <?php
    
        if($printhename == "Carlos"){
          echo "<h1 class='title123'>Hello $printhename </h1>";    
        }
        if (isset($printhename) && $printhename != "Carlos") {
          echo "<h1 class='title123'>You don't belong here!</h1>";
        }
    
        if(empty($printhename)) {
          echo "<h1 class='title'>Log in, please</h1>";
        }
    
      ?>
        <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>
    

    Is there any optimizations I could do?

    打赏 评论

相关推荐 更多相似问题