0

I'm working on a project with PHP and I made this file:

<?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'> &nbsp; Victim's email address is $numchars characters long</p>
                            <p class='bg-success'> &nbsp; This text indicates success.</p>
                            <p class='bg-info'> &nbsp; This text represents some information.</p>
                            <p class='bg-warning'> &nbsp; This text represents a warning.</p>
                            <p class='bg-danger'> &nbsp; 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>

This file is called action.php and whenever I run it with correct information it shows me an error occured, try again or contact with our developers. which I have set before. Basically as you can see I have called a boolean variable ($err) which take true if any kind of error appeared (for example a project name which has too many chars).

But the problem is this variable is set to true even when I submit the form with correct info. However I have called at the $err = false at the top the page.

So why this thing happens ? Note that no php errors appears at all.

  • `$err=false` should be `$err==false`. in the if statement. With a single equal sign the expression is always set to false. – michaPau Dec 20 '16 at 15:41

2 Answers2

1

Change if($err=false){ to if($err === false){ from:

<?php if($err=false){ echo "
                            <p class='bg-primary'> &nbsp; Victim's email address is $numchars characters long</p>
                            <p class='bg-success'> &nbsp; This text indicates success.</p>
                            <p class='bg-info'> &nbsp; This text represents some information.</p>
                            <p class='bg-warning'> &nbsp; This text represents a warning.</p>
                            <p class='bg-danger'> &nbsp; This text represents danger.</p>
                    "; }else{ echo "<h4 style='color:red;'>an error occured, try again or contact with our developers.</h4>"; } ?>

The way you are using it, you are just asigning the value.

You need to to actually verify the two values. If you use === then you also check for the variable type, in this case boolean.

Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
0

You need to set your check to if($err===false). The way you have it now, you're setting it to false instead of checking if it's false.

J_D
  • 681
  • 2
  • 8
  • 22