Solved

Ajax not working properly

Posted on 2016-07-23
2
98 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 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Refresh div via ajax without lost ccs <li> styles 8 36
PHP Installer 5 25
how to use a switch statement with heredoc 11 25
parse url to form? 7 14
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

831 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