Solved

Ajax not working properly

Posted on 2016-07-23
2
127 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 53

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

We will take a look at the d3.js library for visualizations.  I will provide a walkthrough of a short bar graph example as well as introduce you to the d3.js API.  We also will explore links of other examples and further information regarding SVG's.
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…
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 look for a specific file type in a local or remote server directory using PHP.

690 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