Given the below variables:
$field1;
$field2;
$field3;
$field4;
$field5;
How can I use logical operators so that the user has to fill in either field: 1,2,3 OR either field 1, 2, 4, 5. If user does not do one of the following, then I want to give error required fields not complete.
I have tried:
if ((!$field1 | !$field2 |!$field3) | (!$field1 | !$field2 |!$field4|!$field5))
$errors[] = 'You did not complete all of the required fields.';
if (!
// Exactly 1,2,3 are filled in (not 4, 5)
(!empty($field1) && !empty($field2) && !empty($field3) && empty($field4) && empty($field5))
// or Exactly 1,2,4,5 are filled in (not 3)
&& !(!empty($field1) && !empty($field2) && !empty($field4) && !empty($field5) && empty($field3))
) {
// print error
}
Let a = not empty $field1, b = not empty $field2, ..., e = not empty $field5
You want
(a && b && c) || (a && b && d && e)
= (a && b) && ( c || (d && e))
In php:
if(!empty($field1) && !empty($field2) &&
( !empty($field3) || (!empty($field4) && !empty($field5))
) {
// process
} else {
// error
}
If you consider 0 as a valid value use isset in place of empty.
Finally get to use those algebra I learned in school.
Try:
if (!empty($field1) && !empty($field2) && !empty($field3)) {
//process
} else if (!empty($field1) && !empty($field2) && !empty($field4) && !empty($field5)) {
//process
} else {
$errors[] = 'You did not complete all of the required fields.';
}
Or, require that no extra fields are filled in for a given option:
if (!empty($field1) && !empty($field2) && !empty($field3) &&
empty($field4) && empty($field5)) {
//process
} else if (!empty($field1) && !empty($field2) && !empty($field4) && !empty($field5) &&
empty($field3)) {
//process
} else {
$errors[] = 'You did not complete all of the required fields.';
}
I am sure where the $fields are comming from, but the following might do the trick:
if (!(isset($field1) && isset($field2) && isset($field3)) || !(isset($field1) && isset($field2) && isset($field3) && isset($field4)){
//Set the error
}
I occasionally do something like the following for simple validations such as this
$fields = array('field1', 'field2', 'field3', 'field4', 'field5');
foreach($fields as $field) {
if(!isset($_POST[$field]) || strlen(trim($_POST[$field])) == 0) {
// set error message
}
}
Related
I am very new to PHP and trying to get an IF statement to work to change the membership level based on the possibility of multiple answers provided in the registration form. However, it's not working. If anyone can help me understand what I am doing wrong/missing that would be a great help. Here is my code. Thank you.
function my_pmpro_checkout_level($level)
{
if ( ! empty( $_REQUEST['registrationlevel']) && $level->id == '1' ) {
if (( $_REQUEST['registrationlevel'] == 'eligibleforlimited' && $_REQUEST['experience'] != 'morethansixyears') || ($_REQUEST['registrationlevel'] == 'eligibleforlimited' && $_REQUEST['timeassesment'] == 'no')) {
$level->id = '3';
} elseif ( $_REQUEST['registrationlevel'] == 'eligibleforprovisional' && $_REQUEST['experience'] != 'noexperience') {
$level->id = '3'}
}
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level");
$query=mysql_query("SELECT * FROM `scadatel_spin`.`spin` WHERE `tanggal_berangkat`='$tanggal_berangkat' AND `tanggal_kembali`='$tanggal_kembali' ORDER BY `id`");
$data2=mysql_fetch_array($query);
$cek1=$data2['pelaksana1'];
$cek2=$data2['pelaksana2'];
$cek3=$data2['pelaksana3'];
$cek4=$data2['pelaksana4'];
if(($cek1==$pelaksana1) && ($cek2==$pelaksana2) && ($cek3==$pelaksana3) && ($cek4==$pelaksana4)){
header('location: gagal_input.php');exit;
}
if(($cek1==$pelaksana1) && ($cek2==$pelaksana2) && ($cek3==$pelaksana4) && ($cek4==$pelaksana3)){
header('location: gagal_input.php');exit;
}
if(($cek1==$pelaksana1) && ($cek2==$pelaksana3) && ($cek3==$pelaksana4) && ($cek4==$pelaksana2)){
header('location: gagal_input.php');exit;
}
if(($cek1==$pelaksana1) && ($cek2==$pelaksana3) && ($cek3==$pelaksana2) && ($cek4==$pelaksana4)){
header('location: gagal_input.php');exit;
}
if(($cek1==$pelaksana1) && ($cek2==$pelaksana4) && ($cek3==$pelaksana2) && ($cek4==$pelaksana3)){
header('location: gagal_input.php');exit;
}
if(($cek1==$pelaksana1) && ($cek2==$pelaksana4) && ($cek3==$pelaksana3) && ($cek4==$pelaksana2)){
header('location: gagal_input.php');exit;
}
If I used three AND operators, It worked properly.
But, If I used more than three AND operators, It could not work properly.
Anybody help me to solve this problem ?
This might work better for what you're trying to do. I am on my phone so please excuse any errors, but pretty much you want to put them all in an array and test if the first value matches the rest:
$query=mysql_query("SELECT * FROM `scadatel_spin`.`spin` WHERE `tanggal_berangkat`='$tanggal_berangkat' AND `tanggal_kembali`='$tanggal_kembali' ORDER BY `id`");
$data2=mysql_fetch_array($query);
$cek1=$data2['pelaksana1'];
$cek2=$data2['pelaksana2'];
$cek3=$data2['pelaksana3'];
$cek4=$data2['pelaksana4'];
$verifyData = array();
$verifyData[] = $cek1;
$verifyData[] = $cek2;
$verifyData[] = $cek3;
$verifyData[] = $cek4;
$allMatch = true;
$firstValue = current($verifyData);
foreach ($verifyData as $val) {
if ($firstValue !== $val) {
$allMatch = false;
}
}
if(!$allMatch){
header('location: gagal_input.php');
exit;
}
I know that || or && need to be used but I can't work out the correct or best way to format this.
My code for one cookie:
if(isset($_COOKIE['mycookie'])) {
if($_COOKIE['mycookie']=="value1") {
// do some stuff
}
}
But I'd like to include another cookie in this routine where either one can be true for the "stuff" to work.
I'm just not sure how to format this. Is it something like this?
if(isset($_COOKIE['mycookie'] || ['mycookie2')) {
if($_COOKIE['mycookie']=="value1" || $COOKIE['mycookie2']=="value2") {
// do some stuff
}
}
You can write all in one if statement if you want like this:
(The OR statement in the isset() function is not going to work)
if ( (isset($_COOKIE['mycookie']) && $_COOKIE['mycookie'] == "value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] == "value2") )
You need to do the || outside the function, to combine the results of all the calls.
if (isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2'])) {
// do some stuff
}
It will be:
if (isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2'])) {
if ($_COOKIE['mycookie'] == "value1" || $_COOKIE['mycookie2'] == "value2") {
// do some stuff
}
}
Or even:
if ((isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2') && ($_COOKIE['mycookie'] == "value1" || $_COOKIE['mycookie2'] == "value2")) {
// do some stuff
}
to avoid nested if.
Try this
if((isset($_COOKIE['mycookie']) && $_COOKIE['mycookie']=="value1")
|| 9isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] =="value2" )) {
// do some stuff
}
Try this. It puts all requirements in one if statement:
if( (isset($_COOKIE['mycookie'] && $_COOKIE['mycookie']=="value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2']=="value2") ) {
// do some stuff
}
You can use one if condition instead of nested if. If you required to validate both then
if(isset($_COOKIE['mycookie'], $_COOKIE['mycookie2']) && ($_COOKIE['mycookie'] == "value1" && $_COOKIE['mycookie2']=="value2")) {
// do some stuff
}
Or if you have to validate one of them then
if((isset($_COOKIE['mycookie']) && $_COOKIE['mycookie']=="value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] == "value2") ) {
// do some stuff
}
I have a problem with a form check that use an if statement with multiple 'and' and 'or' operators. This check return me an anomalous occasionally false value.
public function insert_checkForm($form) {
$form = array_filter($form);
if (
!isset($form['report_id']) ||
!isset($form['date']) ||
!isset($form['technical_id']) ||
isset($form['travel_go_from']) != isset($form['travel_go_to']) ||
isset($form['work_go_from']) != isset($form['work_go_to']) ||
!isset($form['travel_go_from']) &&
!isset($form['travel_go_to']) &&
!isset($form['work_go_from']) &&
!isset($form['work_go_to'])
) {
return false;
} else {
return $form;
}
}
Last question, the above code changes compared to this (in spite of the priorities of and operators)?
[...]
!isset($form['report_id']) ||
!isset($form['date']) ||
!isset($form['technical_id']) ||
(isset($form['travel_go_from']) != isset($form['travel_go_to'])) ||
(isset($form['work_go_from']) != isset($form['work_go_to'])) ||
(!isset($form['travel_go_from']) && !isset($form['travel_go_to']) && !isset($form['work_go_from']) && !isset($form['work_go_to']))
[...]
Thanks =)
The most common problem with isset() is that it returns false when the item is NOT SET but also returns false if the item IS SET && IS NULL.
isset($arr['nonexisting']); //this returns: false
$arr['existing'] = null;
isset($arr['existing']); //this returns: false
I haven't been able to find a solution to this issue and hope that a another set of eyes may pick up on something. The else $confirm = line is not executed when the first "if" is not true and I can't figure out why.
if($_POST['zip'] && $_POST['country']=='USA' && !empty($fax)){
$plus4 = substr($_POST["zip"],6,4);
if (empty($plus4)) {
$link = 'Zip + 4';
$msg = "Your Zip + 4 was not provided! We cannot fax your Representative without your Zip+4.<br/>Click here to find your ".$link ;
print "<p style=\"color: red;\"><b>$msg<br/><br/></b></p>";
}
if (empty($_POST['state']) || empty($_POST['zip'])) $statezip = false ; // Check for State & Zip Required
else $statezip = true ;
//if (empty($state) || empty($zip) || empty($plus4)) $statezip = false ; // Check for State & Zip Required
$confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($_POST['address']) && !empty($_POST['city']) && !empty($_POST['country']) && $statezip == true ; // Verify required fields
}
else $confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($address) && !empty($city) && !empty($country) && $statezip == true ; // Verify required fields
Your else statement will always return false for $confirm due to the last condition:
&& $statezip == true
You are setting that value in the if statement, so it is undefined in the else statement.
Add your curly braces to the ELSE statement. Since you use them in the defining IF, the parser is expecting them.
else {
$confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($address) && !empty($city) && !empty($country) && $statezip == true ; // Verify required fields
}
use isset to check that variable exist,
function empty return true even if it contain 0 ,
when you are using else use brackets { too
else{do something}