gpt4 book ai didi

php - Codeigniter jquery ajax mysql 形式

转载 作者:行者123 更新时间:2023-11-29 21:47:08 25 4
gpt4 key购买 nike

观点

                            <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 脚本

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). "}";

型号

<?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);

上面的代码仍然不想插入到mysql数据库。

我尝试过其他方法,但还是不行

最佳答案

查看

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);
}

型号

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

return false;

关于php - Codeigniter jquery ajax mysql 形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com