?
Solved

function to execute background process only once and refresh the page

Posted on 2010-11-21
9
Medium Priority
?
530 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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
 
LVL 3

Accepted Solution

by:
Prograministrator earned 2000 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:Shinesh Premrajan
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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

777 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