【问题标题】:How to use a variable (boolean) in PHP and HTMLHow to use a variable (boolean) in PHP and HTML
【发布时间】:2022-12-02 00:36:04
【问题描述】:

This code is supposed to reveal three buttons: modify items, add items, and view accounts, which should only be shown once proper credentials for username and password (hardcoded username = frankbutt and password=franbuttpower123) are shown.

Code:

<?php
    $show = False;
    if(strcmp($_POST["username"], "FrankButt") ==0){
        if(strcmp($_POST["password"], "frantbuttpower123") ==0){
            $show = True;
        }
    }
?>
<html>
<link rel="stylesheet" href="../style/admin.css">
<link rel="stylesheet" href="../style/form.css">


    <h1 class="adminHeader">Admin Access</h1>

    <br>
    <br>
    <br>
    <form action="adminLogin.php" method="post" class="formBox">
            <input type="text" name="username" class="loginFill" placeholder="Username"><br>
            <input type="password" name="password" class="loginFill" placeholder="Password"><br>
            <button type="submit" class="adminsubmitsignin">Sign In</button>
    </form>

        if(<?php echo $show; ?>){
            <button class= "adminOptions" onClick="selectItem.php">Modify Items</button>
            <br>
            <button class="adminOptions" onClick="adminImageUpload.php">Add Items</button>
            <br>
            <button class="adminOptions" onClick="adminHome.php">View Accounts</button>
            <br>

        }
        
    
</html>

I tried to approach the problem by making a boolean that becomes true if the username and password entries are equal to the hardcoded values given. I then wanted to use that boolean for the html code to behave a certain way

【问题讨论】:

  • Your if is inside the HTML, which doesn't know anything about if statements. Move the PHP tags to surround the full if statement and opening brace, and add another set to surround the closing brace

标签: php html button boolean


【解决方案1】:

you can use short form of if in html

<form action="adminLogin.php" method="post" class="formBox">
        <input type="text" name="username" class="loginFill" placeholder="Username"><br>
        <input type="password" name="password" class="loginFill" placeholder="Password"><br>
        <button type="submit" class="adminsubmitsignin">Sign In</button>
</form>

    <?php if ($show): ?>
        <button class= "adminOptions" onClick="selectItem.php">Modify Items</button>
        <br>
        <button class="adminOptions" onClick="adminImageUpload.php">Add Items</button>
        <br>
        <button class="adminOptions" onClick="adminHome.php">View Accounts</button>
        <br>


    <?php endif;?>

【讨论】:

    猜你喜欢
    • 2022-12-28
    • 2022-12-02
    • 2022-12-27
    • 2022-12-27
    • 2011-09-22
    • 2022-12-27
    • 2022-12-02
    • 1970-01-01
    • 2022-12-27
    相关资源
    最近更新 更多