CodeIgniter一个在动态表单上提交

时间:2017-02-07 21:38:12

标签: php forms codeigniter

我正在尝试创建一个需要输入的表单,例如。 3然后根据输入方式在循环中生成3个表单。有人可以看看这个,看看我的代码是否正确?目前我的表单验证由于某种原因立即失败,我不确定如何继续。

有成人表格和儿童表格。两者都取决于另一页的输入。

这是我的观点

<?php echo form_open_multipart('Public/Details/next','class="inputform"');?>
                    <h3>Adults</h3>
                    <?php for($i=0;$i<$adults;$i++){?>
                        <input type="hidden" class="form-control" name="adult" value="<?php echo $adults;?>">
                        <label for="inputfirstname">Firstname</label>
                        <input type="text" class="form-control" name="inputfirstname[]" placeholder="Firstname" value="<?php echo set_value('inputfirstname['.$i.'],""');?>">
                        <!-- <div class="errormessage"><?php echo form_error('inputfirstname['.$i.']'); ?></div> -->
                        <label for="inputfirstname">Lastname</label>
                        <input type="text" class="form-control" name="inputlastname[]" placeholder="Lastname" value="<?php echo set_value('inputlastname[$i]');?>">
                        <!-- <div class="errormessage"><?php echo form_error('inputlastname['.$i.']'); ?></div> -->
                        <label for="inputdateofbirth">Date of Birth</label>
                         <input type="date" class="form-control" name="inputdateofbirth[]" value="<?php echo set_value('inputdateofbirth[$i]');?>">
                         <!-- <div class="errormessage"><?php echo form_error('inputdateofbirth['.$i.']'); ?></div> -->
                        <label for="inputicnumber">IC Number</label>
                        <input type="text" class="form-control" name="inputicnumber[]" placeholder="IC Number" value="<?php echo set_value('inputicnumber[$i]');?>">
                        <!-- <div class="errormessage"><?php echo form_error('inputicnumber['.$i.']'); ?></div> -->
                         <label for="inputpassportnumber">Passport Number</label>
                        <input type="text" class="form-control" name="inputpassportnumber[]" placeholder="Passport Number" value="<?php echo set_value('inputpassportnumber[$i]');?>">
                        <!-- <div class="errormessage"><?php echo form_error('inputpassportnumber['.$i.']'); ?></div> -->
                        <label for="inputmobilenumber">Mobile Number</label>
                        <input type="text" class="form-control" name="inputmobilenumber[]" placeholder="Mobile Number" value="<?php echo set_value('inputmobilenumber[$i]');?>">
                        <!-- <div class="errormessage"><?php echo form_error('inputmobilenumber['.$i.']'); ?></div> -->
                        <label for="inputemail">Email</label>
                        <input type="text" class="form-control" name="inputemail[]" placeholder="Email" value="<?php echo set_value('inputemail[$i]');?>">
                        <!-- <div class="errormessage"><?php echo form_error('inputemail['.$i.']'); ?></div> -->
                        <label for="inputconfirmemail">Confirm Email</label>
                        <input type="text" class="form-control" name="inputconfirmemail[]" placeholder="Confirm Email" value="<?php echo set_value('inputconfirmemail[$i]');?>">
                        <!-- <div class="errormessage"><?php echo form_error('inputconfirmemail['.$i.']'); ?></div> -->
                        <label for="inputaddress1">Address</label>
                        <input type="text" class="form-control" name="inputaddress1[]" placeholder="Address 1" value="<?php echo set_value('inputaddress1[$i]');?>">
                        <input type="text" class="form-control" name="inputaddress2[]" placeholder="Address 2" value="<?php echo set_value('inputaddress2[$i]');?>">
                        <input type="text" class="form-control" name="inputaddress3[]" placeholder="Address 3" value="<?php echo set_value('inputaddress3[$i]');?>">
                        <input type="text" class="form-control" name="inputaddress4[]" placeholder="Address 4" value="<?php echo set_value('inputaddress4[$i]');?>">
                        <input type="text" class="form-control" name="inputaddress5[]" placeholder="Address 5" value="<?php echo set_value('inputaddress5[$i]');?>">
                        <label for="inputpostcode">Postcode</label>
                        <input type="text" class="form-control" name="inputpostcode[]" placeholder="Postcode1" value="<?php echo set_value('inputpostcode[$i]');?>">
                        <!-- <div class="errormessage"><?php echo form_error('inputpostcode['.$i.']'); ?></div> -->
                <?php } ?>

                <?php if($children>0){ ?>
                <h3>Children</h3>
                <?php for($j=0;$j<$children;$j++){ ?>
                    <input type="hidden" class="form-control" name="children" value="<?php echo $children;?>">
                    <label for="inputchildfirstname">Firstname</label>
                    <input type="text" class="form-control" name="inputchildfirstname[]" value="<?php echo set_value('inputchildfirstname[$j]');?>" placeholder="Firstname">
                    <label for="inputchildlastname">Lastname</label>
                    <input type="text" class="form-control" name="inputchildlastname[]" value="<?php echo set_value('inputchildlastname[$j]');?>" placeholder="Lastname">
                    <label for="inputchilddateofbirth">Date of Birth</label>
                    <input type="date" class="form-control" name="inputchilddateofbirth[]" value="<?php echo set_value('inputchilddateofbirth[$j]');?>">
                    <label for="inputchildpassportnumber">Passport Number</label>
                    <input type="text" class="form-control" name="inputchildpassportnumber[]" placeholder="Passport Number" value="<?php echo set_value('inputchildpassportnumber[$j]');?>">
                    <div class="errormessage"><?php echo form_error('inputchildpassportnumber['.$j.']'); ?></div>
                <?php }} ?>
                    <p><button type="submit" class="btn btn-primary">Next</button></p>
                    <p><a href="<?php echo site_url();?>Welcome">Cancel</a></p>
                <?php echo form_close(); ?>
                <p><?php echo $this->session->flashdata('Form'); ?></p>

