0

I am using JQuery validate on two fields. I do all my validation and display messages if validation fails. I then have my submit handler which is using Ajax

submitHandler: function(form) {
         $.ajax({
            type: $(form).attr('method'),
            url: $(form).attr('action'),
            data: $(form).serialize(),
            dataType : 'json'
        })
        .done(function (response) {
            if (response.success == 'success') {               
                alert('success');                       
            } else {
                alert('fail');
            }
        });
        return false;
    }

My problem is that both fields are not required, only one or the other (or both). I have handled this no problem. However, the submitted data will be sent to my PHP file using Ajax. In this PHP, I check to see what fields have data e.g.

if (isset($_POST["mobileNumber"] && !empty($_POST["mobileNumber"])){
    var_dump("WORKING");
} 

I then need to check the input against a web service API. If I do this seperately, it is not a problem. However, if both inputs are entered into the form, I then need to make 2 API calls to 2 different APIs (each input uses a different API), and then return the response for both back to .done. Once again, if only one input is provided, I dont see a problem. The thing I am wondering about is if both inputs are provided, and how I can return both response?

Any advice appreciated.

1 Answer 1

1

Why don't you send both the responses of the API calls back in one response?

$response = array();

if (isset($_POST["mobileNumber"] && !empty($_POST["mobileNumber"])){
    $response['mobileNumberResponse'] = array('success'=>true,'data'=>array());
}

if (isset($_POST["secondParameter"] && !empty($_POST["secondParameter"])){
    $response['secondParameter'] = array('success'=>true,'data'=>array());
}

echo json_encode($response);

Or something similar. If this isn't an option send two ajax's requests if both parameters are present.

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.