Я пытаюсь вставить некоторую информацию со своей страницы регистрации. Я делаю все функции с моделью 'MVC'. код:
Страница регистрации:
<main>
<div id="mainWrraper">
<p id="error"></p>
<?php echo form_open('login_controller/insertData'); ?>
<fieldset>
<legend>Registration:</legend>
<div class="inputWrapper"><label>First name: </label><input id="First_name" class="formInput" type="text" name="First_name"></div>
<div class="inputWrapper"><label>Last name: </label><input id="Last_name" class="formInput" type="text" name="Last_name"></div>
<div class="inputWrapper"><label>Email: </label><input id="Email" class="formInput" type="text" name="Email"></div>
<div class="inputWrapper"><label>Username: </label><input id="Username" class="formInput" type="text" name="Username"></div>
<div class="inputWrapper"><label>Password: </label><input id="Password" class="formInput" type="password" name="Password"></div>
<div class="inputWrapper"><label>Confirm Password: </label><input id="Confirm_password" class="formInput" type="password" name="Confirm_password"></div>
<div class="inputWrapper"><label>Year Of Birth: </label><input id="Year_Of_Birth" class="formInput" type="text" name="Year_Of_Birth"></div>
<!--<div class="inputWrapper"><label>Date Of Birth: </label><input id="Date_B" class="formInput" type="date" name="Date_B"></div>-->
<div class="inputWrapper"><label> Location:</label><select id="Location" name="Location">
<option value="Central">Central</option>
<option value="South">South</option>
<option value="North">North</option>
</select></div>
<div class="inputWrapper"><label> Gender:</label><select id="Gender" name="Gender">
<option value="Women">Women</option>
<option value="Men">Men</option>
</select></div>
<div class="inputWrapper"><input id="register" type="button" value="register" name="submit">
</fieldset>
</form>
</div>
</main>
<script>
$("#register").click(function() {
var First_name = $("#First_name").val();
var Last_name = $("#Last_name").val();
var Email = $("#Email").val();
var Username = $("#Username").val();
var Password = $("#Password").val();
var Confirm_password = $("#Confirm_password").val();
var Year_Of_Birth = $("#Year_Of_Birth").val();
var Location = $("#Location").val();
var Gender = $("#Gender").val();
$.ajax({
type: 'POST',
url: "<?php echo site_url(); ?>"+"/login_controller/insertData",
data: {
First_name: First_name,
Last_name: Last_name,
Email: Email,
Username: Username,
Password: Password,
Confirm_password: Confirm_password,
Year_Of_Birth: Year_Of_Birth,
Location: Location,
Gender: Gender
},
error: function() {
alert('Something is wrong');
},
success: function(data) {
if (data === "1") {
alert("Registration succedded");
window.location.href = "<?php echo site_url('login_controller/login');?>";
} else {
$("#error").html(data);
}
}
});
});
</script>
Login_Controller:
<?php
class login_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('login_model');
$this->load->helper('url_helper');
$this->load->helper('form');
$this->load->library('session');
}
public function login()
{
$data['user']=NULL;
$this->load->view('templates/header', $data);
$this->load->view('login_page/login', $data);
$this->load->view('templates/footer');
}
public function logout()
{
$data = array('User_name','Password');
$this->session->unset_userdata($data);
redirect("login_controller/login");
}
public function auth(){
$data = array(
'Username' => $this->input->post('Username'),
'Password' => md5($this->input->post('Password'))
);
$check=$this->login_model->auth($data);
if ($check==false){
$data['error']= array("message"=>"User is not found");
$data['user'] = NULL;
$this->load->view('templates/header', $data);
$this->load->view('login_page/login', $data);
$this->load->view('templates/footer');
}
else{
$data['user'] = $check;
$this->session->set_userdata($data);
redirect("web_controller/home");
}
}
public function register()
{
$data['user']=NULL;
$this->load->view('templates/header', $data);
$this->load->view('login_page/register', $data);
$this->load->view('templates/footer');
}
public function check($data){
$error ='';
if($data['First_name'] == NULL)
{
$error .= "Enter your first name properly"."<br>";
}
if (!preg_match("/^[a-zA-Z ]*$/",$data['First_name']))
{
$error .= "Enter only letters in the first name"."<br>";
}
if (!preg_match("/^[a-zA-Z ]*$/",$data['Last_name']))
{
$error .= "Enter only letters in the last name"."<br>";
}
if($data['Last_name'] == NULL)
{
$error .= "Enter your last name properly"."<br>";
}
if(!filter_var($data['Email'],FILTER_VALIDATE_EMAIL))
{
$error .= "Invalid email format"."<br>";
}
if($data['Password'] != $data['Confirm_password'])
{
$error .= " The passwords didn't match"."<br>";
}
return $error;
}
public function insertData(){
$data = array(
'First_name' => $this->input->post('First_name'),
'Last_name' => $this->input->post('Last_name'),
'Email' => $this->input->post('Email'),
'Username' => str($this->input->post('Username')),
'Password' => md5($this->input->post('Password')),
'Confirm_password' => md5($this->input->post('Confirm_password')),
'Year_Of_Birth' => $this->input->post('Year_Of_Birth'),
'Location' => $this->input->post('Location'),
'Gender' => $this->input->post('Gender')
);
$error = $this->check($data);
if($error == '')
{
$error_db = $this->login_model->insertData($data);
if($error_db==NULL){
$data['info']=array("message"=>"1");
}
else{
$data['info']=array("message"=> "Error. Registration faild: ".$error_db["message"] );
}
echo $data["info"]["message"];
print_r($data);
}
else{
echo $error;
echo $data['First_name'];
}
}
}
Login_Model:
<?php
class login_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function auth($data)
{
$query = $this->db->query("select * from users where Username = '".$data['Username']."' and Password ='".$data['Password']."'");
if($query){
return $query->result_array();
}
return false;
}
public function insertData($data){
$this->db->db_debug = FALSE;
$error=NULL;
if (!$this->db->insert('users', $data)){
$error=$this->db->error();
}
echo "Test Message";
return $error;
}
}
Проблема в том, что когда я нажимаю «Зарегистрироваться» на странице регистрации. когда я щелкаю по нему, я прыгаю в строку 62 (код страницы регистра) alert('Something is wrong');
и в DevTools показывается мне:
Моя база данных выглядит так:
и в Интернете это выглядит так (здесь показывается предупреждение):
Надеюсь, я объясню себя хорошо, я просто хочу вставить свои данные в базу данных. большое спасибо !
РЕДАКТИРОВАТЬ # 1: Я думаю, что проблема заключается в соединении между контроллером и моделью. похоже, он не распознает функцию модели (и файл).