简体   繁体   中英

Update database without reloading page

Ive just had some fantastic help from Sandeepan, thank you!

Please can anyone see what I have done wrong with this ....

<head>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javasript">

function addItemToUsersList(itemId)
{
  $.ajax({
      'url': 'member-bucketadd-execTEST.php', 
       'type': 'GET',
      'dataType': 'json', 
      'data': {itemid: itemId}, 
       'success': function(data) 
       {
           if(data.status)
           {
               if(data.added)
                {
                        $("span#success"+itemId).attr("innerHTML","Item added to your personal list");
                 }
                 else
                 {
                        $("span#success"+itemId).attr("innerHTML","This item is already on your list");
                }
            }
       },
       beforeSend: function() 
         {
               $("span#success"+itemId).attr("innerHTML","Adding item to your bucketlist...");
         }
          'error': function(data) 
          {
          // what happens if the request fails.
            $("span#success"+itemId).attr("innerHTML","An error occureed");
        }
});
                  }
     </script>

</head>

Then the button to activate the function in :

   <a onclick='addItemToUsersList("<?php echo $itemid ; ?>")'> Add<img src='images/plus-green.png' /> </a>

and the exec page:

<?php 

if($bucketlist < 1) 
 {
    mysql_query("INSERT INTO membersbuckets (memberbucketid, userid, bucketid, complete)
    VALUES ('', '$userid', '$_GET['itemId]', '0')");
        return json_encode(array("status" => true, "added" => true));
 }
 else
 {
        return json_encode(array("status" => true, "added" => false));
 }



//echo "You are being directed to your bucket list, please wait a few moments...<meta http-equiv='Refresh' content='2; URL=mybucketlist.php'/>";
?> 

The links are showing up as links but nothing happens when I click them!! Here is the test page I am working on http://olbl.co.uk/showbucketsTEST.php Thank you in advance!

Here's how I'd figure it out. Open the page in Firefox with Firebug turned on. Turn on the "console."

If it's a Javascript error you'll see it. Firebug displays ajax traffic too so you can open your ajax requests and see if you got a server error.

change

<a onclick="addItemToUsersList("<?php echo $itemid ; ?>")">

into

<a onclick="addItemToUsersList(<?php echo $itemid ; ?>)">

(quotes inside quotes are causing a problem.)

EDIT: Also, change:

<script type="text/javasript">

into

<script type="text/javascript">

As suggested by @Dave, I traced the problem with firebug, and it said addItemToUsersList is not defined, so I searched for it in the content and found the typo in the <script> tag.

If I'm not mistaken, you must have an 'href' attribute for the browser to recognize this as a link. Try adding href="javascript:void(0)" to the links. A # would make the page refresh.

UPDATE: Hmm, this is true about not being a 'link' but with jQuery the event should fire regardless. I'd still try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM