php - 如何通过 PHP 在 HTML 按钮上添加 CSS 类

标签 php html css

我有注册表,我需要隐藏提交按钮。在 PHP 中执行操作后,此按钮将可见。

我尝试使用 CSS 类并且它有效,但我不知道如何使用 PHP 添加类。

这是我要更改类的按钮:

<form method="post" action="register.php" name="registerform">
 <input type="submit" name="register" value="registrovat" id="button8" class=I don't know what /><br><br>
</form>

我的 CSS:

#button8{
    visibility: hidden;
    font-family: century gothic;
    color: #0099FF;
    border: 2px solid #0099FF;
    background: transparent;
}
#button8.visible{
    visibility: visible;}

我需要更改为 button8.visible 但在 PHP 中(我的条件取决于 MySQL 数据)。

编辑:

我需要它,因为该按钮将根据代码显示。用户输入代码,按下按钮。此操作将运行 PHP:连接到 mySQL 查找此代码是否在数据库中。如果是,我需要我的按钮可见。由于我的知识不足,我想在 mySQL 所在的同一个 PHP session 中使用 if 条件。这是完整的代码:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <title>Panákovač 4.0</title>
    <link rel="stylesheet" type="text/css" href="mystyle.css">
</head>


</head>
<body>



</body>
</html>

<?php
// show potential errors / feedback (from registration object)
if (isset($registration)) {
    if ($registration->errors) {
        foreach ($registration->errors as $error) {
            echo $error;
        }
    }
    if ($registration->messages) {
        foreach ($registration->messages as $message) {
            echo $message;
        }
    }
}

?>

<!-- register form -->
<form method="post" action="register.php" name="registerform">
    <input id="login_input_username" class="login_input" type="text" pattern="[a-zA-Z0-9]{2,64}" name="user_name" placeholder="uživatelské jméno" required /><br>
    <input id="login_input_password_new" class="login_input" type="password" name="user_password_new" pattern=".{6,}" placeholder="heslo (min. 6 znaků)" required autocomplete="off" /><br>
    <input id="login_input_password_repeat" class="login_input" type="password" name="user_password_repeat" pattern=".{6,}" placeholder="zopakujte heslo" required autocomplete="off" /><br>
    <input id="login_input_firstname" class="login_input" type="text" name="user_firstname" placeholder="jméno" required /><br>
    <input id="login_input_lastname" class="login_input" type="text" name="user_lastname" placeholder="příjmení" required /><br>
    <input id="login_input_email" class="login_input" type="email" name="user_email" placeholder="e-mail" required /><br>
    <input id="login_input_company" class="login_input" type="text" name="user_company" placeholder="firma" required /><br>
    <input type="submit" name="register" value="registrovat" id="button8" class= /><br><br>
</form>

<form method="post" action="register.php" name="registerform">
    <input id="login_input_code" class="login_input" type="text" name="user_code" placeholder="ověřovací kód" required /><br>
    <input type="submit"  name="kod" value="ověřit kód"/><br>
</form>

<?php
if(isset($_POST['kod'])){
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "login";


            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 
$user_code = $_POST['user_code'];
$sql = "SELECT `used`,`code` FROM `regcode` WHERE `code` LIKE '$user_code' ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

        echo "kód: " . $row["code"]. " - použito: " . $row["used"]. $zob . "<br>";
        $pouzito = $row["used"];
MY CONDION FOR CLASSES
    }
} else {
    echo "chybný kód" . $user_code;
}
            $conn->close();
    }  

?>  

最佳答案

您可以像这样在 html 内容中添加 php if 条件:

<form method="post" action="register.php" name="registerform">
  <input type="submit" name="register" value="registrovat" id="button8" 
     class="<?php if(condition) { echo 'classname'; } ?>" />
</form>

在这种情况下,如果条件为 true,它将 echo class 属性中的类,还有其他方法可以做到,但这是最直观的方法新的 PHP 开发人员。

就像@Jordi Nebot 告诉你的那样,你可以像这样做简短的版本:

<input class="<?= ($condition) ? 'classname' : ''"> ?>

关于php - 如何通过 PHP 在 HTML 按钮上添加 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43894290/

相关文章:

php - 拉维尔 5.8 : Trying to make a where model by using data pulled from database

html - polymer 元素 : paper-card image style under #shadow-root is not working

javascript - 桌面谷歌 chrome 浏览器禁用放大?

javascript - 如何替换 HTML 元素同时保持页面上所有其他元素的位置相同

php - 如何使用 CURL 将此类 PHP 代码翻译成 C/C++?

php - 如何在不重新加载页面的情况下更新 mysql 数据库

javascript - 捕获鼠标离开 body 元素越过顶部边界

html - 为什么 <?xml version ="1.0"encoding ="UTF-8"?> 不应该放在最上面?

除非 <a 有空格,否则 css 不显示图像

php - 使用 php 从 mySQL 数据库创建 xml 时出错