0

I am trying to get this ajax method to work but, for some reason my success function will not execute and I can not retrive informtion from my server. If any one could tell me what I am doing wrong I would apperciate it. My code is below.

 <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <script src="jquery3_2_1.js" type="text/javascript"></script>
        <script src="Test.js" type="text/javascript"></script>
    </head>
    <body>
    <form class="ajax">
    <input type="hidden" name="Item" value="Chicken">
    <input type="hidden" name="Price" value="2.99">
    <input type="hidden" name="Qty" value="5">
    <button type="submit">ADD TO CART</button>
    </form>
    <div id="response1"></div>
    </body>
    </html>

my server code is here:

<?php
//UpdateCart.php

  echo "This is a test.";

?>

here is my jquery:

$(document).ready(function(){
    $('form.ajax').submit(function(){
      alert('Trigger');

        $.ajax({
            type: "post",
            url: "UpdateCart.php",
            dataType: "text",
            success: function(response){
              $('#response1').html('reponse variable equals: '+response);
            },
            error:function(){
              $('#response1').html('in error function');
          }
       });//end ajax function
    });//end submit function
});//end ready function

When I run the code, the error function is always executed and I never reach the success function block of code. I tried changing the dataType to html but, that does not work. I replaced the submit function with the on function and used the submit parameter and I still can not execute any code in my success function. However, when I remove code that listens to the submit event, I am able to reach my success function. But, I do not want this behaviour from my program. I want to be able to control the request to the server with the submit event. I do not get back any errors from the error function nor do I see anything when I check the network tag in my browser's developer console. I even tried looking into apache's access and error logs but, I do not see any errors or any sign of my code communicating. Any suggestions?

p.s. Thanks for all the suggestions. Just to clarify, here are the first things that I tried. I checked the traffic transmitted between the server and my browser and I am not seeing the requested information being returned from my server. I suspect the the ajax method is not sending the request in the first place however, the request is being made. I copied and pasted my uniform request identifier from one code to the next so, I know that it is correct. I took out the action attribute because my application was making the call to the server using the uniform request locator provided by the code in the html and not the url provided by the property in the ajax method. I even went so far as to check apache's access and error logs to see if I could see any http errors that might have been made but, there was no indication of any traffic between my browser and my server. I have tried to use the "event.preventDefault();" but that did not work either. I used the function in the code block that has the submit event listener; I placed it at the end of the code block and it still does not work. I am baffled and do not know what I am doing wrong.

Truck35
  • 19
  • 4
  • 2
    use  `error:function(result, status, error)` so you can display a proper error and see what is the issue. Are you sure your path is correct ? have you tried with the full url just to make sure ? you can check in your browser dev console for each request and their status. What's it saying about this request ? – Unex Dec 19 '17 at 14:43
  • use developer tools on (Chrome or Firefox) to verify the ajax request and see if the script is being called – Claudio Dec 19 '17 at 14:44
  • Obvious first question is: Is the file on disk called `UpdateCart.php` or `updatecart.php` – RiggsFolly Dec 19 '17 at 14:44
  • you have form submit function inside jquery and no prevent-default mechanism. so form will obviously proceed like normal form submission – Alive to die - Anant Dec 19 '17 at 14:44
  • https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – Edwin Dec 19 '17 at 14:46
  • I tried using the error function, I looked up all the network information using my browser, I cut and pasted the uri so I know that is correct. I used the syntax "event.preventDefault();" in the submit block and that also did not work. I used the action attribute but, it sends the info using the html url and not the url in the ajax property. I am baffled and do not know what I am doing wrong. – Truck35 Dec 19 '17 at 17:45
  • I returned the boolean value false in my submit callback method instead of using the preventDefault() function and I was able to get my ajax function to work. Thanks to everyone who replied to my question. – Truck35 Dec 20 '17 at 22:58

2 Answers2

1

Use the browser developer mode, then look up for the Network tab, there u could see the calls your method is doing, and also the responses. There u will find what is causing the errors.You need to solve those issue to overcome your problems.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Sergio
  • 19
  • 2
  • 1
    I know you are short of reps, but this is really just a comment. This type of answer attracts downvotes or gets flagged ___I did not DV___ and if that happens you will loose rep points and take longer to get to the 50 reps you need to comment on any question. Until then, stick to questions that are well asked and therefore easily answered without needing clarification. http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead – RiggsFolly Dec 19 '17 at 14:48
0

You are missing the action attribute in your form element.The file in action attr is called when submit is clicked

    <form class="ajax" action="./UpdateCard.php">//set action attr here
    <input type="hidden" name="Item" value="Chicken">
    <input type="hidden" name="Price" value="2.99">
    <input type="hidden" name="Qty" value="5">
    <button type="submit">ADD TO CART</button>
    </form>
m.kevin
  • 1
  • 2
  • 1
  • Thanks for the reply. As I was saying, I took out the action attribute because the form sends the information to the url in the form's action attribute and disregards the url in the ajax property. In fact, I think the action attribute causes my application to totally by pass the ajax method altogether and go straight to the server like a normally submitted form. – Truck35 Dec 19 '17 at 18:18