我正在使用 Bootstrap Modal 获取用户的输入并将其提交给 CI 控制器进行验证。在将输入保存到数据库表之前,我希望能够将结果返回到相同的模式 (JSON)。到目前为止,Firebug 告诉我我有一个 TypeError :结果为空。我想让它工作。我是 JQuery/Ajax 的新手,感谢您的帮助。
脚本
$(document).ready(function() {
$('#createCardId').click('click', function(e) {
e.preventDefault();
//we'll want to move to page specific files later
var email = $('input#email').val(); //from hidden field
var location = $('input#location').val(); //from hidden field
var card = $('#cardId').val(); //user's input
$.ajax({
url : "../edituser/addCardId",
dataType : "json",
type : 'POST',
data : "email=" + email + "&location=" + location + "&CardId=" + card,
success : function(result) {
if (result.error) {
$(".alert").fadeIn('slow');
$("#error_message").html(result.message);
} else {
$(".alert").fadeIn('slow');
$("#error_message").html(result.message);
$('#new_item').modal('hide');
}
},
});
});
});
视图模态引导
<div id="myModal" class="modal hide fade modal-admin" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Add Card ID</h3>
</div>
<div class="modal-body">
<!--############-->
<!-- column left -->
<div class="span6">
<p>
<?php
$location = $this->session->userdata('location');
$email = $account->email;
echo $location. '<br>';
echo $email;
if(validation_errors())
{
echo '<div class="alert alert-error">'.validation_errors().'</div>';
}
?>
<!--CARD ID -->
<div id="error_message"></div>
<div class="control-group">
<input id="email" type="hidden" name="email" value="<?php echo $email; ?>">
<input id="location" type="hidden" name="location" value="<?php echo $location; ?>">
<label class="control-label">Card ID: <span style="color: red;">*</span></label>
<div class="controls">
<input type="text" id="cardId" name="cardId" value=""/>
</div>
</div>
<!--END OF CARD ID-->
</p>
</div>
<!--############ column left ends-->
<!--############-->
<!--column right-->
<div class="span6">
<p>Hello <?php echo $location; ?></p>
</div>
<!--############ column right ends-->
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
<button class="btn btn-primary" id="createCardId" "data-dismiss=modal">Save changes</button>
<!--<input name="submit" type="submit" id="submit" value="Save Changes" class="btn btn-primary">-->
</div>
<!--############-->
</div>
CI 控制器
public function addCardId()
{
$this->form_validation->set_rules('CardId', 'Card Id', 'required|trim|xss_clean|max_length[6]|is_unique[accounts.CardId]');
$email = $this->input->post('email');
$location = $this->input->post('location');
$result = array();
if ($this->input->is_ajax_request())
{
if ($this->form_validation->run() == FALSE) {
$result['error'] = true;
$result['message'] = validation_errors();
} else {
$result['error'] = TRUE;
$result['message'] = 'The Card ID has been saved';
//Model will load here to add Card ID to the database.
redirect('admin/editaccount/search_account', 'refresh');
}
$json = json_encode($result);
die($json);
}
else
{
redirect('../searchaccount/showresults', 'refresh');
}
}
谢谢您的帮助!!