PHP checking if a field is empty

Status
Not open for further replies.

Insidium

New Member
Jan 24, 2015
8
1
I literally have no idea why this isn't working. I am trying to make a simple ban log system where I can manually log all of the bans I have inputted for each day and add notes about the ban. I am trying to make the code check for notes and if there are no notes, it will print that there are none and send it through to the database. I have over complicated the code just to make sure it works correctly, but, it still won't work.
The form's HTML:
HTML:
<form action="insert.php" method="post">
            Username<br>
            <input name="name" type="text" title="What was the user's name?"><br>
            Reason<br>
            <input name="reason" type="text" title="Why did you ban them?"><br>
            Notes<br>
            <input name="notes" type="text" title="Any notes?"><br>
        <input type="submit">
    </form>
insert.php:
PHP:
   <?php $notes = $_POST['notes'];
  
    $checkfornotes = false;
  
    if(!empty($notes)){
        $checkfornotes = true;
        }else{
        $checkfornotes = false;
        }
  
    echo "Your ban for " . $_POST['name'] . " has been added.";
    echo "<br/><br/>";
    echo "You banned " . $_POST['name'] . " for: " . $_POST['reason'];
    echo "<br/><br/>";
  
    if($checkfornotes = true){
        echo "You also put:<br/><br/>" . $_POST['notes'];
    }elseif($checkfornotes = false){
        echo "You didn't put any notes in!";
    }else{
        echo "LOLWTFHAPPENED";
    }
?>
What it's outputting when I put no notes:
oo8QrqI.png

Thanks for the help.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,639
2,396
e2ugAps.png


Right here, what you're doing is checking to see if $checkfornotes is set to true after setting it to true in the same line. Change = to == and try it then.

Here's a breakdown of the equals sign in PHP, and various other programming languages

= sets a value to a variable
== checks if two things are equal
=== checks it two things are equal AND the same data type (string, array, int etc)
 

Insidium

New Member
Jan 24, 2015
8
1
e2ugAps.png


Right here, what you're doing is checking to see if $checkfornotes is set to true after setting it to true in the same line. Change = to == and try it then.

Here's a breakdown of the equals sign in PHP, and various other programming languages

= sets a value to a variable
== checks if two things are equal
=== checks it two things are equal AND the same data type (string, array, int etc)
Such a stupid mistake yet it made such a big difference. Thanks for the help!
 
Status
Not open for further replies.

Users who are viewing this thread

Top