Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-21-2010, 02:26 AM   PM User | #1
Skippy
New Coder

 
Join Date: Jul 2009
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Skippy is an unknown quantity at this point
No refresh

Hi,

I have 2 links which will insert information into a database.

PHP Code:
<?php
        $q 
mysql_query("SELECT * FROM favourites WHERE username = '$username' AND offerid = '$oID'") or die(mysql_error());
        
$r mysql_fetch_array($q);
        if (empty(
$r)) {
            echo 
'<a href="addfavourite.php?offerid=' $list['id'] . '&username=' $ofusername '"><img src="template/images/afav.png" alt="Add Icon" border="0" /> Favourite</a>';
        }else {
            echo 
'<a href="removefavourite.php?offerid=' $list['id'] . '&username=' $ofusername '"><img src="template/images/rfav.png" alt="Add Icon" border="0" /> Favourite</a>';
        } 
?>
Is there anyway I can get the link to update with addfavourites.php/removefavourite.php without refreshing the page?

Thanks.
Skippy is offline   Reply With Quote
Old 09-21-2010, 01:00 PM   PM User | #2
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
I think so, if I understand what you want.

So... the page initially loads, and the user is shown either the 'add' or 'remove' link, depending on whether the offer is in their favourites. And what you want is that when the displayed link is clicked, the page makes a call behind the scenes to the PHP script, and switches the link over to the other link. Basically, a "toggle favourite" feature. Is that about right?

If so, here's roughly how I'd do it (using jQuery)

Code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    $('.toggle_favourite').click(function(evt){
        evt.preventDefault();        //    stop the link going to the target page
        var $link        = $(this);
        var link_href    = $link.attr('href');
        $.get(link_href, function(response){    // fetch the target page via AJAX
            if (response == 'success'){
                if (link_href.indexOf('addfavourite') > -1){
                    new_link_href = link_href.replace('addfavourite', 'removefavourite')
                    new_link_html = '<img src="template/images/rfav.png" alt="Remove Icon" border="0" /> Remove Favourite';
                }else{
                    new_link_href = link_href.replace('removefavourite', 'addfavourite')
                    new_link_html = '<img src="template/images/afav.png" alt="Add Icon" border="0" /> Favourite';
                }
                $link.attr('href', new_link_href).html(new_link_html);
            }
        });
    });
});
</script>

You'll need to add the "toggle_favourite" class to your links, like:

Code:
<?php
        $q = mysql_query("SELECT * FROM favourites WHERE username = '$username' AND offerid = '$oID'") or die(mysql_error());
        $r = mysql_fetch_array($q);
        if (empty($r)) {
            echo '<a class="toggle_favourite" href="addfavourite.php?offerid=' . $list['id'] . '&username=' . $ofusername . '"><img src="template/images/afav.png" alt="Add Icon" border="0" /> Favourite</a>';
        }else {
            echo '<a class="toggle_favourite" href="removefavourite.php?offerid=' . $list['id'] . '&username=' . $ofusername . '"><img src="template/images/rfav.png" alt="Add Icon" border="0" /> Favourite</a>';
        } 
?>
... and you'll need to alter your PHP script so that it outputs something useful. The javascript above looks for the string "success", so you could, for example, change addfavourite.php to something like:

Code:
<?php
$offerid = $_GET['offerid']
$username= $_GET['username']

// do some stuff

echo('success');
?>
Make sense?
Spudhead is offline   Reply With Quote
Old 09-23-2010, 04:03 PM   PM User | #3
Skippy
New Coder

 
Join Date: Jul 2009
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Skippy is an unknown quantity at this point
Wow, thank you. That was exactly what I was looking for. There is one problem though. I can't seem to get it so the link changes? It submits the link without refresh but the link stays the same.
Skippy is offline   Reply With Quote
Old 09-23-2010, 04:51 PM   PM User | #4
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
Well, there's a few places it could be going wrong. I'm gonna go out on a limb and assume my code is right and that the problem is with the HTTP request.

Do you have Firebug installed? If not, go get it

