2017-01-04 96 views
1

这是我的代码:PHP:isset修改HTML元素

<?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>"; 
    } 
?> 

我想要做的是,如果“USERNAME3”没有设置,消息“你不属于这里显示”。否则,如果名称已设置并且是卡洛斯,则显示其他消息。无论输入的名称如何,我都必须以错误的方式执行此操作,因为它总是打印“Hello $ printhename”。

我希望有人对此有所了解。我是PHP新手。

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

<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> 
+1

'$ printhename = $ _POST ['username3'];'在错误的位置。 '&& $ printhename'使用POST数组。 –

+0

您的第一个变量也未定义,并使用POST数组检查是否等于“Carlos”。 –

+0

顺便说一句,你删除了你的评论我回复;-)只是说。你问我在哪里放,但正在忙着写我的答案。保持你接受的答案,我完全赞成。 –

回答

2

这里有一些错误。

放置您首先为变量分配的POST数组,并使用POST数组检查它是否等于条件语句中的“Carlos”。

<?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> 

请记住,“卡洛斯”和“卡洛斯”是两种不同的动物。

在附加说明中;如果你想避免显示“你不属于这里!”消息,您可以检查提交是否先设置;以防万一你的HTML和PHP也在同一个文件中。

即:

<?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> 

旁注:

使用!empty()而不是isset()用于文本输入,这对于那些真正更好,它会检查两者;如果它被设置而不是空的。

2

所以$ _ POST [“USERNAME3”]永远是设置因为它在你的HTML表单字段,将永远被它提交。但是,如果访问者未填充表单字段的值,则$ _POST ['username3']将为空。

所以,你可能要空($ _ POST [ 'USERNAME3']),而不是isset($ _ POST [ 'USERNAME3'])...

而且因为你是比较“不为空,等于测试到“卡洛斯”,你可以完全放弃“不空”的条件。 (如果它等于卡洛斯,这不是空!)

回顾一下,从弗雷德-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>"; 
    } 
?> 

希望这有助于!

+0

当我看到OP的评论询问我该把它放在哪里时,我实际上正在忙着写下我的问题,但他删除了他的评论* lol * - 但是,嘿..他选择了这个我很开心的,*欢呼声和感谢提及。 –

+0

对不起,OP看到并选择了我的答案。我实际上在评论中问他们是否重新接受你的意见。编辑:现在再次接受大声笑 –

+0

无后顾之忧 - 谢谢! – MacPrawn

0

你的PHP改成这样:

<?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>"; 
    } 
?> 
0

非常感谢你的答复 - 他们真的帮了我很多,我不能更感谢。

我不知道为什么我改变了GET的方法,这是POST之前! 为了达到我想要的样子,我必须像这样工作代码。

在它们之间隔了 “头” 标签:

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

身体:

<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> 

是否有任何的优化,我可以做什么?

+0

'$ foo = $ _POST ['foo'];'如果foo未与表单一起传递,将发出通知。所以通常你在分配之前使用isset。 '$ foo = isset($ _ POST ['foo'])? $ _POST ['foo']:null;' – Progrock