• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Ajax not working properly

I have an AJAX function that almost works properly.  When you fill out a form the AJAX enters your form field data into a database table.  If you don't fill out all of the form fields, the PHP fill returns an error and the error is passed back via json.

However, my form works all of the info is entered into the DB and no errors are sent back  BUT even though everything went as planned my AJAX does not see it as a success it sees it as if my PHP returned errors.

in my code  if (d.length)   always seems to be true even though it is not.  This was not happening before, it just started out of the blue.
                        $(function () {
                            $("#calc").on('submit', function (e) {
                            $("#calc_submit").click(function () {
                                    url: 'http://MYSITE.com/ltv_email_ajax.php',
                                    data: $("#calc").serialize(),
                                    method: 'post',
                                    success: function (d) {
                                        // d will hold the json representation of $errors
                                        if (d.length) {
                                            // there are errors
                                            $( "div.errors").html(function() {
  var errormessage = d;
  return errormessage;
                                        } else {
                                            // success!

Open in new window

2 Solutions
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
>as if my PHP returned errors.

That is your issue.  Start by debugging your php file.

Success means your php returned data, good or bad.  In your jquery, you may want to test for the type of data returned, you are just testing if any data comes back.
RobOwner (Aidellio)Commented:
Above the if(d.length), put console.dir(d) and post the output here if it does make sense
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now