我正在尝试使用 ajax 在引导模式中提交表单。而且我的表单提交成功,但是ajax内的success语句没有执行。该页面被重定向到一个空白页面,显示 {"msg":"ok"}。
我正在粘贴控制器和视图中的代码。请帮忙。
控制器
$update_profile_details = $this->userp_m->edit_profile_m($uname,$uemail,$data1,$new_email);
if($update_profile_details == true)
{
$status['msg'] = 'ok';
}
else
{
$status['msg'] = 'err';
}
echo json_encode ($status);
View
$(document).ready(function()
{
$("#myForm").submit(function(e)
{
e.preventDefault();
var reg = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
var name = $('#inputName').val();
var email = $('#inputEmail').val();
if (name.trim() == '') {
alert('Please enter your name.');
$('#inputName').focus();
return false;
} else if (email.trim() == '') {
alert('Please enter your email.');
$('#inputEmail').focus();
return false;
} else if (email.trim() != '' && !reg.test(email)) {
alert('Please enter valid email.');
$('#inputEmail').focus();
return false;
} else {
var fd = new FormData(this);
$.ajax({
type: 'POST',
url: $('#myForm').attr('action'),
dataType: "json",
data: $('#myform').serialize(), fd,
contentType: false,
cache: false,
processData:false,
beforeSend: function()
{
$('.submitBtn').attr("disabled", "disabled");
$('.modal-body').css('opacity', '.5');
},
success: function(status)
{
alert(status);
if (status.msg == 'ok') {
$('#inputName').val('');
$('#inputEmail').val('');
$('.statusMsg').html('<span style="color:green;">Changes have been saved successfully.</p>');
} else
{
$('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
}
$('.submitBtn').removeAttr("disabled");
$('.modal-body').css('opacity', '');
},
error: function(status)
{
alert("Some error, please try again");
}
});
}
});
HTML
<form id="myform" method="post" enctype="multipart/form-data" action="<?php echo site_url('User/user_index_c/edit_profile_c'); ?>">
<label>Full Name : </label>
<input class="name_styling" type="text" placeholder="Enter name" id="inputName" name="uname">
<label>Email Id : </label>
<input class="email_styling" type="email" placeholder="Enter email" id="inputEmail" name="new_email">
<div class="controls">
<label>Profile Photo : </label>
<input name="file1" type="file" id="image_file" />
<img id="blah" class="logoupload" src="#" alt="your image" />
<span class="filename"></span>
</div>
<center><input class="submitBtn" id="submit" type="submit" value="Save Changes" name="submit" ></center>
</form>