2017-01-04 57 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