I'm trying to Validate email twice on a registration form, in order to do it i'm trying to first check that the actual email field isn't empty by running the "empty" function or both of the fields - but for some reason it wont accept it, it complains about unexpected '('
here's the line:
if (empty(($_POST['e-mail']) OR ($_POST['e-mail2']))) {
$error[] = 'Please Enter your Email ';
}
anyone has an idea why i'm getting it and how to fix it ?
Try:
if (empty($_POST['e-mail']) || empty($_POST['e-mail2'])) {
$error[] = 'Please Enter your Email ';
}
You might also want to include the isset() function to make sure the POST key is actually set before checking for a value.
Edit
Just for clarity, I'll post an edit here. If you want to validate both the existence of a value, and whether one value for an input equals another, you need to do two sanity checks:
// Check existence of values
if (empty($_POST['e-mail']) || empty($_POST['e-mail2'])) {
$error[] = 'Please Enter your Email ';
}
// Check equivalence
if ($_POST['e-mail'] != $_POST['e-mail2']) {
$error[] = 'Your e-mails do not match';
}
You're passing logical expression to empty(), which is obviously wrong
you need to check both variables separately
if (empty($_POST['e-mail']) OR empty($_POST['e-mail2'])) {
$error[] = 'Please Enter your Email ';
}
You meant:
if (empty($_POST['e-mail']) OR empty($_POST['e-mail2']))
PHP cannot guess that, it just evaluates your expression like that:
if (empty(($_POST['e-mail']) OR ($_POST['e-mail2'])))
if (empty('value_of_email1' OR 'value_of_email1' ))
if (empty( true ))
if ( false )
if one of the submitted values is truthish (i.e. not-empty), the OR expression still evaluates to true.
I don't think you can use logical expression within an empty() method.
Try separating logical expression to two separate empty() method calls.
Try:
if (empty($_POST['e-mail']) OR empty($_POST['e-mail2'])) {
$error[] = 'Please Enter your Email ';
}
For more information on empty(), take a look at PHP manual on empty().
For posterity...
$email1 = filter_var('aaa#email.com', FILTER_VALIDATE_EMAIL);
$email2 = filter_var('aaa#email.com', FILTER_VALIDATE_EMAIL);
$unmatched1 = filter_var('aaa#emil.com', FILTER_VALIDATE_EMAIL);
$unmatched2 = filter_var('aab#email.com', FILTER_VALIDATE_EMAIL);
$invalid1 = filter_var('aaa.com', FILTER_VALIDATE_EMAIL);
$invalid2 = filter_var('email.com', FILTER_VALIDATE_EMAIL);
$empty = filter_var('', FILTER_VALIDATE_EMAIL);
var_dump(!$email1 || $email1 !== $email2);
var_dump(!$email1 || $email1 !== $unmatched2);
var_dump(!$unmatched1 || $unmatched1 !== $email2);
var_dump(!$email1 || $email1 !== $invalid2);
var_dump(!$invalid1 || $invalid1 !== $email2);
var_dump(!$email1 || $email1 !== $empty);
var_dump(!$empty || $empty !== $email2);
Gives:
bool(false) // Desired in validation...
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
https://ignite.io/code/511f8bf8ec221e7f21000000
So if you replace var_dump with if and simplify e-mail2 (no need to validate):
$email = filter_var($_POST['e-mail'], FILTER_VALIDATE_EMAIL);
if (!$email || $email !== $_POST['e-mail2']) {
// error stuff, including separate check on empty.
}
Related
I want to check if email is required and if yes,check email is not null ,Whenever i am checking this condition the variable validation_ok is always returning 1 as the value . Even if the condition satisfies, every time it is returning true.
This is my code:
if ($validations['customer_reg_email'] == 'required' && $email=='')
{
$validation_ok = false;
$status = 'Please provide a valid email';
}
else
{
$validation_ok = true;
}
I would appreciate your help regarding this issue.Thanks in advance
It's much better to check your email with empty() and check what you have with var_dump() function.
var_dump($validations['customer_reg_email']);
echo "\n";
var_dump($email);
echo "\n";
$validation_ok = TRUE;
if($validations['customer_reg_email'] == 'required' and empty($email)){
$validation_ok = FALSE;
$status = 'Please provide a valid email';
}
if($action == "send"){
$_POST['name'] = $name ;
$_POST['email'] = $email ;
$_POST['phone'] = $phone ;
if(!empty($name) || !empty($email) || !empty($phone)){
.....
} else {
$msg = 'All fields required';
}
//whatever I do only shows $msg.
//already tried that too
if(!empty($_POST['name']) || !empty($_POST['email']) || !empty($_POST['phone'])){
....
}
What Im trying to do is a form that email me the data, and I want all fields to be filled so maybe Im writing the if statement the wrong way.
sorry if I didnt explained well before.
Your code reads:
If name is not empty, or email is not empty, or phone is not empty
This means that as long as at least one of them are non-empty, then you're good!
Pretty sure that's not what you meant. You want:
If name is not empty, AND email is not empty, AND phone is not empty
Use && instead of || and it should just work!
I think you're getting confused by all the negatives involved here. I suspect what you're after is:
if (!(empty($name) || empty($email) || empty($phone))) {
...
} else {
$msg = 'All fields required';
}
Which would be better written (in my opinion) as:
if (empty($name) || empty($email) || empty($phone)) {
$msg = 'All fields required';
} else {
...
}
if($name=='' || $email=='' || $phone=='')
{
$msg='All fields required';
}
else
{
..............
}
I have a conditional if check on a webform on which ajax (and php) is doing the submit work.
if(empty($name) or empty($message))
{
do something
}
The above is working if one of the 2 strings is empty.I want to add to the above
or (empty($name) and empty($message))
for checking if both strings are empty but in someway its not working!I want to make sure all 3 senarios are covered,Name empty or Message empty or both empty.
Any ideas?
This will check that both $name and $message is not blank, than true
if($name != '' && $message != '') {
//Do something
}
If you use this, this will check if any 1 field is empty than the condition is true
if($name == '' || $message == '') {
//Do something
}
If you use this, this will be true if both are empty
if($name == '' && $message == '') {
//Do something
}
Replace "or" with "||". Example:
if (empty($name) || empty($message))
{
// Do something
}
or means "If either condition is true", not "If exactly one condition is true".
The code you have already covers your requirement.
I've a form,in the form only few fields are mandatory.When a fields is not mandatory,it should not check for empty data validation.If the non mandatory field contain data then it shoudl check the data,validation should happen only when data present.
My below code check for all fields.For eg:Say phone is not mandatory in below code,how to change the code.
$validate = array(
array($x, '/^[a-z\d ]{4,20}$/i', "Please enter valid name."),
array($y, '/^[a-z\d ]{4,20}$/i', "Please enter a real category."),
array($phone, '/^\(?[0-9]{3}\)?|[0-9]{3}[-. ]? [0-9]{3}[-. ]?[0-9]{4}$/' , "Please enter a valid phone number")
);
$error = '';
foreach ($validate as $validation)
{
if (!preg_match($validation[1],$validation[0]))
{
$error .= $validation[2];
}
}
if($error != '')
{
echo $error;
exit;
}
Comment on this post,if it is not clear.
Thanks in advance!
If you want to check if at least required fields have been submitted, you can check whether they have been set and have a truthy value using the isset and empty functions.
For example:
if ( isset($_POST['username'], $_POST['password']) &&
! empty($_POST['username']) && ! empty($_POST['password']) ) {
// validation here
}
That would check if the username and password fields were submitted and have a value, whereas for example an email field wouldn't necessarily have been filled in for the above condition to return true.
If you know the mandatory fields before hand I'll suggest you group them in an array and test for based on the current key. If a key is in not mandatory but holds value you can test otherwise do your regular check.
$error = '';
$mandatoryFields = array('key1', 'key2' 'key3');
foreach ($validate as $validation)
{
if (!in_array($validation[0], $mandatoryFields) && strlen(trim($validation[0])) > 0)
{
// There is data in a non mandatory field.
// Perform your test.
}
else {
// Do your regular test.
if (!preg_match($validation[1],$validation[0]))
{
$error .= $validation[2];
}
}
}
You can give this a try.
I'm trying to make an if statement with 2 conditions. One that checks if one variable is NOT present & does NOT matches the word "good2go" and the other that checks to make sure "body" variable is present. I'm trying to trip the error message here. Here is what I have and what I've tried, and none of it seems to work.
if (stripos($_POST['check'], 'good2go') == FALSE && $_POST['body']) {
$error = true; }
if (!$_POST['check'] == 'good2go' && $_POST['body']) {
$error = true; }
if (!stripos($_POST['check'], 'good2go') && $_POST['body']) {
$error = true; }
if ((!stripos($_POST['check'], 'good2go')) && $_POST['body']) {
$error = true; }
How do I get this to work?
here's the entire code of contact_us.php this has the validation code and the email code.
$error = false;
if (isset($_GET['action']) && ($_GET['action'] == 'send')) {
// Winnie the pooh check
//$t = tep_db_prepare_input($_POST['verify']);
if (!isset($_POST['check']) && !$_POST['check']=='good2go' && isset($_POST['body'])) {
$error = true;
} else { // Winnie the pooh Check
$name = tep_db_prepare_input($_POST['name']);
$email_address = tep_db_prepare_input($_POST['email']);
//IP recorder start
$ipaddress = $_SERVER["REMOTE_ADDR"];
$ip = "\n\nIP: " . $ipaddress;
$content = "\n\nName: ".$name."\n\nComments: ".$_POST['enquiry'];
$product = tep_db_prepare_input($_POST['product']);
if ($product) {
$product_text = "\n\nProduct Interest: ".$product; }
$content_ip = $content . $product_text. $ip;
$enquiry = tep_db_prepare_input($content_ip);
//IP recorder end
}
// BOF: Remove blank emails
// if (tep_validate_email($email_address)) {
// tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, $enquiry, $name, $email_address);
// tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
// } else {
// $error = true;
// $messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
if (! tep_validate_email($email_address)) {
$error = true;
$messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
}
if ($enquiry == '') {
$error = true;
$messageStack->add('contact', ENTRY_EMAIL_CONTENT_CHECK_ERROR);
}
if ($error == false) {
tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, $enquiry, $name, $email_address);
tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
// EOF: Remove blank emails
}
}
Solution to your updated problem:
if (!isset($_POST['check']) || !$_POST['check']=='good2go' || !isset($_POST['body'])) {
$error = true;
}
The reason for the pipes vs ampersands is that you want to throw an error if ANY of the fields has issue. Also, you want to check if body is NOT set vs IS set. Glad this worked out for you!
and the other that checks to make sure "body" variable is not present.
if(stripos($_POST['check'], "good2go") !== false && !isset($_POST['body'])){
//code here
}
According to PHP docs regarding the stripos function:
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
So you need to change the first line to:
// Doing stripos checks you MUST use === (not ==)
if (stripos($_POST['check'], 'good2go') !== FALSE && $_POST['body']) {
$error = true; }
And to check if there is no $_POST['body'] you can change the above to:
if (stripos($_POST['check'], 'good2go') !== FALSE && (!isset($_POST['body'])) {
-- Update --
According to your comment, you need $_POST['check'] to equal 'good2go', then you shouldn't be using stripos as it will check for the existence of good2go regardless if it's exactly equal, or part of a string; 'wow this hamburger is good2go'.
So I would change the conditional to:
if (((isset($_POST['body'])) && (strlen($_POST['body']) > 0)) && ((!isset($_POST['check'])) || ($_POST['check'] !== 'good2go'))) {
// Post body has a value and Post check DOES NOT equal good2go, someone is hax0rin!
}
You may want to read up on Cross-site request forgery as it seems right inline with what you are working on.
One that checks if one variable is present & matches the word "good2go"
isset($_POST['check']) AND $_POST['check'] == 'good2go'
and the other that checks to make sure "body" variable is not present.
!isset($_POST['body'])
so, just put them together
if (isset($_POST['check']) AND $_POST['check'] == 'good2go' AND !isset($_POST['body'])) {
$error = true;
}
try this:
if(!empty($_POST['check']) && $_POST['check']=='good2go' && empty($_POST['body'])) { $error=true; }
Consider using empty instead of isset if your $_POST['body'] can be present with an empty value.
No need for all those unneeded functions. What you are trying to achieve is:
if (isset($_POST['check']) && $_POST['check']=='good2go' && !isset($_POST['body']) {
// your code
}
However, As per the title of the question: Use a ternary statement. Syntax is as such
$var = <condition> ? <true> : <false>;