issue in form validation in php? - php

I have a form with several fields and with 4 radio buttons. Based on this radio button click, the form has to be validated. All the fields in the form need not be validated.
Check this link, the similar thing I want:
https://stackoverflow.com/questions/30396119/i-have-issue-in-php-validation-for-nested-if-else-condition
The code i tried:
<?php
$guestname="";$validguestname="";
$guestno= "";$validguestno="";
$nochild= 0;$validnochild="";
$noadults=0;$validnoadults="";
$infants =0;$validinfants="";
$noofperson ="";$validnoofperson="";
$check_in="";$validdate1="";
$check_out="";$validdate2="";
$noofnight = "";$validnoofnight="";
$room_type="";$validroom_type="";
$nfroom="";;$validnfroom="";
$noofextrabed ="";$validnoofextrabed="";
$roomarray="";$validroomarray="";
$mealplan="";$validmealplan="";
$rateperday ="";$validrateperday="";
$rateperbed ="";$validrateperbed="";
$total_amount ="";$validtotal_amount="";
$advance = "";$validadvance="";
$balance ="";$validbalance="";
$receiptno ="";$validreceiptno="";
$dateofremitance ="";$validdateofremitance="";
$bankorcash="";$validbankorcash="";
$bank_name="";$validbank_name="";
$bookedby="";$validbookedby="";
$paymentmode ="";$validpaymentmode="";
$bookingtype="";$validbookingtype="";
$reservationstatus="";$validreservationstatus="";
$book_by="" ;$validbook_by="";
$agentname= "";$validagentname="";
$agentphoneno="";$validagentphoneno="";
$error_message="";$error_message1="";$error_message2="";
$date3=date('Y-m-d');
if(isset( $_POST['submit']))
{
$guestname= $_POST['guest_name'];
$bookingtype=$_POST['booking_type'];echo $bookingtype."helooo";
$guestno= $_POST['guest_no'];
$reservationstatus=$_POST['reservation_status'];
$nochild= $_POST['no_child'];
$book_by= $_POST['bookby'];
$agentname= $_POST['bookbyother'];
$agentphoneno= $_POST['phone1'];
$noadults= $_POST['no_adults'];
$infants = $_POST['no_infants'];
$noofperson = $nochild+$noadults+$infants;
$check_in=trim( $_POST['checkin']);
$check_out=trim( $_POST['checkout']);
$noofnight = $_POST['no_of_days'];
$room_type= $_POST['roomtype'];
$nfroom= $_POST['noofroom'];
$noofextrabed = $_POST['no_of_extra_bed'];
$roomarray= $_POST["room_no"];
$mealplan= $_POST['meal_plan'];
$rateperday = $_POST['rp_day'];
$rateperbed = $_POST['extrarate'];
$total_amount = $_POST['totalamount'];
$advance = $_POST['advance_amount'];
$balance = $_POST['val_username'];
$receiptno = $_POST['receipt_no'];
$dateofremitance = $_POST['date_remitance'];
$bankorcash= $_POST['paymentmode'];
$bank_name= $_POST['bankname'];
$booking_date=date('y-m-d');
$bookedby= $_POST['adminbooking'];
$paymentmode = $_POST['payment_mode'];
$date1 = date('Y-m-d', strtotime($check_in));
$date2 = date('Y-m-d', strtotime($check_out));
///1st condition///
if($bookingtype=="direct" && $reservationstatus=="H" && other_conditions)
{
insert query;
}
///2nd condition///
else if($bookingtype=="direct" && $reservationstatus=="C" && othercondition for particular fields)
{insert query;}
///3rd condition///
else if($bookingtype=="agent" && $reservationstatus=="H" && $book_by=="other")
{echo "agent anfd holding and other";}
///4th condition///
else if($bookingtype=="agent" && $reservationstatus=="H" && $book_by!="other")
{echo "agent anfd holding and not toher";}
///5th condition///
else if($bookingtype=="agent" && $reservationstatus=="C" && $book_by=="other")
{echo "agent anfd confirm and other";}
///6th condition///
else if($bookingtype=="agent" && $reservationstatus=="C" && $book_by!="other")
{echo "agent anfd confirm and not other";}
//else condiition
else
{echo "choose ur option";}

From what I gather, your problem seems to be figuring out the full range of values that have been entered on the first. You can try one of two approaches.
Get your form values default options if they have not been entered by the user. As an example:
if (!empty($_POST['')) {
$reservationstatus=$_POST['reservation_status'];
}
else {
$reservationstatus=false;
}
then your following query will work:
if($bookingtype=="direct" && $reservationstatus=="H" && other_conditions)
{
your_query_here();
}
Check explicitly for entered values, but this makes for a more complex check.
if( ((!empty($_POST['booking_type']) &&
($_POST['booking_type']=="direct") ) &&
((!empty($_POST['reservation_status']) &&
($_POST['reservation_status']=="H") ) &&
other_conditions)
{
your_query_here();
}

Related

How to check the values in multiple text box already exist or not in codeigniter

$numrows = $this->input->post('no_of_rows');
for($i=1;$i<=$numrows;$i++)
{
$state_name = $_POST['proj_block_'.$i];
if(($state_name != '') && ($country != 0) && ($state == 0))
{
$ckds = $this->settings_model->get_ckds($state_name);
if(count($ckds) == 0)
{
$this->settings_model->insert_state_details($country,$state,$sid,$cid);
}
}
else
{
if(($country != 0) && ($state != 0) && ($state_name != ''))
{
$ckdc = $this->settings_model->get_ckdc($state_name);
if(count($ckdc) == 0)
{
$this->settings_model->insert_state_details($country,$state,$sid,$cid);
}
}
}
}
I have included multiple text box proj_block_$i and need to check the value i have entered is alredy exist or not. it is only checking the condition for last row. if i already entered the value ABC in a table then i enter the same ABC value in text box and try to insert but it is not saving, again i try to insert the value ABC and DEF, both value is inserted. I need to avoid the insertion of ABC value. any one please help me to fix this issue.

Does anyone know how to display one field condition in php

I have a situation where I need to compare 3 different select option fields of the same table. I compared 3 fields, 2 fields and 1 field. The corresponding results are displayed, but in one field condition (category) result are not displayed.
<?php
include'connect.php';
if(isset($_POST['submit']))
{
$qn=$_POST['location'];//LOCATION
$qn1=$_POST['category'];//CATEGORY
$qn2=$_POST['salary'];//SALARY
if(isset($_POST['location']))
{
$q2=mysql_query("SELECT * FROM job_posting where location='$qn'");
while($quew=mysql_fetch_array($q2))
{
echo $ans=$quew['title'];
}
}
else
{
$qir=mysql_query("SELECT * FROM job_posting where category='$qn1'");
while($quew=mysql_fetch_array($qir))
{
echo $gn=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']))
{
$q3=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1'");
while($quew=mysql_fetch_array($q3))
{
echo $ans2=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']) && isset($_POST['salary']))
{
$q4=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1' && minsalary='$qn2'");
while($quew=mysql_fetch_array($q4))
{
echo $ans3=$quew['title'];
}
}
if(isset($_POST['location']) || isset($_POST['category']) && isset($_POST['salary']))
{
$q5=mysql_query("SELECT * FROM job_posting where location='$qn' || category='$qn1' && minsalary='$qn2'");
while($quew=mysql_fetch_array($q5))
{
echo $ans4=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']) || isset($_POST['salary']))
{
$q7=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1' || minsalary='$qn2'");
while($quew=mysql_fetch_array($q7))
{
echo $ans5=$quew['title'];
}
}
}
?>
For a start, I can think of three things
Database Query => check if request the "category"
Database Type => utf8-bin, utf8-general-ci etc.. because is very important how are saved data.
Server file "php.ini" => check "default_charset" and "mbstring..." params, is important to manage string type data.
If you need more help only need send we more information
Try this:
if (!empty($_POST['location'])) {
$lq = "&& location='$_POST['location']'";
}
if (!empty($_POST['category'])) {
$cq = "&& category='$_POST[category]'";
}
if (!empty($_POST['salary'])) {
$sq = "&& minsalary='$_POST[salary]'";
}
$qr = mysql_query("SELECT * FROM job_posting WHERE 1 $lq $cq $sq");
while ($rs = mysql_fetch_array($qr)) {
// do whatever you want
}

Wordpress execute edit_user_profile_update before user_profile_update_errors

I have a problem with wordpress userprofile editing.
The problem is that the validate function is executing after the update function so what i get on my wordpress is the error but the fields updated.
function bw_admin_validate_fields($errors, $update, $user){
if(isset($_POST["credits"]) && !ctype_digit($_POST['credits'])){
$errors->add("credits_error","The field credits must be a number");
}
if(isset($_POST['ipaddress']) && !filter_var($ip, FILTER_VALIDATE_IP)){
$ip = gethostbyname($_POST['ipaddress']);
if($ip == $_POST['ipaddress'])
$errors->add("ip_error","You must enter a valid ip address or a hostname");
}
}
.
function bw_admin_save_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) || !ImAdmin() )
return false;
if(isset($_POST['generate']) && isset($_POST["ipaddress"])){
$ip = $_POST['ipaddress'];
if(!filter_var($ip,FILTER_VALIDATE_IP)) $ip = gethostbyname($_POST['ipaddress']);
$key = API::I()->createApi($ip);
update_user_meta($user_id,"api_key",$key);
}
else{
$apikey = trim(get_user_meta($user_id,'api_key',true));
$banned = isset($_POST['banned']) && $_POST['banned'] ? true : false;
if($banned) API::I()->Ban($apikey);
else API::I()->UnBan($apikey);
if(isset($_POST['credits']) && ctype_digit($_POST['credits']))
{
if(((int)$_POST['credits']) < 0) $_POST['credits'] = 0;
API::I()->setCredits($_POST['credits'],$apikey);
}
}
}
And when i hit the edit button wordpress says me that i have to enter a valid ip or valid hostname but the Api is already created.

Another PHP if/else not working as expected

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}

Logical operators issue

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
}
}

Categories