我正在使用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错误。
答案 0 :(得分:1)
将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
。
答案 1 :(得分:0)
您需要将支票设置为if($err===false)
。你现在拥有它的方式,你将它设置为false而不是检查它是否为假。