单击输入时,如何使用PHP更改HTML元素的类标签

时间:2019-03-29 05:31:00

标签: php html

首先,我不能为此使用JavaScript。作为本作业的一部分,我们只能使用PHP。

我当前的代码侦听input type="submit"帖子,然后检查某些文本输入是否为空或其中是否包含某些内容。目前,它只会回显一些有效的文本。

我想要的是根据输入文本字段上的class标记来更改它们是否已满。

以下是我当前的代码:

<?php
        $statusCodeClass='class="form-control"';
        $statusTextClass='class="form-control"';

        // Are all fields fill
        if(isset($_POST['post'])){
            checkStatusCodeIsEmpty();
            checkStatusTextIsEmpty();

            //Check if the fields are empty
            if(checkStatusCodeIsEmpty() && checkStatusTextIsEmpty()){
                echo "move to the next page";
            }else{
                echo "not correct yet";
            }           
        }

        function checkStatusCodeIsEmpty(){
            if(empty($_POST['statusCode'])){
                $statusCodeClass='class="form-control is-invalid"';
                return false;
            }else{
                $statusCodeClass='class="form-control is-valid"';
                return true;
            }
        }
        function checkStatusTextIsEmpty(){
            if(empty($_POST['statusText'])){
                $statusTextClass='class="form-control is-invalid"';
                return false;
            }else{
                $statusTextClass='class="form-control is-valid"';
                return true;
            }
        }
    ?>

    <!-- Whole page -->
    <div class="container-fluid px-lg-5">
        <h1>Status Posting System</h1>
        <form method="post">
            <!-- Status code group. This has the label and input -->
            <div class="form-group row">
                <label for="statusCodeInput" class="col-sm-2 col-form-label">Status Code (required):</label>
                <div class="col-sm-4">
                    <input type="text" name="statusCode" <?php echo $statusCodeClass; ?> id="statusCodeInput" placeholder="eg: S0001">
                </div>
            </div>
            <!-- Status text group. This has the label and input-->
            <div class="form-group row">
                <label for="statusText" class="col-sm-2 col-form-label">Status (required):</label>
                <div class="col-sm-8">
                    <input type="text" name="statusText" <?php echo $statusTextClass; ?> id="statusText" placeholder="eg: programming my first assignement">
                </div>
            </div>

            <!-- Button Fields -->
            <div class="form-group">
                <input type="submit" class="btn btn-primary" name="post" value="submit">
            </div>
        </form>
    </div>

3 个答案:

答案 0 :(得分:3)

通过以下更改您的PHP代码,它将起作用:

$statusCodeClass = 'class="form-control"';
$statusTextClass = 'class="form-control"';
// Are all fields fill
if (isset($_POST['post'])) {
        $isCodePresent = checkStatusCodeIsEmpty();
        $isTextPresent = checkStatusTextIsEmpty();

        //Check if the fields are empty
        if ($isCodePresent && $isTextPresent) {
            echo 'move to the next page';
        } else {
            echo 'not correct yet';
        }

        $statusCodeClass = 'class="form-control '.($isCodePresent ? 'is-valid' : 'is-invalid').'"';
        $statusTextClass = 'class="form-control '.($isTextPresent ? 'is-valid' : 'is-invalid').'"';
    }

    function checkStatusCodeIsEmpty()
    {
        return !empty($_POST['statusCode']);
    }
    function checkStatusTextIsEmpty()
    {
        return !empty($_POST['statusText']);
    }
}

您试图在该范围中未定义的函数中分配变量。我已经更改了基于函数输出分配代码的代码。

答案 1 :(得分:1)

也许您可以尝试这样的功能:

 function checkStatusCodeIsEmpty($statusCode){
      $statusCodeClass= 'class="form-control"';
        if(!statusCode){
            $statusCodeClass='class="form-control is-invalid"';
        }else{
            $statusCodeClass='class="form-control is-valid"';
        }
       return $statusCodeClass;
    }

然后您可以这样称呼

 $statusCodeClass=checkStatusCodeIsEmpty($_POST['statusCode']);

与其他功能相同

答案 2 :(得分:0)

您可以使用内联php if-else语句来这样做:

//Define an error variable on the top of your page
<?php $error = 0; ?>

//Codes here

<div class="form-group row">
    <label for="statusText" class="col-sm-2 col-form-label">Status (required):</label>
    <div class="col-sm-8">
    <?php
        if(empty($_POST['statusText'])){
            echo '<input type="text" name="statusText" class="form-control is-invalid" id="statusText" placeholder="eg: programming my first assignement">';
            $error = 1;
        } else {
            echo '<input type="text" name="statusText" class="form-control is-valid" id="statusText" value="'.$_POST['statusText'].'">';
        }
    ?>
    </div>
</div>

//At the end of the page
<?php
    if($error != 0){
        echo 'Not correct yet';
    } else {
        echo 'Move to the next page';
    }
?>