usman610
usman610

Reputation: 519

if fields empty or !empty run the query and dont display error for empty field

i have 3 different forms in 3 accordions but the query is one. if (!empty($_POST) this query, then submit the form. But it shows the error for the other forms that online blah blah blah is error. It shows this error for empty fields.

Is there a way to hide this error message for empty fields?

if(!empty($_POST)){

        $sql = "
            INSERT INTO 
            leave_req
            (
              `lvm_type`,
              `lvm_do`,
              `lvm_db`,
              `lvm_td`,
              `lvc_type`,
              `lvc_do`,
              `lvc_db`,
              `lvc_td`,
              `lvo_type`,
              `lvo_do`,
              `lvo_db`,
              `lvo_td`,
              `lvn_type`,
              `lvn_do`,
              `lvn_db`,
              `lvn_td`,
              `lv_reason`,
              `lv_usign`,
              `lv_rsign`,
              `lv_asign`,
              `lv_sec`,
              `lv_ip`,
              `lv_cdate`
            )
            values
            (
                '".addData($_POST['altype'])."',
                '".addData($_POST['aleave1'])."',
                '".addData($_POST['aleave2'])."',
                '".addData($_POST['altotal'])."',
                '".addData($_POST['cltype'])."',
                '".addData($_POST['cleave1'])."',
                '".addData($_POST['cleave2'])."',
                '".addData($_POST['cltotal'])."',
                'null',
                'null',
                'null',
                'null',
                'null',
                'null',
                'null',
                'null',
                'null',
                'null',
                'null',
                'null',
                'null',
                '".$ip."',
                now()
            )";
         echo "message submitted successfully";
         } else {
         echo "check your form"; }

Upvotes: 2

Views: 1262

Answers (2)

user188654
user188654

Reputation:

A nice way of solving this problem is by using isset() combined with the ternary operator. Basically your ternary operator will evaluate to one of the given values, either the appropriate $_POST field or null. If there is a set $_POST field it will be made part of the query and if not null will be used instead.

<?php
if(!empty($_POST)){
    $sql = "
        INSERT INTO 
        leave_req
        (
          `lvm_type`,
          `lvm_do`,
          `lvm_db`,
          `lvm_td`,
          `lvc_type`,
          `lvc_do`,
          `lvc_db`,
          `lvc_td`,
          `lvo_type`,
          `lvo_do`,
          `lvo_db`,
          `lvo_td`,
          `lvn_type`,
          `lvn_do`,
          `lvn_db`,
          `lvn_td`,
          `lv_reason`,
          `lv_usign`,
          `lv_rsign`,
          `lv_asign`,
          `lv_sec`,
          `lv_ip`,
          `lv_cdate`
        )
        values
        (
            '" . (isset($_POST['altype']) ? addData($_POST['altype']) : 'null') . "',
            '" . (isset($_POST['aleave1']) ? addData($_POST['aleave1']) : 'null') . "',
            '" . (isset($_POST['aleave2']) ? addData($_POST['aleave2']) : 'null') . "',
            '" . (isset($_POST['altotal']) ? addData($_POST['altotal']) : 'null') . "',
            '" . (isset($_POST['cltype']) ? addData($_POST['cltype']) : 'null') . "',
            '" . (isset($_POST['cleave1']) ? addData($_POST['cleave1']) : 'null') . "',
            '" . (isset($_POST['cleave2']) ? addData($_POST['cleave2']) : 'null') . "',
            '" . (isset($_POST['cltotal']) ? addData($_POST['cltotal']) : 'null') . "',
            'null',
            'null',
            'null',
            'null',
            'null',
            'null',
            'null',
            'null',
            'null',
            'null',
            'null',
            'null',
            'null',
            '".$ip."',
            now()
        )";
        echo "message submitted successfully";
 } else {
    echo "check your form"; }
 ?>

Upvotes: 1

echo_Me
echo_Me

Reputation: 37243

try this

if (isset($_POST['form1']) or
    isset($_POST['form2']) or
    isset($_POST['form3']) )
{
    ...
}

Upvotes: 1

Related Questions