简体   繁体   中英

How to Submit the form without reloading the page with codeigniter

This is my form code which i was using with code ignitor

   <div id="tab_register_content"class="content-form hidden">

        <?php echo form_open('renty/sign_up_user')?>

          <div>
            <?php echo form_error('register_email');?>
            <input id="register_email"class="input_placeholder email"type="text"value=""placeholder="Email address"name="register_email"/>
          </div>
          <div>
            <?php echo form_error('password');?>
            <input id="register_name" class="password" type="password" value="" name="password"  onfocusout="get_form_value_from_user()"/>
          </div>


          <div>
            <input id="register_remember_me_checkbox"type="checkbox"class="styled"name="remember_me"value="1"/>
            <label for="register_remember_me_checkbox">
              Remember me next time
            </label>
          </div>

          <input class="admin-form-submit orange_button"type="submit"value="Continue"/>

          <div class="admin_form_link">
            <span class="sign_in">
              <a class="tab_link_button"href="#sign_in"title="">
                Already registered?
              </a>
            </span>for
          </div>
        </form>
      </div>

I was using this form and i want to submit the from without reloading the page i was trying to the ajax code but its not working any suggestions? i commented the form_open line

   <?php //echo form_open('renty/sign_up_user')?>

and then tried it with ajax but it's not working

   <script type="text/javascript">

  function get_form_value_from_user(){

    var email = $(".email").val();
    var password = $(".password").val();

    if(email != "" && password != ""){

       $.ajax({
          url: '<?php echo base_url(); ?>/index.php/renty/sign_up_user?email='+email+'&password='+password,
          success: 'Working'
      });

    }

  }

</script>

My Controller

   public function sign_up_user(){

    $this->form_validation->set_rules('register_email','Register Email','required|valid_email|is_unique[sign_up.email]');
    $this->form_validation->set_rules('password','Password','required|md5');

    if($this->form_validation->run() == FALSE){

        $this->load->view('application/index');

    }

    else

    {

        $data['email']    = $_GET['email'];
        $data['password'] = $_GET['password'];

        $this->load->model('renty/db_data');
        $this->db_data->insert($data);
        $this->home();

    }


}

There is no need to add renty/sign_up_user to your form open action. url attributes does the work in jquery.

If you remove <?php echo form_open('renty/sign_up_user')?> and replace with <?php echo form_open('')?> it will solve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM