views:

139

answers:

3

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!

A: 

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.

d2burke
onclick works whether or not you have an href.
Dave Aaron Smith
yes, you are correct. this i forgot, then remembered. thx Dave
d2burke
+2  A: 

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.

aularon
Thanks aularon I tried removing hte quotes but, still the same thing, its not activating the link??
Dan
@Dan I edited the post to fix another problem.
aularon
oops! Thanks. I downloaded firebug as dave said and kept clicking stuff till i saw some errors in the bottom right corner! and i can see the additemtouserslist is not defined. I have changed the sript to script, taken out the quotes yet its still undefined. Am i missing some quotes anywhere round here - at the funtion add... bit?<script type="text/javascript">function addItemToUsersList(itemId){ $.ajax({ 'url': 'member-bucketadd-execTEST.php', 'type': 'GET', 'dataType': 'json', 'data': {itemid: itemId}, 'success': function............
Dan
sorry, bit jumbled:this bit<script type="text/javascript"> function addItemToUsersList(itemId)
Dan
@Dan you have a missing comma, just before `'error': function(data)`, I could tell from firebug error message at the console, then I copied your code and tried executing it at the console (you can have a larger console with the arrow, execute then with `CTRL+ENTER`), I kept changing the code to figure out where's the missing part, till I found it, it didn't take much time though, thanks to firebug : ) (Thanks again @Dave for reminding us to not only tell the solution but how to get to it)
aularon
wow! I would never have worked that out! Thank you so much!!!!!
Dan
I will have a look at the console thing, javascript and ajax is something that I have never played with so this is all new. Thank you for the solution! and also for firebug! You guys n gals are great!! :)
Dan
You are welcome anytime : ) we are happy we helped you find some resources to help you debug and do stuff. BTW, don't forget to choose accepted answers for your questions.
aularon
Ahh, didn't realise! Done. Thanks again.
Dan
+5  A: 

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.

Dave Aaron Smith
+1 if you give a man a fish...
Byron Whitlock
This should be a comment, this is not an answer (I agree it's the way to solve this issue, though).
Marcel Korpel
Never used firebug, downloaded and attempting to work it out now ;)
Dan
ah ha... addItemToUsersList is not defined...
Dan