使用 codeIgniter,我成功地使用 enctype: multipart/form-data 实现了用户注册,但之后我开始尝试使用 AJAX 进行用户注册,但我卡住了。我可以在控制台中看到 ajax 响应,但有一个响应始终为空值(即密码)。而且它不再注册用户了。提交ajax请求后没有数据插入。这就是我正在做的事情。这是我编写此脚本的注册视图
<script type="text/javascript">
function AJAXcreatemember() {
//getting form fields values
var fname = $('#firstname').val();
var lname = $('#lastname').val();
var email = $('#email').val();
var pwd = $('#pwd').val();
var cpwd = $('#cpwd').val();
var gender = $('#gender').val();
var country = $('#country').val();
var city = $('#city').val();
var occupation = $('#occupation_area').val();
var about = $('#about').val();
var interest = $('#interest').val();
var contact_no = $('#contact_no').val();
var website_url = $('#website_url').val();
//setting ajax information to be sent to $.ajax()
var path = "<?php echo base_url()?>user/register/";
var data = 'first_name='+fname+'&last_name='+lname+'&email='+email+'&password='+pwd+'&gender='+gender+'&country='+country+'&city='+city+'&occupation_area='+occupation+'&about='+about+'&interest='+interest+'&contact_no='+contact_no+'&website_url'+website_url;
alert('URL: ' + path);
$.ajax({
url: path,
data: data,
type: 'GET',
success: function(data) {
alert(data);
}
});
return false;
</script>
用户控制器:
public function register() {
$time = date("Y-m-d H:i:s");
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('gender', 'Gender', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('pwd', 'Password', 'matches[cpwd]');
$this->form_validation->set_rules('city', 'City', 'required');
$this->form_validation->set_rules('country', 'Country', 'required');
$this->form_validation->set_rules('occupation_area', 'Functional Area', 'required');
if ($this->form_validation->run() == FALSE) {
$this->index();
}
else {
$user_email = $this->user_model->get_user_by_email($this->input->post('email'));
if($user_email->num_rows() == 0) {
$avatar = $this->upload('profile_image');
$this->user_model->create_user($time , $avatar);
/*if($this->upload->data()) {
$data['success'] = 'yes';
$this->load->view('index', $data);
}*/
}
/*else {
$data['page_title'] = 'CM Rizwan - Khabrain News Group Columnist';
$data['email_exist_error'] = 'Email Already exist.';
$this->load->view('index', $data);
}*/
}
}
public function upload($field) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if(!$this->upload->do_upload($field)) {
$upload_error = array('upload_error' => $this->upload->display_errors());
$this->load->view('index', $upload_error);
}
/*else {
$upload_success = array('upload_data' => $this->upload->data());
$this->load->view('index', $upload_success);
}*/
//$this->upload->data() returns all the required information for an upload file.
$avatar = $this->upload->data();
return $avatar['file_name'];
}
注意已编辑:注册视图是我已切片的 index.php 文件的一部分,注册视图是单击注册链接时显示的弹出窗口。