Solved

Ajax not working properly

Posted on 2016-07-23
2
83 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
PHP Undefined Index in HTML Form? 2 32
Gulp not seeing Changes 4 36
only allow numbers with preg match 4 25
What is the best PDF generator to use? 1 27
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now