Ajax does not work

Asked

Viewed 209 times

0

I have this code below in ajax:

    <script>
$("a").live("click", function(event) {      
      var targeturl = $(this).attr("href");

      $.ajax({
               type: "get",
               url: "/"+targeturl +"",
               data: "",
               dataType: "html",
               success: function(html){
                   jQuery('#add2').hide().html(html).fadeIn(1000);
                   location.reload();
               }

           });  // closing for  ajax
        event.preventDefault(); 

  });
</script>

And this code in PHP:

while($row = $stm->fetch())  {

  $color = completePayment($row["pago"]);

      "<div id=add9 align=middle style='color: {$color}; text-shadow: 1px 1px 1px black, 0 0 20px blue, 0 0 1px darkblue'>" .$row['pago'] . " <div id=add2 align=middle <br><br> <a id=add2 href=pago.php?id=". $row['id'] ."><img src=images/pago.png><a href=naopago.php?id=". $row['id'] . " ><img src=images/naopago4.png >

I just put in the interesting section of the code. The page has two images (Paid and Not Paid), when I click paid, it takes the user ID and gives an Update in the bank , setting the value YES, and automatically this same value on the page, when it is not paid, does the same process. Bring the information NOT by setting this value in the Bank. The problem that Ajax is not working, when I take Ajax the page runs and when I leave this code, it does not run, I click on the images and nothing happens.

  • What version of jQuery?

  • I have two jquery-1.8.2.js and 1.9.0 arrows

  • I left only 1.9.0 and now it runs, but giving Ubmit to the file that makes the change in the bank, and the right was to stay on the page without giving Ubmit to another right.

  • Can’t have 2 jQuery. Use only one. About what I said above you want the page not to refresh ne?

  • Exactly, with two nothing happened, now with the latest version, it directs to the page that makes the update in the bank and the right was to do the update of the bank without leaving the page.

  • Take that out of the script: location.reload();. There you are asking to reload the page.

Show 1 more comment

1 answer

0

Leave only one Jquery library loaded.

And your Javascript script should look like this:

<script>
$("a").live("click", function(event) {      
  var targeturl = $(this).attr("link");

  $.ajax({
           type: "get",
           url: "/"+targeturl +"",
           data: "",
           dataType: "html",
           success: function(html){
               jQuery('#add2').hide().html(html).fadeIn(1000);
           }

       });  // closing for  ajax
    event.preventDefault(); 

 });
</script>

In php change is part like this:

 while($row = $stm->fetch())  {

 $color = completePayment($row["pago"]);

  "<div id=add9 align=middle style='color: {$color}; text-shadow: 1px 1px 1px black, 0 0 20px blue, 0 0 1px darkblue'>" .$row['pago'] . " <div id=add2 align=middle <br><br> <button id=add2 link=pago.php?id=". $row['id'] ."><img src=images/pago.png><button link=naopago.php?id=". $row['id'] . " ><img src=images/naopago4.png >
  • I did the test by removing this line: Location.Reload();, but the same thing happens. When clicking on the image that contains as href the php file that gives the update in the database, directs to a blank page, setting the values. However it is necessary to remain on the previous screen without directing.

  • @user54154 I edited the answer.

  • @user54154 You may not have href on a button that does not want to be refreshed on the page.

  • @user54154 I changed the href to another attribute.

  • I made the changes to LINK instead of href, the images lost the LINK and nothing happens by clicking on them.

  • Modify the a tag to button. Note that you should have the link attribute inside the tag. And in Javascript you should also have this attribute in the second line.

  • @user54154 Failed to?

  • Hello Willian, yes I did, not in the expected way, I will need to study more my code and understand the real reason for the problem. I came back with Href, giving a Location in the redirected file (paid or naopago.php) to go back to the current page. As soon as I correct, I inform here with the solution. It was just a palliative solution.

  • Buddy, internal redirects aren’t the right one. It’s to make it work like I did last time. In virtually all my projects use this way.

  • You must be doing something wrong.

  • I will calmly evaluate your guidelines, I understand it is wrong yes, it was only palliative. As soon as I finish I give you a feedback on this.I thank you

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.