![](/img/trans.png)
[英]show ajax success or error message when form is submitted in the same page
[英]Why the form submitted using AJAX is redirecting to next page and the error/success messages are not displayed into alert on the same page?
我的網站使用PHP,Smarty,jQuery,AJAX等。以下是我使用AJAX提交的表單HTML代碼:
<form name="question_issue_form" id="question_issue_form" action="http://localhost/xyz/pqr/web/control/modules/questions/question_issue.php">
<input type="hidden" name="form_submitted" id="form_submitted" value="yes"/>
<input type="hidden" name="op" id="op" value="question_issue"/>
<input type="hidden" name="question_id" id="question_id" value="35718"/>
<table class="trnsction_details" width="100%" cellpadding="5">
<tbody>
<tr>
<td></td>
<td>
<input type="checkbox" name = "que_issue[]" value = "Question is wrong" id ="chkQueWrong">Question is wrong</input>
</td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name = "que_issue[]" value = "Answers are wrong" id ="chkAnsWrong">Answers are wrong</input></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name = "que_issue[]" value = "Question direction is incorrect" id ="chkDirIncorrect">Question direction is incorrecct</input></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name = "que_issue[]" value = "Other" id ="chkOther">Other</input></td>
</tr>
<tr>
<td></td>
<td class="set_message" style="display:none;"><textarea name="que_issue_comment" id = "que_issue_comment" rows="4" cols="25" maxlength="100"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" id="report_question_issue" class="c-btn submit_form"/></td>
</tr>
</tbody>
</table>
</form>
提交表單的AJAX代碼如下:
$(document).ready(function() {
$('#question_issue_form').submit(function() {
var ans = confirm("Are you sure to report the question issue?");
if (!ans) {
return false;
}
var post_url = $(this).attr('action');
$.ajax({
type: "POST",
url: post_url,
data: $('#question_issue_form').serialize(),
dataType: 'json',
success: function(data) { alert(data);
var error = data.error_message;
if(error)
alert(error);
else {
alert("Question issue has been reported successfully.");
$.colorbox.close();
}
}
});
});
});
我要提交此表單的文件(question_issue.php)的PHP代碼如下:
<?php
require_once("../../includes/application-header.php");
$objQuestionIssue = new QuestionIssue();
prepare_request();
$request = $_POST ;
$user_type = $_SESSION[SESSION_NAME_CONTROL][STAFF_TYPE];
if($user_type == 'super_admin' || $user_type == 'admin' || $user_type == 'data_entry_operator' || $user_type == 'owner' || $user_type == 'faculty' || $user_type == 'content_development_head' || $user_type == 'test_admin' || $user_type == 'student_admin')
$requested_user_type = 'staff';
else
$requested_user_type = 'student';
$form_data = array();
$form_data['question_id'] = $request['question_id'];
$form_data['reported_site_id'] = SITE_ID;
$form_data['reported_user_type'] = $requested_user_type;
$form_data['reported_user_id'] = $_SESSION[SESSION_NAME_CONTROL][STAFF_ID];
$form_data['que_issue'] = implode(",", $request['que_issue']);
$form_data['que_issue_comment'] = $request['que_issue_comment'];
$form_data['que_issue_date'] = time();
switch( $op ) {
case "question_issue":
if($request['form_submitted']=='yes') {
$ret = $objQuestionIssue->InsertQuetionIssue($form_data, $question_issue_error_messages);
/*If condition : If there are any errors in submission of a report question form*/
if(!$ret) {
$error_msg = $objQuestionIssue->GetAllErrors();
$data = array();
$data['error_message'] = $error_msg['error_msgs'];
$data = json_encode($data);
echo $data;
die;
/*Else condition : If there is no error in submission of a report question form*/
} else {
$data = array();
$data['success_message'] = "success";
$data = json_encode($data);
echo $data;
die;
}
} else {
$smarty->assign('question_id', $request['question_id']);
$file_to_show = 'question-issue.tpl';
}
$smarty->display($file_to_show);
break;
die;
}
?>
我面臨的問題是,當我單擊確認警報的“確定”按鈕時,表單被提交,但是錯誤消息或json格式的成功消息出現在黑屏上。
實際上,它們應該出現在彈出警報中,並且頁面不應重定向到其他URL。 但是錯誤/成功消息將打印在空白的白頁上,並且該頁也被重定向到question_issue.php。 有人可以幫我避免這些事情並將錯誤/成功消息顯示在同一頁面的警報框中嗎?
我認為您應該防止表單的默認行為,因此它不會真正提交,而僅要求提供json:
$('#question_issue_form').submit(function(e) {
e.preventDefault();
// your code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.