doupa1883 2015-05-24 10:44
浏览 22
已采纳

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";}
  • 写回答

1条回答 默认 最新

  • dongnang8192 2015-05-24 15:58
    关注

    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.

    1. 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();
    }
    
    1. 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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)