我是codeigniter的新手,但我有一些问题: 我有表单注册,从我的表单获取输入并将其添加到数据库,并在表单注册下显示此数据。 我的问题是在我的ajax函数中,因为它返回我的错误和[object Object],或者它可能在ajax中没有问题....我不知道...... 谢谢你的帮助... 我的观点和脚本是:
<head>
<title>Welcome</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/main.css'); ?>" />
<script type="text/javascript" src="<?php echo base_url('assets/js/jquery.js'); ?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/js/jquery.validate.js'); ?>"></script>
<script>
function AjaxFormRequest(result_id,form_id,url) {
$.ajax({
url: url, //Адрес подгружаемой страницы
type: "POST", //Тип запроса
dataType: "html", //Тип данных
data: jQuery("#"+form_id).serialize(),
success: function(response) { //Если все нормально
document.getElementById(result_id).innerHTML = response;
alert(response);
},
error: function(response) {
console.log(response);
document.getElementById(result_id).innerHTML = "Error on sending form"+response;
}
});
}
</script>
</head>
<body>
<center>
<div id="header">
<h1>Welcome friend</h1>
</div>
<div id="center">
<div id="move">
<p>Please make your next move</p>
<button type="button" onclick="show_button('in');">Sign in</button>
or
<button type="button" onclick="show_button('up');">Sign up</button>
</div>
<div id="sigin">
<!-- <form id="form" method="post" action="javascript:void(0);" onsubmit="ajax()"> -->
<form method="post" action="" id="myform">
<input maxlength="20" id="email" name="email" placeholder="Email" type="text">
<br>
<input maxlength="20" id="password" name="password" placeholder="Password" type="password">
<br>
<input maxlength="20" id="conf_password" name="conf_password" placeholder="Confirm password" type="password">
<br>
<button id ="btsignin" type="submit" name="send" class="send" onclick="AjaxFormRequest('result', 'myform', '<? site_url().'/main/send' ?>')">Sign in</button>
<button id ="btsignup" type="submit" name="send" class="send" onclick="AjaxFormRequest('result', 'myform', '<? site_url('main/send'); ?>')">Sign up</button>
</form>
<!-- </form> -->
<button id="cancel" onclick="cancel();">Cancel</button>
</div>
<div id="result" >
<? var_dump(site_url().'/main/send');?>
</div>
</div>
</center>
</body>
主要的php是:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('index');
}
public function send() {
if (!isset($_POST['email']) or empty($_POST['email'])) {
$error1 = "Email?<br />";
}
else
$error1 = NULL;
if (!isset($_POST['password']) or empty($_POST['password'])) {
$error2 = "Password?<br />";
}
else
$error3 = NULL;
if (!isset($_POST['conf_password']) or empty($_POST['conf_password'])) {
$error3 = "Confirm Password?<br />";
}
else
$error4 = NULL;
if (empty($error1) and empty($error2) and empty($error3)) {
$password = $_POST['password'];
$email = $_POST['email'];
$type = 1;
$this->load->library('userlib');
if ($this->userlib->register($email, $password, $type, true)) {
echo $_POST['email'];
echo $_POST['password'];
echo $_POST['conf_password'];
} else {
return false;
}
} else {
echo $error1 . $error2 . $error3;
}
}
}