With the Firebug console open, click one of your links: you should be able to see the HTTP request logged. You'll be able to click on it and see exactly what was sent to your PHP page, and what the response from the PHP page was. If the response wasn't exactly the string "success", then none of the link-swapping code will get called.

So: first question - did the HTTP request fire off OK, and did it respond with "success"?
Spudhead is offline   Reply With Quote
Old 09-23-2010, 09:18 PM   PM User | #5
Skippy
New Coder

 
Join Date: Jul 2009
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Skippy is an unknown quantity at this point
Okay, I installed Firebug and tried what you said. It did respond with success.

Last edited by Skippy; 09-23-2010 at 10:51 PM..
Skippy is offline   Reply With Quote
Old 09-24-2010, 12:30 PM   PM User | #6
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
Oh. Ummm....

Ok, let's do some debugging.

So the PHP page is returning 'success'. That means we're getting to, and past, the IF statement at the start of this bit:

Code:
if (response == 'success'){
                if (link_href.indexOf('addfavourite') > -1){
                    new_link_href = link_href.replace('addfavourite', 'removefavourite')
                    new_link_html = '<img src="template/images/rfav.png" alt="Remove Icon" border="0" /> Remove Favourite';
                }else{
                    new_link_href = link_href.replace('removefavourite', 'addfavourite')
                    new_link_html = '<img src="template/images/afav.png" alt="Add Icon" border="0" /> Favourite';
                }
                $link.attr('href', new_link_href).html(new_link_html);
            }
Add a couple of lines just above the last line:
Code:
$link.attr('href', new_link_href).html(new_link_html);
so it now reads:
Code:
console.log('new_link_href = ' + new_link_href);
console.log('new_link_html = ' + new_link_html);
console.log($link);
$link.attr('href', new_link_href).html(new_link_html);
That should result in some more stuff being written to your firebug console. The first two lines should be fairly self-explanatory, and if those variables are empty or don't contain what you'd expect, there's the problem. The last one should show you something like 'object' with an expandable list of options. If you get "undefined" or anything else, we're not getting a handle on the link object in the DOM for some reason.
Spudhead is offline   Reply With Quote
Old 09-24-2010, 01:45 PM   PM User | #7
Skippy
New Coder

 
Join Date: Jul 2009
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Skippy is an unknown quantity at this point
Okay, after I input the extra code no extra lines are added to the console.
Skippy is offline   Reply With Quote
Old 09-24-2010, 03:41 PM   PM User | #8
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
Ok, can you post the full code of your page? If no extra lines are added, and there are no error messages, then (response == 'success') isn't returning true. Are you definitely sure that's the string that your PHP page is returning?
Spudhead is offline   Reply With Quote
Old 09-24-2010, 08:42 PM   PM User | #9
Skippy
New Coder

 
Join Date: Jul 2009
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Skippy is an unknown quantity at this point
addfavourites.php
PHP Code:
<?php
session_start
();
require (
'connect.php');
require (
'functions.php');
include (
'template/header.php');

if(!isset(
$_SESSION['username'])){
    echo 
'Please <a href="login.php">login</a> to access this area!';
}
    
if(isset(
$_SESSION['username'])){    
    
    
$offerID mysql_real_escape_string($_GET['offerid']);
    
$username mysql_real_escape_string($_GET['username']);
    
    
$result mysql_query("SELECT * FROM offers WHERE id = '$offerid'") or die(mysql_error());
    while(
$list mysql_fetch_array($result)){
        
$cat $list['catID'];
    }
    
    
mysql_query("INSERT into favourites (`username`, `offerid`) VALUES('$username', '$offerID')") or die(mysql_error());

    echo(
'success');
}

