Page 1 of 1

Determining if link was pressed

Posted: Wed Jun 16, 2010 12:16 pm
by Smudly
Hi, I am trying to store a variable into my database upon a user's click.

Once a user clicks on the link, a value of 100 should be added to their score.
The page includes 2 frames. The top has the button "Next" (once it's clicked, the points are added).
The bottom, shows a random website from a list in my database.
How could write a script that could do something like this?

The script uses the users table. This has information like the user's id which will be used to determine which user is gaining the points, and exp, which is where the points should be added.

Database Version: 5.0.91
Table information:
Name: users
[text]id int(11) No
username varchar(25) No
level int(2) No 1
email varchar(64) No
fname varchar(25) No
lname varchar(25) No
member tinyint(1) No
referrer varchar(25) No
joindate date No
lastsurfed date No
credits decimal(9,3) No
exp int(6) No
password varchar(32) No
ip varchar(15) No[/text]

Here is my current code. This is surfbar.php

Code: Select all

<?php

session_start();

?>

<html>
<head>
<title>My Traffic Exchange</title>
<link rel="stylesheet" type="text/css" href="styles/surfbar.css" />
<script type="text/javascript">

var time = 2;

function startCountdown(){
    var t = setTimeout("countdown()", 1000);
}

function countdown(){
    --time;
    if(time == 0){
        document.getElementById("countdown").innerHTML = "<a href='surf.php'>Next</a>";
    }else{
        document.getElementById("countdown").innerHTML = time;
        var t = setTimeout("countdown()", 1000);
    }
}


</script>
</head>
<body onload="startCountdown();" class="surfbar" bgcolor="#333333">

<!-- Surfing Container -->

<div id="container">

<!-- Logo -->
<div id="logo" align="center">
	<img src="img/ad.jpg" alt="ptc" width="150" height="48"><br />
    <a href="member.php">Members Area</a>
</div>
<!-- Exp -->
<div id="exp" align="center">
	<p><strong>Experience:</strong><p>
</div>
<!-- Timer -->
<div id="timer" align="center">
	<div id="countdown">2</div>
	<p><strong>
    Total Surfed: 78<br />
    Credits Earned: 78<br />
    </strong></p>
</div>
<!-- Bonus -->
<div id="bonus" align="center">
	<p><strong>Surf 25 More for <br>a 25 Credit Bonus!</strong></p>
</div>
<!-- Banner -->
<div id="banner" align="center">
	<img src="img/ptc.jpg" alt="ptc">
</div>

</div>

</body>
</html>
// surf.php

Code: Select all

<?php

session_start();
include('inc/connect.php');
$userid = $_SESSION['userid'];

// Select a Random Website
$sitequery = "SELECT users.id, users.credits, users.username, websites.id, websites.url, websites.userid, websites.active
FROM users JOIN websites ON websites.userid = users.id
WHERE websites.active='yes' and users.credits > 0 order by rand() LIMIT 1";

$siteresult = mysql_query($sitequery) or die("Error in query: $sitequery. " . mysql_error());

if (mysql_num_rows($siteresult) != 0){
	$siterow = mysql_fetch_array($siteresult);
	$url = $siterow["url"];
}
else{
     echo 'error';
}
?>
<html>
<head>
<title>My Traffic Exchange</title>
<script>
top.surfbar.location = 'surfbar.php'
top.Site.location = '<?php echo $url; ?>';
</script>
</head>
<frameset rows="80,*" BORDERCOLOR="#222222" BORDER="3">
          <frame src="surfbar.php" name="surfbar" marginwidth="O" marginheight="0" NORESIZE SCROLLING="no" />
          <frame src="<?php echo $url ?>" name="Site" marginwidth="O" marginheight="0" noresize scrolling="auto" />
</frameset>
<noframes>
<body><p>Sorry, but your Browser does not support Frames. Please open this page in a new browser.</p></body>
</noframes>
</html>
I just need help determining how to determine if Next was clicked, and if it was, add 100 to the exp in the database.

Here is some basic pseudocode of how I think it could work:

Code: Select all


include('inc/connect.php');
$userid = $_SESSION['userid'];

$expquery = mysql_query("SELECT `exp`, `level` FROM users WHERE id=$userid");
$row = mysql_num_rows($expquery);

while($row = mysql_fetch_assoc($expquery))
{
    $exp = $row['exp'];
}
if (surfed){
$newexp += 100;
}
$inputexp = mysql_query("UPDATE users SET exp='$newexp' WHERE id='$userid'");

Re: Determining if link was pressed

Posted: Wed Jun 16, 2010 1:41 pm
by internet-solution
There are a number of ways to do this.
1. You can use javascript

[syntax]document.getElementById("countdown").innerHTML = "<a href='surf.php' onclick='javascript:increament()'>Next</a>";[/syntax]

in increment() function you can use Ajax to update MySQL via PHP

2. You can replace <a> tag with form

[syntax]document.getElementById("countdown").innerHTML = "<form method='post' action='surf.php'><input type=submit name='next' value='Next' /></form>";[/syntax]

and then in surf.php check

Code: Select all

isset($_POST['next'] )
and if set, increment value in database.