douzhan4522 2015-12-01 13:57
浏览 53

Codeigniter jquery ajax mysql表单

VIEWS

                            <div id="result_post_wifi"></div>                                
                            <form role="form" action="" id="form-registrasi-field" class="form-registrasi" onsubmit="return registration();">
                                <div class="form-group">
                                    <label class="sr-only" for="form-username">Username</label>
                                    <input type="text" name="username" placeholder="Username..." class="form-username form-control" id="username" required>
                                </div>
                                <div class="form-group">
                                    <label class="sr-only" for="form-password">Password</label>
                                    <input type="password" name="password" placeholder="Password..." class="form-password form-control" id="password" required>
                                </div>
                                            <div class="form-group">
                                    <label class="sr-only" for="form-password2">Password</label>
                                    <input type="password" name="password2" placeholder="Confirm Password..." class="form-password2 form-control" id="password_lagi" required>
                                </div>
                                            <div class="form-group">
                                    <label class="sr-only" for="form-name">Full Name</label>
                                    <input type="text" name="name" placeholder="Full Name..." class="form-name form-control" id="name" required>
                                </div>
                                <div class="form-group">
                                    <label class="sr-only" for="form-email">Email</label>
                                    <input type="text" name="email" placeholder="Email..." class="form-email form-control" id="email" required>
                                </div>

                                            <button type="submit" class="btn"> REGISTRASI </button>
                    </form>
                </div>
        </div>

    </div>

JQUERY SCRIPT

function open_form()
{
    $('#result_wifi').attr({style : 'display:none'});
    $('#form_wifi').removeAttr('style');
}

function registration()
{
$('#result_post_wifi').html('<div class="alert alert-info">Sending Data  ...</div>');
    $.ajax({
                dataType     : "json",
        type     : "POST",
        url      : "wifi/wifi_sign_up",
        data     : "username=" + $('#username').val() + 
                   "&password=" + $('#password').val() + 
                   "&password2=" + $('#password2').val() + 
                   "&name=" + $('#name').val() + 
               "&email=" + $('#email').val() + 
        success  : function(res)
                            {
                                $('#result_post_wifi').html(res.message);
                                if(res.status == '200')
                                {
                                        $('#username').val('');
                                        $('#password').val('');
                                        $('#password2').val('');
                                        $('#name').val('');
                                        $('#email').val('');                                 
                                        $('#form_registrasi_field').attr({style : 'display:none'});
                                }
            },
        error   : function(){ $('#result_post_wifi').html('<div class="alert alert-warning">Fail to send registration form.</div>'); }, 
                timeout: 50000          
    });     
    return false;
}

CONTROLLER

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

class Wifi extends MX_Controller {

    public function __construct()
    {
        parent::__construct();
//      $this->load->model('wifi_model');
    }                                  



        public function sign_up()

    {
                $username          = $this->input->post('username');
                $password          = $this->input->post('password');
                $password2          = $this->input->post('password2');      
                $name               = $this->input->post('name');
                $email             = $this->input->post('email');

               $this->wifi_model->add_user();
               header("Content-type: application/json");
               echo "{\"data\":" .json_encode($data). "}";

MODELS

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Wifi_model extends MX_Model {

    public function __construct()
    {
        parent::__construct();
    }

 public function add_user($data)
    {
        $data=array(
            'username'=>$this->input->post('username'),
            'password'=>md5($this->input->post('password')),
            'password2'=>md5($this->input->post('password2')),
            'name'=>$this->input->post('name'),
            'email'=>$this->input->post('email'),

            );

        $this->db->insert('wifi_regs', $data);  

the above code still doesn't want to insert to the mysql database.

i've been trying with some other way but it still doesn't works

  • 写回答

1条回答 默认 最新

  • dongxiaoyan4388 2015-12-01 14:17
    关注

    view

    var data = $('#form-registrasi-field').serialize();
    
    $.ajax({
                    dataType     : "json",
            type     : "POST",
            url      : "wifi/sign_up",
            data     : data,
            success  : function(res)
                                {
                                    $('#result_post_wifi').html(res.message);
                                    if(res.status == '200')
                                    {
                                            $('#form-registrasi-field')
                                               .find('input').val('');
                                            $('#form-registrasi-field').hide();                                    }
                },
            error   : function(){ $('#result_post_wifi').html('<div class="alert alert-warning">Fail to send registration form.</div>'); }, 
                    timeout: 50000          
        });     
    

    controller

    public function sign_up(){
           $data['status'] = 200;
           $data['message'] = 'OK';
           if (!$this->wifi_model->add_user()){
               $this->output->set_status_header('500');
               $data['status'] = 500;
               $data['message'] = 'KO :( ';
           }
           //$data['post'] = $this->input->post();
           echo json_encode($data);
    }
    

    model

    $this->db->insert('wifi_regs', $data);  
    if ($this->db->affected_rows() == 1)
       return true;
    
    return false;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真