Solved

Ajax not working properly

Posted on 2016-07-23
2
106 Views
Last Modified: 2016-07-23
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.
 <script>
                        $(function () {
                            $("#calc").on('submit', function (e) {
                                e.preventDefault();
                            });
                            $("#calc_submit").click(function () {
                                $.ajax({
                                    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
alert("error");
                                            $( "div.errors").html(function() {
  var errormessage = d;
  return errormessage;
});
                                        } else {
                                            alert("hello");
                                            // success!
                                            $(".submit-cont").hide("fast");
                                            $(".calculator-statement").show("fast");
                                        }
                                    }
                                });
                            });
                        });
                    </script>

Open in new window

0
Comment
Question by:rgranlund
2 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 250 total points
ID: 41726074
>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.
0
 
LVL 43

Assisted Solution

by:Rob
Rob earned 250 total points
ID: 41726078
Above the if(d.length), put console.dir(d) and post the output here if it does make sense
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question