Reputation: 793
I have an HTML form and I use isset
to check if POST
variables are set, then I process them (send them via email or to a Google Spreadsheet). Sometimes I receive empty result(s), like if the variable(s) is/are null or empty, not even an empty space. Why is this happening? Am I missing something?
<?php if(isset($_POST["name"]) && isset($_POST["tel"])){
$name = $_POST["name"];
$phone = $_POST["phone"];
$message="Name: $name \nPhone : $phone \n";
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('[email protected]', 'From Name');
$mail->addAddress('[email protected]', 'To Name');
$mail->Subject = 'Message Subject';
$mail->Body = $message; ?>
<form enctype="multipart/form-data" action="#" method="post">
<input id="name" type="text" name="name" title="Enter your name" placeholder="Name" required >
<input id="phone" type="phone" name="phone" pattern="0(6|5)([-. ]?[0-9]{2}){4}" title="Enter your phone" placeholder="Phone" required >
<button type="submit" id="submit" name="submit">Send</button>
</form>
P.S. The fields may have RTL Arabic entries sometimes. I'm also using client-side Javascript validation to check if the fields are valid and not empty. I understand that client-side validation can be flooded since it's client-side and since browsers are different from each other, however, I can't figure out how empty entries are still returning true
from if(isset($_POST[])
! Is that a normal behaviour of isset($_POST[])
when the input
fields are empty? Will checking if !empty($_POST[])
be correct and relevant in my case?
.
Upvotes: 0
Views: 1262
Reputation: 1137
isset() just checks that the variable is set and not NULL.
http://php.net/manual/en/function.isset.php
If it's set and empty, isset() returns TRUE.
You need to use empty() to check for an empty value.
http://php.net/manual/en/function.empty.php
You can also do a strict check against the value if you know what to check against:
if (isset($_POST['name']) && $_POST['name'] !== '')
Upvotes: 2