Codeigniter - 表格

时间:2016-06-16 07:50:11

标签: javascript php codeigniter

我的观点文件:new_request.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>New Request</title>
    <script type="text/javascript">
     window.onload = function() {
        document.getElementById('ifYes').style.display = 'none';
        document.getElementById('ifNo').style.display = 'none';
    }
    function yesnoCheck() {
        if (document.getElementById('yesCheck').checked) {
            document.getElementById('ifYes').style.display = 'block';
            document.getElementById('ifNo').style.display = 'none';
        } 
        else if(document.getElementById('noCheck').checked) {
            document.getElementById('ifNo').style.display = 'block';
            document.getElementById('ifYes').style.display = 'none';
       }
    }

    </script>

</head>
<body>
    <h3>Make a new Request!</h3>

    <?php
        echo form_open('requests/new_function');
        echo validation_errors();
        echo form_label('Service : ', 'service[]');
        $options = array(
            'facial' => 'Facial',
            'threading' => 'Threading',
            'spa' => 'Spa',
            'service' => 'Service'
            );
        echo "<br>";
        echo form_multiselect('service[]', $options);
        echo "<br>";
        echo form_label('Time(hh:mm:ss) : ', 'time');
        echo form_input('time', '');
        echo "<br>";
        echo form_label('Date(dd-mm-yyyy) : ', 'date');
        echo form_input('date', '');
        echo "<br>";

        echo form_label('Address : ', 'address');
        ?>
        My Address
        <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="yesCheck"/>
        New Address
        <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="noCheck"/>
        <br>
        <div id="ifYes" style="display:none">
            <?php
            echo form_label('Street : ', 'street');
            echo form_input('street', $street);
            echo "<br>";
            echo form_label('City : ', 'city');
            echo form_input('city', $city);
            echo "<br>";
            echo form_label('State : ', 'state');
            echo form_input('state', $state);
            echo "<br>";
            echo form_label('PinCode : ', 'pincode');
            echo form_input('pincode', $pincode);
            echo "<br>";
            ?>
        </div>

        <div id="ifNo" style="display:none">
            <?php
            echo form_label('Street : ', 'street');
            echo form_input('street', '');
            echo "<br>";
            echo form_label('City : ', 'city');
            echo form_input('city', '');
            echo "<br>";
            echo form_label('State : ', 'state');
            echo form_input('state', '');
            echo "<br>";
            echo form_label('PinCode : ', 'pincode');
            echo form_input('pincode', '');
            echo "<br>";
            ?>
        </div>

        <?php



        echo "<br>";
        //adding CAPTCHA
            $random_number = substr(number_format(time() * rand(),0,'',''),0,6);
            $vals = array(
                'word' => $random_number,
                'img_path' => './captcha/',
                'img_url' => base_url().'captcha/',
                'img_width' => 140,
                'img_height' => 32,
                'expiration' => 7200
                );
            $cap = create_captcha($vals);
            echo $cap['image'];
            ?>
            <br>
            <?php 
            $this->session->set_userdata('captchaWord', $cap['word']);
            echo form_label('Please fill in the string above : ', 'captcha');
            echo form_input('captcha', '');
            echo "<br>";
        echo form_submit('submit-button-request', 'Submit Request');
        echo form_close();
    ?>


</body>
</html>

问题是$ this-&gt; input-&gt; post('street')和类似'city','state','pincode'不起作用。它返回空字段。 'ifYes'div是从数据库中预先填充的。

这是控制器文件:Requests.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Requests extends CI_Controller {

    public function new_req()
    {
        $this->load->model('model_users');
        $object = $this->model_users->getRow($this->session->userdata('email'));
        //var_dump($object);die();
        $data['street'] = $object[0]->street;
        $data['city'] = $object[0]->city;
        $data['state'] = $object[0]->state;
        $data['pincode'] = $object[0]->pincode;
        $this->load->view('new_request', $data);
    }

    public function new_function()
    {
        $this->form_validation->set_rules('service[]', 'Service', 'required');
        // $this->form_validation->set_rules('street', 'Street', 'required');
        // $this->form_validation->set_rules('city', 'City', 'required');
        // $this->form_validation->set_rules('state', 'State', 'required');
        // $this->form_validation->set_rules('pincode', 'Pincode', 'required');
        $this->form_validation->set_rules('time', 'Time', 'required|regex_match[/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/]');
        $this->form_validation->set_rules('date', 'Date', 'required|regex_match[/^[0-9]{2}-[0-9]{2}-[0-9]{4}$/]');
        $this->form_validation->set_rules('captcha', 'Captcha', 'required|callback_checkCaptcha');
        $date = $this->input->post('date');
        $time = $this->input->post('time');
        $date_time = nice_date($date, 'Y-m-d') . " " . $time;
        $service = implode(',' , $this->input->post('service[]'));
        $data = array(
            'email_customer' => $this->session->userdata('email'),
            'service' => $service,
            'date_time' => $date_time,
            'street' => $this->input->post('street'),
            'city' => $this->input->post('city'),
            'state' => $this->input->post('state'),
            'pincode' => $this->input->post('pincode'),
            );
        $this->load->model('model_users');

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('new_request');
        }
        else
        {
            $this->model_users->insert_request($data);
            redirect('main/members');
        }

    }

    public function old_req()
    {

    }

    public function checkCaptcha($captcha)
    {
        if($this->session->userdata('captchaWord') == $captcha)
        {
            return true;
        }
        else
        {
            $this->form_validation->set_message('checkCaptcha', 'Please fill in the Captcha String Correctly!');
            return false;
        }
    }

}

1 个答案:

答案 0 :(得分:1)

问题是您正在重复字段名称。提交表单时,任何两次命名的字段的最后一个将是发布的字段。换句话说,只有'isNo'Div被服务器接受并放入$ _POST。

解决方案是重命名一组字段或另一组字段,即在'isNo'div中使用“new_”前缀所有字段名称,即new_street,new_city等。