I have a contact form which has a hidden field (profession). I am trying to get the script to check to make sure this hidden field IS empty and if so, then send the form result to me. If however this field is NOT empty, the form is not sent to me.
Orginially my code was:
if(isset($_POST['profession']) && $_POST['profession'] == ''){
But I believe this is actually incorrect and is forcing the profession field to be blank? So I believe the code should simply be:
if(!isset($_POST['profession'])){
Have I got this correct. Which would be the best way to code this?
PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.
To show invalid input in PHP, set the name of the input textbox which is in HTML. All the fields are first checked for empty fields and then it is validated for correctness. If all fields are correct then it shows the success message.
Nice to see you using the honeypot method of spam prevention. The best you can probably do is simply this:
if (empty($_POST['profession'])){
// Send form result.
}
The empty()
function evaluates to true
when it's an empty string (''
) or when the variable or array element doesn't exist at all. For more details on comparisons see here: http://php.net/manual/en/types.comparisons.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With