Solved

function to execute background process only once and refresh the page

Posted on 2010-11-21
9
524 Views
Last Modified: 2012-05-10
I have a link page that is only available when the user login to the system,  I want to execute a php function only once if the user have not press that link.

If the link is pressed the background process generates a key and insert some values in a database then refresh the page and let the user view the results.

Scenario: a link have not been click
User login in the system;
- there is a list of links
- a link is available it says Run Me (the user have not pressed the link before)
- after pressing the link the page is refresh and the link is now label View Me

Scenario: a link was clicked in a previous session
User login in the system
-there is a list of links
- the link the user pressed in a previous session display View Me

What I would like is the PHP code to make sure when a specific link is pressed once, the option to Run Me is not longer available the user only can see the View Me label.

The goal is to toogle between the options of Run Me and View Me, making sure Run Me is only available if the user never clicked that link before. The link has id available and other values from the database.

thanks for your help!
0
Comment
Question by:a0k0a7
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:Prograministrator
ID: 34185759
I think there is no need to using php here,

css and javascript will do that very will,

like this(for example) :
<html>
<head>
<style type="text/css">
a { color: #000; }
a:visited { color: #999; }
</style>
<script type="text/javascript">
function chklink() {
if(document.getElementById("lnk").style.color == "#999") {
document.getElementById("lnk").innerHTML = "View Me";
} else {
document.getElementById("lnk").innerHTML = "Run Me";
}
</script>
</head>
<body onLoad="chklink()">
<a href="" id="lnk">Run Me</a>
</body>
</html>

Open in new window

0
 
LVL 3

Expert Comment

by:Prograministrator
ID: 34185761
sorry for typos :)
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 34185784
I think I need to be PHP and a database to make sure this link was not clicked prior from a different computer or another browser session.

I am not sure if Javascript code cover that scenario, do you think it will?
0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34185820
when you press the link first time make the log it into databse with all the information regarding browser client. when next time chekc it into DB.

or save the cookie on first time click and chekc that cookie is present or not
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 3

Accepted Solution

by:
Prograministrator earned 500 total points
ID: 34185834
Ooh, I understand now what you want to do,

you're right, javascript not enough,

OK, you need something like this :

<?php
if(isset($_GET['visited'])) {
$sql="update tbl set visited=1 where id=$_SESSION[id]";
mysql_query($sql);
echo "<script>window.location=''</script>";//place here the path that you want to get user to
}
else {
$res=mysql_query("select visited from tbl where id=$_SESSION[id]");
$result=mysql_fetch_array($res);
}
?>
<a href="?visited=1"><?php if($result['visited']==1) { echo "View Me"; } else { echo "Run Me"; } ?></a>

Open in new window


you need to add visited column to user table in your database.

Is that what you want?
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 34185857
@boraganesh the end user can click the link from several machines or a mobile device.

Could you please provide code or pseudo code to make sure the condition of a specific link click only  once is captured?
0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34185871
ok create table as follows

table name link_status
userid  link1 link2, link3, link4
1            1        0       0     0
2            1         1        1    0
3            1         1        1    0
4            1         1        1    0
5           0         0       0    0

when you are creating new user add entry for this user in above tabel with all zeros(meas not licked)
when he clicks on link make it as 1.

use tthis table to verify user has clicked partyicular link or not
0
 
LVL 8

Author Comment

by:a0k0a7
ID: 34185898
@Prograministrator

What does this line does... could you please describe it a little bit,

if(isset($_GET['visited'])) {


In the case I have several links.. does that still works?

Thank you so much for your help, it help me organize my design a lot...
0
 
LVL 17

Expert Comment

by:shinuq
ID: 34186599
if(isset($_GET['visited'])) {

This means the variable "visited" is passed as get method to the script and the the value in the "visited" is not blank.

Hopethis helps
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now