提交时,页面在同一页面上不重定向?

时间:2017-07-01 07:56:03

标签: php codeigniter captcha

controller:test.php

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class Test extends CI_Controller 
    {
        function __construct() 
        {
            parent :: __construct();
            $this->load->helper(array('form', 'url', 'captcha'));
            $this->load->model('Fetch_data');
            $this->session->set_flashdata('');
        }
        public function login()
        {
            if($this->input->post('signup'))
            {
                $data  = array(
                                'firstname' => $this->input->post('fname'),
                                'lastname' => $this->input->post('lname'),
                                'email' => $this->input->post('email'),
                                'phone' => $this->input->post('mobile'),
                                'city' => $this->input->post('city'),
                                'interest_field' => $this->input->post('interest_field'),
                                'password' => $this->input->post('password')
                              );


                $this->db->select('*');
                $this->db->from('students');
                $where = "email = '".$this->input->post('email')."'";
                $this->db->where($where);
                $query = $this->db->get();
                if ($query->num_rows() > 1)
                {
                    $this->session->set_flashdata('message', '<p style="color: red;font-weight: bold;">Email Id already exist. Please login with diffrent email id.</p>');
                }
                else
                {
                    $inputCaptcha = $this->input->post('captcha');
                    $sessCaptcha = $this->session->userdata('captchaCode');
                    if($inputCaptcha === $sessCaptcha)
                    {
                        return $this->db->insert('students',$data);
                        $this->session->set_flashdata('message', '<p style="color: #89c343;font-weight: bold;">Successfull.</p>');
                    }
                    else
                    {
                        $this->session->set_flashdata('message', '<p style="color: red;font-weight: bold;">Captcha code was not match, please try again.</p>');
                    }
                }
            }
            $config = array(
                'img_path'      => 'resources/img/captcha_images/',
                'img_url'       => base_url().'resources/img/captcha_images/',
                'img_width'     => '200',
                'img_height'    => 40,
                'word_length'   => 8,
                'font_size'     => 16
            );
            $captcha = create_captcha($config);
            $this->session->unset_userdata('captchaCode');
            $this->session->set_userdata('captchaCode',$captcha['word']);
            $data['captchaImg'] = $captcha['image'];
            $this->load->view('login',$data);
        }
    }

view:login.php

<?php echo form_open(base_url('index.php/')."test/login");?>

    <input type="text" name="fname" id="fname" placeholder="Your First Name" class="text-line"/>

    <input type="text" name="email" id="email" placeholder="Your Email" class="text-line"/>

    <input type="text" name="mobile" id="mobile" placeholder="Your Mobile" class="text-line"/>

    <p id="captImg"><?php echo $captchaImg; ?></p>
    <input type="text" name="captcha" value="" class="captcha" placeholder="Enter Image Inputs"/>
    <a href="javascript:void(0);" class="refreshCaptcha" >Can't Read Please Refersh Image</a>
    <input type="submit" name="signup" id="signup" value="Sign Up" class="btn btn-warning"/>
<?php echo form_close(); ?>

在这段代码中,我创建了一个具有验证码的表单,其中验证码和电子邮件ID已经存在,但是当我将值插入学生表时。表单值将插入到数据库中,但页面不会在登录页面上重定向。现在,我希望在单击“提交”按钮时,它会在表中插入值并显示成功消息。

谢谢

3 个答案:

答案 0 :(得分:0)

  

View.login.php

<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

    <input type="text" name="fname" id="fname" placeholder="Your First Name" class="text-line"/>

    <input type="text" name="email" id="email" placeholder="Your Email" class="text-line"/>

    <input type="text" name="mobile" id="mobile" placeholder="Your Mobile" class="text-line"/>

    <p id="captImg"><?php echo $captchaImg; ?></p>
    <input type="text" name="captcha" value="" class="captcha" placeholder="Enter Image Inputs"/>
    <a href="javascript:void(0);" class="refreshCaptcha" >Can't Read Please Refersh Image</a>
    <input type="submit" name="signup" id="signup" value="Sign Up" class="btn btn-warning"/>

</form>

答案 1 :(得分:0)

使用

redirect('controllerName/function_name_where_you_want_to_redirect');

如果在仅包含

的控制器中创建单独的index()函数会更好
$this->load->view('view_name');

然后重定向到已经提到的那个功能。

希望有所帮助

答案 2 :(得分:0)

要在提交表单后重定向到同一页面,您不需要在 form_open()函数中传递任何参数。