[英]How to change input text color if it is blank submit by help of php and javascript?
In index.php I have input area for emailId, php code to show error by user and my in login page I have php code to store user email id in database(mysql).在 index.php 中,我有 emailId 的输入区域,php 代码显示用户和我的登录页面错误 我有 php 代码将用户电子邮件 id 存储在数据库(mysql)中。 And I don't want user submit blank email id to handle this problem I wrote "if (empty($uemail)){" but I want to show user input area is empty by changing changing border color of input text area into red.
我不希望用户提交空白电子邮件 ID 来处理这个问题,我写了“if (empty($uemail)){”,但我想通过将输入文本区域的边框颜色更改为红色来显示用户输入区域为空。
index.php索引.php
<input type="email" id="email" name="email" placeholder="Email">
<?php
session_start();
if(isset($_SESSION['error'])){
echo '<script type="text/javascript">
function downIt() {
document.getElementById("input").style.cssBorder = "color: red";
}
</script>';
}
unset ($_SESSION['error']);
session_destroy ();
?>
login.php登录.php
$uemail = $_POST['email'];
if (empty($uemail)){
$_SESSION['error'];
Header( 'Location: index.php');
exit;
}
else{
query code....here....
}
Why you do not try just HTML5 instead?为什么你不只尝试 HTML5 呢? For example:
例如:
<style>
.was-validated input:invalid {
border: 1px solid red;
}
</style>
<script>
// Bootstrap code
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function () {
'use strict';
window.addEventListener( 'load', function () {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName( 'needs-validation' );
// Loop over them and prevent submission
var validation = Array.prototype.filter.call( forms, function ( form ) {
form.addEventListener( 'submit', function ( event ) {
if ( form.checkValidity() === false ) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add( 'was-validated' );
}, false );
} );
}, false );
})();
</script>
<form method="post" action="login.php" class="needs-validation" novalidate>
<input type="email" id="email" name="email" placeholder="Email" required />
<br />
<input type="submit" />
</form>
But if you want to use PHP anyway, I do not see reason to use Javascript.但是如果你无论如何都想使用 PHP,我看不出使用 Javascript 的理由。
index.php索引.php
<?php
session_start();
$error_border = '';
if ( isset( $_SESSION[ 'error' ] ) ) {
$error_border = ' style="border: 1px solid red;"';
}
unset ( $_SESSION[ 'error' ] );
?>
<form method="post" action="login.php">
<input type="email" id="email" name="email" placeholder="Email"<?php echo $error_border; ?> />
<br />
<input type="submit" />
</form>
<?php session_destroy(); ?>
login.php登录.php
<?php
session_start(); // resume existing session
$uemail = $_POST[ 'email' ];
if ( empty( $uemail ) ) {
$_SESSION[ 'error' ] = 1;
Header( 'Location: index.php' );
exit;
} else {
query code....here....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.