0

I'm using PHP, Javascript, AJAX for my website.

I'm putting below only the necessary code.

JS code(AJAX function code):

$("#btn_add_event").click(function(){

    var strSeriaze = $( "#formAddEvent" ).serialize();
    url = $( "#formAddEvent" ).attr('action');
    $("#btn_add_event").attr('disabled', 'disabled');
    $("#addEventErrorMsg").html('');
    $.ajax({
        url: url,
        type: "POST",
        data:  {postData:strSeriaze},
        beforeSend: function(){
            $('#loader-icon').show();
        },
        complete: function(){
            $('#loader-icon').hide();
        },
        success: function(data){
        //Control is not returning here on success from PHP file
            $('#loader-icon').hide();

            if(data == "Success")
            {
                $("#myModal-add-event").modal('hide');
                $("#myModal-add-event").hide();
                window.location.href = site_url + "event_index.php";
                return false;
            }
            else
            {
                $("#btn_add_event").attr('disabled', false);
                $("#addEventErrorMsg").show();
                $("#addEventErrorMsg").html(data);
            }
        },
        error: function(){}
    });   
  }) 

I tried to debug the issue the call is going perfectly to PHP file, the logic written over there is also working but the control is not getting returned to the js file(i.e. AJAX success function). In turn I'm unable to execute code written inside success function.

Please correct the mistake I'm making in my code.

Following is the necessary PHP code after execution it is expected to return the control to js file(i.e. AJAX success funtion) but it's not happening.

if($ret) {
  $eventAddResultArr = $objEvent->GetResponse();

  if($eventAddResultArr['msg'] == 'Success') {
    echo "Success";
    exit;
  //From here it is expected to return the control to js file's AJAX success function but it's stopping execution here only
  } else {
    $errMsg = "";
    foreach($eventAddResultArr['msg'] as $key => $err_msg) {
      $errMsg .= $err_msg."<br>";
    }       
    echo $errMsg;
    exit;
  }
}

Note : I've put in comments in my code to make you understand my issue better. Still if you need any further information regarding the issue I'm facing please do let me know.

Thanks.

PHPLover
  • 1
  • 51
  • 158
  • 311

2 Answers2

3

If you are not using DataType in ajax than you can follow this:

In php

Use echo true; instead of echo "Success";

And in Ajax, check with:

if(data == true) {
//your code
}
devpro
  • 16,184
  • 3
  • 27
  • 38
  • Hey Thanks bro, your trick worked for me. If you don't mind could you please do me a favor and add the code with DataType too. Like json. It would be of great help to me man. Thanks once again. Waiting for your reply keenly. – PHPLover Dec 30 '15 at 11:56
  • @user2839497 Make sure you test your error route correctly too - the code above may well treat an error string in `data` as a boolean `true`. `if (data === true)` may be all that's needed, though returning two different types to the same handler doesn't seem right - it would be better if you came up with some other representation of success/failure, eg `{ success: true }` and `{success: false, message: 'error message' }`, then test for `if (data.success)` and use `data.message` in your error handling route. – James Thorpe Dec 30 '15 at 12:00
  • @user2839497: its better to use datatype instead of assuming anything in response... well, yes i can show u the example.. – devpro Dec 30 '15 at 12:04
  • It would be great devpro if you could. – PHPLover Dec 30 '15 at 12:04
  • @JamesThorpe: you are senior in SO, please suggest how can i share the example ? can i update in my answer, but its not related to question... can i? or plz suggest – devpro Dec 30 '15 at 12:07
  • 1
    @devpro I'm not sure you need to - you're running the risk of "question/answer creep" - sounds like you've successfully answered the original question, I was just providing the OP with some hints about how they may wish to make their code more robust in the long run. Ideally this is something they go and research further, and come back with a _new_ specific question about any issues encountered. – James Thorpe Dec 30 '15 at 12:08
  • @user2839497: my friend, i hope you read the commect of Mr. James, now you can research your self there are so many examples available on internet or create a seperate question, i will share u the solution. thank you.. dont forgot to mark as accepted the answer this will help to others.. – devpro Dec 30 '15 at 12:11
0

Just remove the exit; it would be the problem, especially there's no need for it.

Yazid Erman
  • 1,166
  • 1
  • 13
  • 24
  • Well, Debugging is mandatory then! Try to replace the target php file with a file that just 'echo "Success";' and tell us what happens – Yazid Erman Dec 30 '15 at 11:55