我遇到了ajax电子邮件和密码验证问题。我对控制器和模型没有任何问题,因为当我删除e.preventDefault();
时,它会返回正确的结果。为什么ajax代码总是返回false?是因为我使用MD5类型作为密码,或者这件事data: "email=" + email + "&" + "password=" + password,
不正确。
JS
$(document).ready(function(){
$('#myform').on('submit', function(e) {
// e.preventDefault(); //<---- stop submiting the forms
var email = $('#email').val();
var password = $('#password').val();
$.ajax({
type: "POST",
url: "<?php echo base_url()?>KulinerControl/otentikasi",
dataType: "json",
data: "email=" + email + "&" + "password=" + password,
success: function(data) {
if(data.valid) {
$('#msg').html(data.msg);
// $("#myform").submit(); //<---- submit the forms
} else {
$('#msg').html(data.msg);
}
}
});
});
});
控制器
function otentikasi() {
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$exists = $this->KulinerModel->otentikasi($email, $password);
if($exists) {
$msg = array(
'valid' => true,
'msg' => 'correct'
);
} else {
$msg = array(
'valid' => false,
'msg' => 'error !'
);
}
echo json_encode($msg);
}
查看
<?php echo form_open('KulinerControl/otentikasi', 'id="myform"'); ?>
<span id="msg"> </span>
<label for="email>Email</label>
<input type="text" name="email" required>
<label for="password" >password</label>
<input type="password" name="password" required>
<button type="submit" >Submit</button>
</form>
提前致谢
解决
我正在使用$("#formlogin").serialize()
答案 0 :(得分:0)
尝试这样。以对象格式{}
发送数据。并使用JSON.parse()
来解析您的json响应。
<script type="text/javascript">
$(document).ready(function(){
$('#myform').on('submit', function(e) {
// e.preventDefault(); //<---- stop submiting the forms
var email = $('#email').val();
var password = $('#password').val();
$.ajax({
type: "POST",
url: "<?php echo base_url('KulinerControl/otentikasi');?>",
dataType: "json",
data: {email:email,password:password},
success: function(response){
var data = JSON.parse(response)//or data = eval(response);
if(data.valid){
$('#msg').html(data.msg);
// $("#myform").submit(); //<---- submit the forms
}else{
$('#msg').html(data.msg);
}
}
});
});
});
</script>
不要忘记在控制器中加载url helper。
$this->load->helper('url');
在application/config/autoload.php
你的观点一定是这样的..
<?php echo form_open('', array('id'=>'myform')); ?>
<span id="msg"> </span>
<label for="email>Email</label>
<input type="text" name="email" id="email" required>
<label for="password" >password</label>
<input type="password" name="password" id="password" required>
<button type="submit" >Submit</button>
</form>
答案 1 :(得分:0)
在 js 中,您可以通过 $('#email').val();
和 $('#password').val();
获取值但是您还没有指定 id属性。因此,在两个输入中添加id属性。
要更多了解 jquery选择器,请访问 http://www.w3schools.com/jquery/jquery_ref_selectors.asp
试试这个
查看强>
<?php echo form_open('KulinerControl/otentikasi', 'id="myform"'); ?>
<span id="msg"> </span>
<label for="email>Email</label>
<input type="text" name="email" id="email" required>
<label for="password" >password</label>
<input type="password" name="password" id="password" required>
<button type="submit" >Submit</button>
</form>
<强> JS 强>
**$(document).ready(function(){
$('#myform').on('submit', function(e) {
e.preventDefault(); //<---- stop submiting the forms
var email = $('#email').val();
var password = $('#password').val();
$.ajax({
type: "POST",
url: "<?php echo base_url()?>KulinerControl/otentikasi",
dataType: "json",
data: "email=" + email + "&" + "password=" + password,
success: function(data) {
if(data.valid) {
$('#msg').html(data.msg);
// $("#myform").submit(); //<---- submit the forms
} else {
$('#msg').html(data.msg);
}
}
});
});
});**
<强>控制器强>
function otentikasi() {
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$exists = $this->KulinerModel->otentikasi($email, $password);
if($exists) {
$msg = array(
'valid' => true,
'msg' => 'correct'
);
} else {
$msg = array(
'valid' => false,
'msg' => 'error !'
);
}
echo json_encode($msg);
}