我正在研究一个带有PHP的项目,我制作了此文件:
<?php
$err = false;
if (isset($_POST['submit'])){
$projectname = $_POST['project_name'];
if (strlen($projectname)>10){
$err = true;
echo "
<script type='text/javascript'>
alert('Your project name contains too many words');
</script>
";
}
$emailone = $_POST['mail_one'];
$emailtwo = $_POST['mail_two'];
$numstars = $_POST['num_stars'];
$numchars = $numstars + 2;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome To Email Guesser</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<br />
<h1 style="text-align:center;color: #F90B6D; font-family: 'Open Sans', sans-serif; font-size: 34px; font-weight: 300; line-height: 40px; margin: 0 0 16px;">Email Guesser <small>v0.2</small></h1>
<h1 style="text-align:center;color: #F90B6D; font-family: 'Open Sans', sans-serif; font-size: 24px; font-weight: 300; line-height: 32px; margin: 0 0 14px;"><a href="http://www.emailguesser.pouyavagefi.com">www.emailguesser.pouyavagefi.com</a></h1>
<br />
<div class="inner contact">
<!-- Form Area -->
<div class="contact-form">
<!-- Form -->
<form id="contact-us" method="POST" action="action.php">
<!-- Left Inputs -->
<div class="col-xs-6 wow animated slideInLeft" data-wow-delay=".5s">
<!-- Name -->
<input type="text" name="project_name" id="name" required="required" class="form" placeholder="Name Of Project" />
<!-- Another Email Address -->
<input type="email" name="mail_one" id="mail" class="form" placeholder="Other Email Address You May Know #1" />
<!-- Another Email Address Two -->
<input type="email" name="mail_two" id="mail" class="form" placeholder="Other Email Address You May Know #2" />
<!-- Subject -->
<input type="number" name="num_stars" id="subject" required="required" class="form" placeholder="Number Of Stars" />
</div><!-- End Left Inputs -->
<!-- Right Inputs -->
<div class="col-xs-6 wow animated slideInRight" data-wow-delay=".5s">
<!-- Message -->
<textarea name="message" id="message" class="form textarea" placeholder="Message"></textarea>
</div><!-- End Right Inputs -->
<!-- Bottom Submit -->
<div class="relative fullwidth col-xs-12">
<button type="submit" id="submit" name="submit" class="form-btn semibold">Submit</button>
</div><!-- End Bottom Submit -->
<!-- Clear -->
<div class="clear"></div>
</form><p><i>make your project online on social medias with this hashtag: <strong>#hcktck</strong></i></p>
</div><!-- End Contact Form Area --></br>
</div>
<hr class="style-one">
<!-- Showing Outputs -->
<div class="container outptut">
<h3>Returning Results:</h3>
<?php
if($err=false){
echo "
<p class='bg-primary'> Victim's email address is $numchars characters long</p>
<p class='bg-success'> This text indicates success.</p>
<p class='bg-info'> This text represents some information.</p>
<p class='bg-warning'> This text represents a warning.</p>
<p class='bg-danger'> This text represents danger.</p>
";
}else{
echo "<h4 style='color:red;'>an error occured, try again or contact with our developers.</h4>";
}
?>
</div>
<center> Visit Developer's Website <a href="http://pouyavagefi.com" target="blank">Pouya Vagefi </a> </center>
</div>
</body>
</html>
此文件称为action.php
,每当我使用正确的信息运行时,它都会显示我以前设置的an error occured, try again or contact with our developers.
。基本上,如您所见,我称之为布尔变量($ err),如果出现任何类型的错误(例如,一个具有太多字符的项目名称)。
,但是问题在于,即使我提交了正确的信息,该变量也设置为true。但是我在页面上以$ err = false打电话。
那么为什么这件事会发生呢?请注意,根本没有PHP错误。
从:
将if($err=false){
更改为if($err === false){
<?php if($err=false){ echo "
<p class='bg-primary'> Victim's email address is $numchars characters long</p>
<p class='bg-success'> This text indicates success.</p>
<p class='bg-info'> This text represents some information.</p>
<p class='bg-warning'> This text represents a warning.</p>
<p class='bg-danger'> This text represents danger.</p>
"; }else{ echo "<h4 style='color:red;'>an error occured, try again or contact with our developers.</h4>"; } ?>
使用它的方式,您只是在挑剔。
您需要实际验证两个值。如果使用===
,则还检查变量类型,在这种情况下,boolean
。
您需要将支票设置为if($err===false)
。您现在拥有它的方式,将其设置为false而不是检查它是否为false。