include (
'template/footer.php');
?>
This is the code in header.php
Code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    $('.toggle_favourite').click(function(evt){
        evt.preventDefault();        //    stop the link going to the target page
        var $link        = $(this);
        var link_href    = $link.attr('href');
        $.get(link_href, function(response){    // fetch the target page via AJAX
            if (response == 'success'){
                if (link_href.indexOf('addfavourite') > -1){
                    new_link_href = link_href.replace('addfavourite', 'removefavourite')
                    new_link_html = '<img src="template/images/rfav.png" alt="Remove Icon" border="0" /> Remove Favourite';
                }else{
                    new_link_href = link_href.replace('removefavourite', 'addfavourite')
                    new_link_html = '<img src="template/images/afav.png" alt="Add Icon" border="0" /> Favourite';
                }
                console.log('new_link_href = ' + new_link_href);
                console.log('new_link_html = ' + new_link_html);
                console.log($link);
                $link.attr('href', new_link_href).html(new_link_html);
            }
        });
    });
});
</script>
The add favourites button code:
PHP Code:
    echo '<div class="favourite">';
            
$q mysql_query("SELECT * FROM favourites WHERE username = '$username' AND offerid = '$oID'") or die(mysql_error());
            
$r mysql_fetch_array($q);
            if (empty(
$r)) {
                    echo 
'<a class="toggle_favourite" href="addfavourite.php?offerid=' $list['id'] . '&username=' $ofusername '"><img src="template/images/afav.png" alt="Add Icon" border="0" /> Favourite</a>';
            }else {
                    echo 
'<a class="toggle_favourite" href="removefavourite.php?offerid=' $list['id'] . '&username=' $ofusername '"><img src="template/images/rfav.png" alt="Add Icon" border="0" /> Favourite</a>';
            } 
        echo 
'</div>'
Skippy is offline   Reply With Quote
Old 09-27-2010, 09:04 AM   PM User | #10
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
Code:
include ('template/footer.php');
what's in that?

Lets see exactly what comes out of the PHP -

Code:
$.get(link_href, function(response){    // fetch the target page via AJAX
	console.log(response);
    if (response == 'success'){
Spudhead is offline   Reply With Quote
Old 09-27-2010, 02:42 PM   PM User | #11
Skippy
New Coder

 
Join Date: Jul 2009
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Skippy is an unknown quantity at this point
In footer.php is the rest of the template, basically just a few links.

The PHP just returns success.
Skippy is offline   Reply With Quote
Old 09-27-2010, 06:22 PM   PM User | #12
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
If that PHP file is including a bunch of HTML, then it's not returning the string "success": it's returning the string "success" plus a bunch of HTML. Your PHP file shouldn't be outputting any HTML at all.
Spudhead is offline   Reply With Quote
Old 09-27-2010, 07:08 PM   PM User | #13
Skippy
New Coder

 
Join Date: Jul 2009
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Skippy is an unknown quantity at this point
Ah, I removed the header.php and footer.php from the add favourites.

So it's just:
PHP Code:
<?php
session_start
();
require (
'connect.php');
require (
'functions.php');

if(!isset(
$_SESSION['username'])){
    echo 
'Please <a href="login.php">login</a> to access this area!';
}
    
if(isset(
$_SESSION['username'])){    
    
    
$offerID mysql_real_escape_string($_GET['offerid']);
    
$username mysql_real_escape_string($_GET['username']);
    
    
$result mysql_query("SELECT * FROM offers WHERE id = '$offerid'") or die(mysql_error());
    while(
$list mysql_fetch_array($result)){
        
$cat $list['catID'];
    }
    
    
mysql_query("INSERT into favourites (`username`, `offerid`) VALUES('$username', '$offerID')") or die(mysql_error());

    echo(
'success');
}

?>
So it just outputs the word success.

Last edited by Skippy; 09-27-2010 at 07:18 PM..
Skippy is offline   Reply With Quote
Old 09-29-2010, 08:59 AM   PM User | #14
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
Does it work? Is the link now changing?
Spudhead is offline   Reply With Quote
Old 10-01-2010, 04:26 PM   PM User | #15
Skippy
New Coder

 
Join Date: Jul 2009
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Skippy is an unknown quantity at this point
No, the links don't switch and the extra code you told me to put in doesn't return anything. It just prints the word success.
Skippy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:11 AM.


Advertisement
Log in to turn off these ads.