0

I have two forms in my page. when I submit either of the form, both of them show the same validation error message. I used

if (isset ($_POST['click_cus_button']))

to check what button is licked in the from seems it does not work for me.

Can anyone tell me why this bit of code isn't working?

this is my controller (admin.php)

function add_users_data()
{

    if (isset ($_POST['click_cus_button']))// click_cus_button is set load this validation errors
    {
        $this->form_validation->set_rules('cus_name', 'Name', 'trim|required|min_length[5]');
        // validate name
        $this->form_validation->set_rules('cus_email', 'Email', 'trim|required|valid_email');
        // validate email
        $this->form_validation->set_rules('cus_phone', 'Phone Number', 'trim|required|min_length[10]|max_length[10]');
        // validate phone number
        $this->form_validation->set_rules('cus_password', 'Password', 'required|matches[cus_passconf]');
        // validate password
        $this->form_validation->set_rules('cus_passconf', 'Retype Password', 'required');
        // validate password
        if ($this->form_validation->run() == FALSE)// if from validation failed load following pages from views
        {

            $this->load->view('admin/admin_add_users.php');

        }
        else // if from validation success load following pages from views
        {
            $data = array(
                'cus_name' => $this->input->post('cus_name'),
                'cus_email' => $this->input->post('cus_email'),
                'cus_mobile' => $this->input->post('cus_mobile'),
                'cus_phone' => $this->input->post('cus_phone'),
                'cus_status' => $this->input->post('cus_status')
                //'database_field' => $this->input->post('text_box_name')
            );
            $this->mod_users->add_record($data);//add a comment
            $data['result']="Customer Added Successfully ";
            $this->load->view('admin/admin_add_users.php',$data);

        }
    }
    else if (isset ($_POST['click_oper_button']))// click_oper_button is set load this validation errors
    {
        $this->form_validation->set_rules('oper_firstname', 'Name', 'trim|required|min_length[5]');
        // validate name
        $this->form_validation->set_rules('oper_lastname', 'Name', 'trim|required|min_length[5]');
        // validate name
        if ($this->form_validation->run() == FALSE)// if from validation failed load following pages from views
        {

            $this->load->view('admin/admin_add_users.php');

        }
        else // if from validation success load following pages from views
        {

            $data['result']="Customer Added Successfully ";
            $this->load->view('admin/admin_add_users.php',$data);

        }
    }
}

this is my View (admin_add_users.php)

I removed some fields to make the code shorter in the view

 <?php  echo form_open('admin/add_users_data')   ?><!-- start of the form -->
    <div class="span8">
        <div style="padding-bottom:11px;">
            <div class="div add_cus box"><!-- start of the add customer add form -->
                <?php  echo validation_errors('<div class="notice marker-on-bottom bg-darkRed fg-white" id="bottom_form_error">', '</div>'); ?>
                <?php

                if(isset($result))
                {
                    echo '<div class="notice marker-on-bottom  bg-green fg-white">'.$result.'</div>';
                }

                ?>
                <!-- Display Validation error massages  -->
                <feildset>
                    <legend><i class="icon-user-2 on-right on-left"></i><strong>Add Customer</strong></legend>

                    <input name='cus_status' type='hidden' value='disabled'/>
                    <input type="submit" value="Add Customer" class="info" name="click_cus_button">
                </feildset>
            </div><!-- END of the add customer add form -->
        </div>
        <div class="div add_oper box"><!-- start of the add Operator add form -->
            <?php  echo validation_errors('<div class="notice marker-on-bottom bg-darkRed fg-white" id="bottom_form_error">', '</div>'); ?>
            <?php

            if(isset($result))
            {
                echo '<div class="notice marker-on-bottom  bg-green fg-white">'.$result.'</div>';
            }

            ?>
            <feildset>
                <legend><i class="icon-user on-right on-left"></i><strong>Add Operator</strong></legend>
                <h6>*All fields marked are required</h6>
                <lable>Name*</lable><br>
                <div class="input-control text size6" data-role="input-control">
                    <input type="text" name="oper_firstname" placeholder="First Name">
                </div>
                <div class="input-control text size6" data-role="input-control">
                    <input type="text" name="oper_lastname" placeholder="Last Name">
                </div>
                <br>
                <div class="input-control radio default-style" data-role="input-control">
                    <label>
                        Gender*<br>
                        <input type="radio" checked="" name="oper_gender">
                        <span class="check"></span>
                        Male
                    </label>
                </div>
                <div class="input-control radio default-style" data-role="input-control">
                    <label>
                        <input type="radio" name="oper_gender">
                        <span class="check"></span>
                        Female
                    </label>
                </div>
                <br>
                <lable>Date of Birth*</lable>
                <div class="input-control text" data-role="input-control">
                    <input id="datepicker" name="oper_dob" type="text">
                </div>
                <lable>Address*</lable><br>
                <div class="input-control text size1" data-role="input-control">
                    <input type="text" name="oper_add_no" placeholder="No">
                </div>

                <input type="submit" value="Add Operator" class="info" name="click_oper_button">
            </feildset>                
        </div><!-- END of the add Operator add form -->
        <div class="div add_admin">
            RadioButton 3 Selected
        </div>
    </div>
    <?php  echo form_close(); ?><!-- END of the form -->
Walter White
  • 86
  • 1
  • 1
  • 11
  • What is the error message ? – Rupam May 11 '14 at 14:16
  • no error message both of them show the same validation error message – Walter White May 11 '14 at 14:19
  • What's the validation error message? – Rwd May 11 '14 at 15:32
  • The Name field is required. The Email field is required. The Phone Number field is required. The Password field is required. The Retype Password field is required.------------ this is for the form Add Customer – Walter White May 11 '14 at 15:35
  • The Name field is required. The Name field is required.------------ this is for the form Add Operator – – Walter White May 11 '14 at 15:36
  • because they are both part of the same form it will submit both buttons at the same time so it will only ever process `if (isset ($_POST['click_cus_button']))`. You can either use 2 separate forms so that the 2 button names won't appear in the form submission. Or use some JS trickery to override the form submit and change a value of a hidden field based on on the button you click. – mic May 12 '14 at 14:43
  • mic i am trying to do this -http://stackoverflow.com/questions/12795818/multiple-form-validation-codeigniter-conflict/12798451#12798451 but it is not working for me – Walter White May 13 '14 at 14:18

0 Answers0