控制器

public function next(){
        $adultlength = $this->input->post('adults'); 
        $childrenlength = $this->input->post('children'); 
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
        if($adultlength>0){
            for ($i=0; $i <$adultlength; $i++) { 
                $this->form_validation->set_rules('inputfirstname['.$i.']', 'Firstname', 'trim|required');
                $this->form_validation->set_rules('inputlastname['.$i.']', 'Lastname', 'trim|required');
                $this->form_validation->set_rules('inputdateofbirth['.$i.']', 'Date of Birth', 'trim|required');
                $this->form_validation->set_rules('inputicnumber['.$i.']', 'IC Number', 'trim|required');
                $this->form_validation->set_rules('inputpassportnumber['.$i.']', 'Passport Number', 'trim|required');
                $this->form_validation->set_rules('inputmobilenumber['.$i.']', 'Mobile Number', 'trim|required');
                $this->form_validation->set_rules('inputemail['.$i.']', 'Email', 'trim|required');
                $this->form_validation->set_rules('inputconfirmemail['.$i.']', 'Confirm Email', 'trim|required');

                $inputfirstname[] =  $this->input->post('inputfirstname['.$i.']');
                $inputlastname[] =  $this->input->post('inputlastname['.$i.']');
                $inputdateofbirth[] =  $this->input->post('inputdateofbirth['.$i.']');
                $inputicnumber[] =  $this->input->post('inputicnumber['.$i.']');
                $inputpassportnumber[] =  $this->input->post('inputpassportnumber['.$i.']');
                $inputmobilenumber[] =  $this->input->post('inputmobilenumber['.$i.']');
                $inputemail[] =  $this->input->post('inputemail['.$i.']');
                $inputaddress1[] =  $this->input->post('inputaddress1['.$i.']');
                $inputaddress2[] =  $this->input->post('inputaddress2['.$i.']');
                $inputaddress3[] =  $this->input->post('inputaddress3['.$i.']');
                $inputaddress4[] =  $this->input->post('inputaddress4['.$i.']');
                $inputaddress5[] =  $this->input->post('inputaddress5['.$i.']');
                $inputpostcode[] =  $this->input->post('inputpostcode['.$i.']');    
            }
        }


        if($childrenlength>0){
                for($j=0;$j<$childrenlength;$j++){
                    $this->form_validation->set_rules('inputchildpassportnumber['.$j.']', 'Passport Number', 'trim|required');
                    $inputchildfirstname[] =  $this->input->post('inputchildfirstname['.$j.']');
                    $inputchildlastname[] =  $this->input->post('inputchildlastname['.$j.']');
                    $inputchilddateofbirth[] =  $this->input->post('inputchilddateofbirth['.$j.']');
                    $inputchildpassportnumber[] =  $this->input->post('inputchildpassportnumber['.$j.']');
                }
        }

        if($this->form_validation->run()==false){
            $data['productdetail'] = $this->ProductsModel->getProduct($this->session->userdata('productid'));
            $data['adults'] = $this->session->userdata('adults');
            $data['children'] = $this->session->userdata('children');
            $this->load->view('public/publicMenu/navigationLink');
            $this->load->view('public/publicDetails/details',$data);
            $this->load->view('public/publicMenu/navigationJquery');
        }else{
            $transactionid = $this->generateRandomString(10);
            if($adultlength>0){
                for ($i=0; $i <$adultlength; $i++) { 
                    $passengerdetails[] = array(
                        'firstname'=>$inputfirstname[$i],
                        'lastname'=>$inputlastname[$i],
                        'email'=>$inputemail[$i],
                        'phone'=>$inputmobilenumber[$i],
                        'accessLevel'=>'1',
                        'dateofbirth'=>$inputdateofbirth[$i],
                        'icnumber'=>$inputicnumber[$i],
                        'passportnumber'=>$inputpassportnumber[$i],
                        'address'=>$inputaddress1[$i]." ".$inputaddress1[$i]." ".$inputaddress1[$i]." ".$inputaddress1[$i]." ".$inputaddress1[$i]." ".$inputpostcode[$i],
                        'usertype'=>'adult',
                        'transactionid'=>$transactionid
                    );
                }

            }
            if($childrenlength>0){
                    for($j=0;$j<$childrenlength;$j++){
                        $childpassengerdetails[] = array(
                            'firstname'=>$inputchildfirstname[$j],
                            'lastname'=>$inputchildlastname[$j],
                            'email'=>'',
                            'phone'=>'',
                            'accessLevel'=>'1',
                            'dateofbirth'=>$inputchilddateofbirth[$j],
                            'icnumber'=>'',
                            'passportnumber'=>$inputchildpassportnumber[$j],
                            'address'=>'',
                            'usertype'=>'child',
                            'transactionid'=>$transactionid
                        );
                    }

            }
            $this->session->set_userdata('passengerdetails',json_encode($passengerdetails));
            $this->session->set_userdata('childpassengerdetails',json_encode($childpassengerdetails));
            redirect('/Public/Payment');
        }
    }

