0

I have a script where I open a jQuery Dialog Box with a form. I send the values of the form via ajax to a php script. The php script save the value in a mysql database.

Everything work fine. But:

In the case, that something is wrong with the insert of mysql I will give a information back to the user. But not only "is wrong". I want give for example back "Already exists", "Wrong parameters" and so on.

How can give information back from the php script to tha jQuery Dialog Box?

My script looks like this:

  function openmodal() {   
      var $dialogContent = $("#create_mieter"),    
      mieterID = $("#mieterID"),
      snID = $( "#snID" ),
      mietwechsel = $("#datepicker"),
      zahlart = $("#zahlart");

      $dialogContent.dialog({
        height: "auto",
        width: 500,
        modal: true,
        resizable: false,
        title: "Neues Mietverhältnis anlegen",
        close: function() {},
        buttons: {
            Sichern : function() {
                $.ajax({
                type: "POST",
                url: "wohnungen_bearbeiten_mieter_sichern.php?act=create",
                data: "mieterID=" + mieterID.val() + "&snID=" + snID.val() + "&mietwechsel=" + mietwechsel.val() + "&zahlart=" + zahlart.val(),
                        success: function(response) {
                            alert("Show Success");                          
                        },
                        error: function() {
                            alert("Sorry something went wrong");    
                        }
                });
            $( this ).dialog( "close" );
            },
         Abbrechen : function() {  $( this ).dialog( "close" ); }
         }
      }).show();

      $("#ui-datepicker-div").css("z-index", "99999");
2
  • You need to return a response from the server to indicate there was an error. Format a json string to be sent back to the server in the success: function(response). Then you can do if(response.error){alert(response.error)}else{$(this).dialog('close')}. Commented Dec 23, 2012 at 0:32
  • you are dealing with 2 separate types of errors. Error in AJAX communicating or returning unparseable data, or server side issues that you may need to send a message back such as failing to connect to db. They are not the same thing. Research AJAX status codes as a start for the error handler in jQuery. If your db fails to connect but server code doesn't crash... ajax success will still fire Commented Dec 23, 2012 at 0:52

1 Answer 1

0

Its all about the 'response' right?

You want it to show you, for example, that the email the user chose, is already in use. So, in the file you will request by ajax, you should do the validating, I did this once My 'response' was like this:

it validates the email, and query it to see if it's in use, if it is, the response will be one, else it will be 0, and if another kind of error occur, the response would be the error itself

then back on the ajax function, I would validate the response, it works for me, hope it works for you

Sorry for the bad English, not native ;D

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.