! Empty (trim ($ _ POST ['username']

Well, the problem is that when I use the trim function, it does not work, but when I run the code without the trim function, it works, but does not work properly (the form takes spaces)

<?php session_start(); unset($_SESSION['username']); if (isset($_SESSION['username'])) {echo "You are in already";} else if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (!empty(trim($_POST['username'])) && !empty(trim($_POST['email']))) { $uname = htmlentities($_POST['username']); $email = htmlentities($_POST['email']); $_SESSION['username'] = $uname; echo "THANKS: " . $uname . "<br />"; } else { echo "fill the goddemn field"; } } else { ?> <form action="index.php" method="post"> <label for="username">USERNAME:</label> <input type="text" name="username" /> <label for="E-MAIL">E-mail:</label> <input type="text" name="email" /> <input type="submit" value="Enter" /> </form> <?php } ?> 

I tried the manual http://php.net/manual/en/function.trim.php , but it was hard to read, and I did not understand anything.

+7
source share
1 answer

The PHP manual says:

empty - determine if the variable is empty

In your case, trim is a function call, not a variable.

If you really want to make your if inline if , you can use something like:

 if (!empty($var=trim($_POST['username'])) && !empty($var=trim($_POST['email']))) 

Strike>

But it is better to implement:

 $username = array_key_exists('username', $_POST) ? trim($_POST['username']) : null; $email = array_key_exists('email', $_POST) ? trim($_POST['email']) : null; if (!empty($username) && !empty($email)) { (...) 
+15
source

All Articles