我希望有人可以帮助我。我已经浏览了很长时间,我看不出我错过了什么,或者整个事情都错了。

1 个答案:

答案 0 :(得分:0)

我为您创建了一个简单的控制器和视图。

控制器:

public function test()
    {
        $view_data = array();
        $view_data['step'] = 1; // default step
        //step 1
        if($this->input->post('_step') AND $this->input->post('_step')==1) {
            $view_data['step'] = 1;
            $this->form_validation->set_rules('children_length', 'How many children', 'trim|required|integer|min_length[1]');
            $this->form_validation->set_rules('adult_length', 'How many adult', 'trim|required|integer|min_length[1]');

            if($this->form_validation->run()) {
                //set variable for status 2
                $view_data['step'] = 2;
            }
        }

        //step 2
        if($this->input->post('_step') AND $this->input->post('_step')==2) {
            $view_data['step'] = 2;

            $children_length = (int)$this->input->post('children_length');
            $adult_length = (int)$this->input->post('adult_length');

            //validation for childrens
            for($i=0;$i<$children_length;$i++) {
                $this->form_validation->set_rules('childrens['.$i.'][firstname]', 'Children '.$i.' - firstname', 'trim|required');
                $this->form_validation->set_rules('childrens['.$i.'][lastname]', 'Children '.$i.' - lastname', 'trim|required');
            }
            //validation for adults
            for($i=0;$i<$children_length;$i++) {
                $this->form_validation->set_rules('childrens['.$i.'][firstname]', 'Children '.$i.' - firstname', 'trim|required');
                $this->form_validation->set_rules('childrens['.$i.'][lastname]', 'Children '.$i.' - lastname', 'trim|required');
            }

            if($this->form_validation->run()) {
                //set variable for status 3
                //here save etc
                $view_data['step'] = 3;
            }
        }


        $this->load->view('/test', $view_data);
    }

查看:

<?php 

if($step==3) {
    //step 3 - show array from step 2
    echo '<h3>Step 3</h3>'; 
    echo '<pre>';
        print_r($this->input->post());
    echo '</pre>';
}elseif($step==2) {
    //step 2 Generation form fo childrens and adults
    echo '<h3>Step 2</h3>';
    echo form_open('', '', ['_step'=>2,'children_length'=>$this->input->post('children_length'),'adult_length'=>$this->input->post('adult_length')]);
        echo '<div style="color:red">'.validation_errors().'</div>';

        echo '<h4>Childrens</h4>';
        for($i=0;$i<$this->input->post('children_length');$i++) {
            echo form_input(['type'=>'text','name'=>'childrens['.$i.'][firstname]','value'=>set_value('childrens['.$i.'][firstname]'),'placeholder'=>'Children '.$i.' - firstname']);
            echo form_input(['type'=>'text','name'=>'childrens['.$i.'][lastname]','value'=>set_value('childrens['.$i.'][lastname]'),'placeholder'=>'Children '.$i.' - lastname']);
            echo '<br><br>';
        }

        echo '<h4>Adults</h4>';
        for($i=0;$i<$this->input->post('adult_length');$i++) {
            echo form_input(['type'=>'text','name'=>'adults['.$i.'][firstname]','value'=>set_value('adults['.$i.'][firstname]'),'placeholder'=>'Adult '.$i.' - firstname']);
            echo form_input(['type'=>'text','name'=>'adults['.$i.'][lastname]','value'=>set_value('adults['.$i.'][lastname]'),'placeholder'=>'Adult '.$i.' - lastname']);
            echo '<br><br>';
        }
        echo form_input(['type'=>'submit','value'=>'Next (go to step 3)']);
    echo form_close();
}else {
    //step 1 How many children as adults
    echo '<h3>Step 1</h3>';
    echo form_open('', '', ['_step'=>1]);
        echo '<div style="color:red">'.validation_errors().'</div>';
        echo form_input(['type'=>'text','name'=>'children_length','value'=>set_value('children_length'),'placeholder'=>'How many children']);
        echo form_input(['type'=>'text','name'=>'adult_length','value'=>set_value('adult_length'),'placeholder'=>'How many adult']);
        echo form_input(['type'=>'submit','value'=>'Next (go to step 2)']);
    echo form_close();
}
?>