php - 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? -


i'm using php, smarty, jquery, ajax, etc. website.following html code of form i'm submitting using ajax:

<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 wrong" id ="chkquewrong">question wrong</input>             </td>           </tr>           <tr>             <td></td>             <td><input type="checkbox" name = "que_issue[]" value = "answers wrong" id ="chkanswrong">answers wrong</input></td>            </tr>           <tr>             <td></td>             <td><input type="checkbox" name = "que_issue[]" value = "question direction incorrect" id ="chkdirincorrect">question direction 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> 

the ajax code submitting form follows:

$(document).ready(function() { $('#question_issue_form').submit(function() { var ans = confirm("are sure report 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();           }         }       });     });   }); 

the php code of file(question_issue.php) i'm submitting form follows:

<?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 errors in submission of 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 no error in submission of 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;   }   ?> 

the issue i'm facing when click on ok button of confirmation alert, form gets submit error messages or success messages in json format appearing on blank screen.

actually should appeared in pop-up alert , page should not redirected other url. error/success messages printing on blank white page , page redirected question_issue.php. can please me in avoiding these things , showing error/success messages alert box on same page?

i think should prevent default behavior of form, won't submit ask json:

$('#question_issue_form').submit(function(e) {     e.preventdefault();     // code 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -