[英]How can I change the class tag of an HTML element using PHP when I click an input
First of all I cannot use JavaScript for this. 首先,我不能为此使用JavaScript。 As part of this homework we can only use PHP. 作为本作业的一部分,我们只能使用PHP。
My current code listens for a input type="submit"
post and then checks if some text inputs are empty or have something in them. 我当前的代码侦听input type="submit"
帖子,然后检查某些文本输入是否为空或其中是否包含某些内容。 Currently it will just echo some text which works. 当前,它将仅回显一些有效的文本。
What I want is for the class tag on the input text fields to change based on whether they're full or not. 我想要的是根据输入文本字段上的class标记来更改它们是否已满。
Bellow is my current code: 贝娄是我当前的代码:
<?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>
Change your PHP code by following, it will work: 通过以下更改您的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']);
}
}
You were trying to assign the variable in a function which was not defined in that scope. 您试图在该范围中未定义的函数中分配变量。 I have changed the code to assign the variable based on the function output. 我已经更改了代码,以根据函数输出分配变量。
You can use inline php if-else statements to do so: 您可以使用内联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';
}
?>
Maybe you could try the functions like this: 也许您可以尝试这样的功能:
function checkStatusCodeIsEmpty($statusCode){
$statusCodeClass= 'class="form-control"';
if(!statusCode){
$statusCodeClass='class="form-control is-invalid"';
}else{
$statusCodeClass='class="form-control is-valid"';
}
return $statusCodeClass;
}
Then you can call it like this 然后你可以这样称呼它
$statusCodeClass=checkStatusCodeIsEmpty($_POST['statusCode']);
The same with the other fucntion 与其他功能相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.