Link to home
Start Free TrialLog in
Avatar of baz86
baz86

asked on

Disable a link once its clicked

Hello experts.

I am trying to create a link in a php file known as attempt.php which displays a question with four options and user selects one and submits it. Each time the user submits his option, he gets a new question by refreshing the page. So he stays on the same page and keeps answering the questions. Now I want to create a hint answer kind of thing.

I want to create a link clicking on which a new php file namely studenthelp.php opens up which would show him the count of people choosing each option for that question which could act like a hint and the user knows what is the right answer. But once the user clicks and uses this link, even if the page (attempt.php) refreshes after that, he shouldn't be able to see or access the link.

I have tried using onclick() but that only disables the link till the page is not refreshed. As soon as the user refreshes the page to get another question, the link shows up again, which is something I don't want. I have tried using pseudo style sheet attributes (visibility, display) but they don't seem to be working.

Any help or suggestions on this would be appreciated.

Thanks
Baz
Avatar of plymelk
plymelk
Flag of United States of America image

Since you are reloading the page to show the next question, just pass the state of the hint in the POST data.
You stated that you already have the logic working to disable the button until the page is reloaded.  I assume that you are using Java code with the onClick() function.  
Add a line to the Java code to set the value of a hidden parameter for the POST data.
Insert your <form> tags, add an input of type hidden.
        <input type=hidden name="allowHint" value=1>
In your JavaScript, add a line like this:   (assume form name is form1)
        document.form1.allowHint.value = 0
In your PHP, you can choose to hide the button with logic similiar to this:
        if ( $_POST['allowHint'] ) {
              ?>
              <input type=submit name=showHint value=Hint>
              <?php
        }
       
 
just use a cookie, when the user clicks on a link use the php setcookie() function to set a cookie called pages or whatever, then set the value to link numbers such as 1,4,7,12,98 then when the page reloads explode the value of the cookie via the commas and disable the link if the link number is in the cookie list.

or you could do it the same way but with sessions instead of cookies, i can write an example if you want, just ask.
Avatar of baz86
baz86

ASKER

@plymelk:
 I only have one php file where I am writing the code for the link and the other php file has the logic. So there isn't any javascript file as such, the one javascript function I have written is to disable the link once clicked is in the php file itself.

But once this is done, even if the page refreshes, I do not want the link on the page. But the thing happening right now is that the link after refreshing the page again reappaears. This is something I don't want.

So do you think, keeping a hidden value would not display the link even if the page is refreshed? Please let me know if your solution can still be applicable or it has to be tweaked.

@flexibsw:
As you can see I only have one php file displaying the link an other php file that opens up after the link is clicked. Since I am a novice in php, It would be great if you could explain me with the help of an example how do i use session or cookies to not  to display the link again even if the page gets refreshed.

Any help would be appreciated.
echo "<a href=\"http://localhost/moodle/mod/quiz/studenthelp.php\" name=\"help\" target=\"_blank\" onclick=\"this.onclick=function(){return false}\"> Help </a>";

Open in new window

I think you might want to use the session to store this kind of information.  But if you need to keep the information across a longer period than the browser life, you would want to have a data base in the mix.  You could store the client ID and the stateful information in the data base.

The code snippet uses a form to send the checkbox data to the next page.  It stores the checkbox data in the session.  And it remembers the checkbox data permanently, so that once you have fired a checkbox you can never, in effect, un-check it.

You probably want this kind of logic for your app.  Once the client has fired the "help" link, you would set the value in the session and on subsequent pages you would no longer produce the help link.

Regards, ~Ray

PS: A good book to start learning PHP:
http://www.sitepoint.com/books/phpmysql4/
<?php // RAY_checkbox_memory.php
error_reporting(E_ALL);
session_start();

// INITIALIZE SOME TEST DATA - THIS MIGHT COME FROM A DATA BASE
if (!isset($_SESSION['box1'])) $_SESSION['box1'] = FALSE;
if (!isset($_SESSION['box2'])) $_SESSION['box2'] = FALSE;
if (!isset($_SESSION['box3'])) $_SESSION['box3'] = FALSE;

// PROCESS THE POST DATA
if ( isset($_POST['box1'])) $_SESSION['box1'] = TRUE;
if ( isset($_POST['box2'])) $_SESSION['box2'] = TRUE;
if ( isset($_POST['box3'])) $_SESSION['box3'] = TRUE;

// TELL WHAT WAS CHECKED
if ($_SESSION['box1']) echo 'YOU CHECKED box1<br/>';
if ($_SESSION['box2']) echo 'YOU CHECKED box2<br/>';
if ($_SESSION['box3']) echo 'YOU CHECKED box3<br/>';

// CREATE THE FORM
$form = <<<EOFORM
<form method="post">
CHECK OR UNCHECK SOME BOXES HERE<br/>
<input name=box1 type="checkbox" /><br/>
<input name=box2 type="checkbox" /><br/>
<input name=box3 type="checkbox" /><br/>
<input type="submit" value="go" />
</form>
EOFORM;

// REPLACE THE FORM FIELDS TO INDICATE THE CHECKED VALUES IN THE SESSION
if ($_SESSION['box1']) $form = str_replace('box1', 'box1 checked', $form);
if ($_SESSION['box2']) $form = str_replace('box2', 'box2 checked', $form);
if ($_SESSION['box3']) $form = str_replace('box3', 'box3 checked', $form);

// PRESENT THE FORM
echo $form;

Open in new window

Try this example:
<?
$link = $_GET['link'];
$linkcookie = $_COOKIE['studentlink'];

$one = 'studenthelp.php?q=1';
$two = 'studenthelp.php?q=2';
$three = 'studenthelp.php?q=3';
$four = 'studenthelp.php?q=4';
$five = 'studenthelp.php?q=5';

$oner = '';
$twor = '';
$threer = '';
$fourr = '';
$fiver = '';

if($link == '1'){
	$one = '#';
	$oner = 'return false;';
	setcookie('studentlink', $linkcookie . $link . ',');
}
if($link == '2'){
	$two = '#';
	$twor = 'return false;';
	setcookie('studentlink', $linkcookie . $link . ',');
}
if($link == '3'){
	$three = '#';
	$threer = 'return false;';
	setcookie('studentlink', $linkcookie . $link . ',');
}
if($link == '4'){
	$four = '#';
	$fourr = 'return false;';
	setcookie('studentlink', $linkcookie . $link . ',');
}
if($link == '5'){
	$five = '#';
	$fiver = 'return false;';
	setcookie('studentlink', $linkcookie . $link . ',');
}

$exp = explode(',', $linkcookie);

foreach($exp as $link){
if($link == '1'){
	$one = '#';
	$oner = 'return false;';
}
if($link == '2'){
	$two = '#';
	$twor = 'return false;';
}
if($link == '3'){
	$three = '#';
	$threer = 'return false;';
}
if($link == '4'){
	$four = '#';
	$fourr = 'return false;';
}
if($link == '5'){
	$five = '#';
	$fiver = 'return false;';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Links</title>
</head>

<body>
<script type="text/javascript">
function clink(linkid){
	window.location = "links.php?link=" + linkid;
}
</script>
<p><a id="1" href="<? echo $one ?>" target="_blank" onclick="clink('1');<? echo $oner ?>">Link 1</a></p>
<p><a id="2" href="<? echo $two ?>" target="_blank" onclick="clink('2');<? echo $twor ?>">Link 2</a></p>
<p><a id="3" href="<? echo $three ?>" target="_blank" onclick="clink('3');<? echo $threer ?>">Link 3</a></p>
<p><a id="4" href="<? echo $four?>" target="_blank" onclick="clink('4');<? echo $fourr ?>">Link 4</a></p>
<p><a id="5" href="<? echo $five ?>" target="_blank" onclick="clink('5');<? echo $fiver ?>">Link 5</a></p>
</body>
</html>

Open in new window

Avatar of baz86

ASKER

Well in my program, there would be no sub-sequent pages. Always there would be just one page that would be refreshed every time. And whenever the page gets refreshed, a new question shows up. I do not want to store the state of any question or the option selected by the user.

I am trying to build up a "Millionaire" kind of feature. The "Help" link should just appear once when the student clicks it, if he doesn't know what to answer to a question. The logic for the other file is build already. I am not concerned about that.

All I want is this link is like a "Lifeline". Once the user uses this link, even if the page refreshes and the same page shows up but with a different question, the link should not come as the user has already used it for some previous question.

So I am unable to grasp the codes written above. If someone can let me know how do I modify them according to my condition, it would be of great help.

Thanks
the code i posted simplt sets a cookie when the user clicks on the link, if the cookie is then present then the link is disablerd, if not then its enabled. even with page refreshes the link will still be disabled.

my code will work for what you need, if the user clicks on the link then it opens up the help and disables itself, if the page is refreshed then that link is still disabled.
Avatar of baz86

ASKER

OK. I would try it out and let you know in a  while.

Thanks
Here, this is the code i posted, on a test server, try it out and you will see what it does, when the link is first clicked the popup opens, however anytime after that it is not, even after page refreshes.

http://www.fwsw.co.uk/Test/links.php
Avatar of baz86

ASKER

Well I did try your code and your test file. Your test file seems to be working perfectly fine and thats what I am looking for. But for my code, even after clicking the link once, when I refreshed the page containign the link and try clicking the link, the link di open up again.

And since I only need one link, I have not included the 2,3,4 and 5 part of the code which prints the rest of the 4 links. Also I don't think I need 'studenthelp.php?q' . I think there would be only 'studenthelp.php'.
 
Also in "windows.location" part, I am sure "links.php would not come but I don't know what would come in its place.

I got few errors and I am still in the process of understanding your code line by line. I am sorry but I am sure I have made some silly mistakes. Can you help  me out with the errors please?

I am attaching the code I have written from your code. Please let me know if you don't understand any part of the modifications I did.

The error I am getting on the page where the link is placed is:
"Notice: Undefined index: link in C:\xampp\htdocs\moodle\mod\quiz\attempt.php on line 535

Notice: Undefined index: studentlink in C:\xampp\htdocs\moodle\mod\quiz\attempt.php on line 536"

And the page (studenthelp.php) that should open up after that throws this error:
"Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.
Error 404
localhost"

Thanks

PS: Since my entire code is in a .php file, I have made some changes regrading the  and <script> tag.
$link = $_GET['link'];
	$linkcookie = $_COOKIE['studentlink'];

	$one = 'studenthelp.php';
	$oner = '';
	if($link == '1'){
	$one = '#';
	$oner = 'return false;';
	setcookie('studentlink', $linkcookie . $link . ',');
	}

	$exp = explode(',', $linkcookie);

	foreach($exp as $link){
	if($link == '1'){
		$one = '#';
		$oner = 'return false;';
		}
	}


echo "<script type='text/javascript'>";
echo "function clink(linkid){window.location = 'links.php?link=' + linkid;}";
echo "</script>";
echo "<a id='1' href=' echo $one ' target='_blank' onclick='clink('1'); echo '$oner'>Link 1</a></p>";

Open in new window

was that your entire php file?

and the studenthelp file must be on your server, thats why you got that error.

and try this code, ive tidies it up a bit and fixed your errors.
<?
if(isset($_GET['link']){
	$link = $_GET['link'];
} else {
	$link = '';
}

if(isset($_COOKIE['studentlink']){
	$linkcookie = $_COOKIE['studentlink'];
} else {
	$linkcookie = '';
}

$phpself = basename(__FILE__);
$_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], $phpself)) . $phpself;

$one = 'studenthelp.php';
$oner = '';
	
if($link == '1'){
	$one = '#';
	$oner = 'return false;';
	
	setcookie('studentlink', '1');
}

if($linkcookie == '1'){
	$one = '#';
	$oner = 'return false;';
}


echo "<script type='text/javascript'>";
echo "function clink(linkid){window.location = '" . $_SERVER['PHP_SELF'] . "?link=' + linkid;}";
echo "</script>";
echo "<a id='1' href='" . $one . "' target='_blank' onclick='clink('1');" . $oner . ">Link 1</a></p>";

?>

Open in new window

oh and you can have html inside a php file by the way, you just need to open and close the php tags in the appropriate places like i did in my example.
Avatar of baz86

ASKER

I replaced the code with the code you gave now. I ain't getting an errors. Link gets displayed properly, the studenthelp.php page also opens up on the display of the link properly.

But if I try refreshing the page that contains the link, and then try clicking the link, the link is still accessible and it does show the studenthelp.php page again. So basically this shouldn't be happening right?

What do you think is happening wrong in the code? Also the file that displays the link has a lot of code, so the only bit that this link is concerned with I have pasted that code.If you still think you would want to see the entire code, then I can paste it. Coz I am working with moodle. So the code has some already written in-built functions which is responsible for displaying a  Quiz. And I am making modifications in that.

Please see and let me know why is the same logic not working with this code.

Thanks

if(isset($_GET['link'])){
	$link = $_GET['link'];
} else {
	$link = '';
}

if(isset($_COOKIE['studentlink'])){
	$linkcookie = $_COOKIE['studentlink'];
} else {
	$linkcookie = '';
}

$phpself = basename(__FILE__);
$_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], $phpself)) . $phpself;

$one = 'studenthelp.php';
$oner = '';
	
if($link == '1'){
	$one = '#';
	$oner = 'return false;';
	
	setcookie('studentlink', '1');
}

if($linkcookie == '1'){
	$one = '#';
	$oner = 'return false;';
}


echo "<script type='text/javascript'>";
echo "function clink(linkid){window.location = '" . $_SERVER['PHP_SELF'] . "?link=' + linkid;}";
echo "</script>";
echo "<a id='1' href='" . $one . "' target='_blank' onclick='clink('1');" . $oner . ">Link 1</a></p>";

Open in new window

there were some errors in the file,

try this one:
<?
error_reporting(E_ALL);
ini_set('display_errors', '1');


if(isset($_GET['link'])){
	$link = $_GET['link'];
} else {
	$link = '';
}

if(isset($_COOKIE['studentlink'])){
	$linkcookie = $_COOKIE['studentlink'];
} else {
	$linkcookie = '';
}

$phpself = basename(__FILE__);
$_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], $phpself)) . $phpself;

$one = 'studenthelp.php';
$oner = '';
	
if($link == '1'){
	$one = '#';
	$oner = 'return false;';
	
	setcookie('studentlink', '1');
}

if($linkcookie == '1'){
	$one = '#';
	$oner = 'return false;';
}


echo "<script type='text/javascript'>";
echo "function clink(linkid){window.location = '" . $_SERVER['PHP_SELF'] . "?link=' + linkid;}";
echo "</script>";
echo '<a id="1" href="' . $one . '" target="_blank" onclick="clink(\'1\');' . $oner . '">Link 1</a></p>';

?>

Open in new window

Avatar of baz86

ASKER

This code got me errors in my existing file too. When I click on the link, the URL of the existing file changes too,which should not happen. The studenthelp.php does open up, but then the page having the link starts throwing up errors. I think its because its taking the link id in the url, and the quiz won't operate with any extra unknown parameters in the url.

This is the error in the file containing the link:

There is no quiz with id 0
Stack trace:

    * line 1664 of lib\deprecatedlib.php: call to debugging()
    * line 41 of mod\quiz\attempt.php: call to error()

error_reporting(E_ALL);
ini_set('display_errors', '1');


if(isset($_GET['link'])){
	$link = $_GET['link'];
} else {
	$link = '';
}

if(isset($_COOKIE['studentlink'])){
	$linkcookie = $_COOKIE['studentlink'];
} else {
	$linkcookie = '';
}

$phpself = basename(__FILE__);
$_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], $phpself)) . $phpself;

$one = 'studenthelp.php';
$oner = '';
	
if($link == '1'){
	$one = '#';
	$oner = 'return false;';
	
	setcookie('studentlink', '1');
}

if($linkcookie == '1'){
	$one = '#';
	$oner = 'return false;';
}


echo "<script type='text/javascript'>";
echo "function clink(linkid){window.location = '" . $_SERVER['PHP_SELF'] . "?link=' + linkid;}";
echo "</script>";
echo '<a id="1" href="' . $one . '" target="_blank" onclick="clink(\'1\');' . $oner . '">Link 1</a></p>';

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of flexiwebsw
flexiwebsw
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of baz86

ASKER

This solution is working perfectly fine. Thanks a lot. The link gets clicked only once and then its not accessible.

But I just got a last thing to ask, that right now, as some X user I am accessing the link from say netscape browser. The link is accessible once and then its not. And then even If i try to log in again an take the quiz as the same X user or as a different Y user , but the same browser still the link doesn't open. So basically any other time from same browser, link doesn't open up.

However if i sign in from different browser, like Mozilla, then the link again opens up. But then If i try to log in as different user in mozilla itself, it again doesn't open up.

So I am guessing its because of the cookie, its opening up every browser only once. So can something be done about this or this is how it would function as the cookie/session is stored for each browser.

Because in this way, the link never opens up in future once its accessed. If this happens then I would never be able to access the link ahead.

Please let me know this.Otherwise code is working perfectly fine as its supposed to be.

Thanks.
the only other way would be to use a database, and to store which users have accessed the link, then it would stop that user form accessing thwe link no matter what browser they are using, and multiple users could do it on the one browser.
Avatar of baz86

ASKER

The file I am working with displays a quiz, so the user should only not be able to see the link if he is taking the quiz in that attempt and has already accessed the link.

Once the user logs out and again logs in to take the quiz, he should again be allowed to access the link, irrespective of which ever browser he is taking and even if he did access the link in the previous attempt of the quiz.

So how would the database thing help?

Also I am thinking If i create a hidden value that has initial value 0, and on the onclick event, a javascript code that increments the value from 0 to 1.. Then if the value is changed from what it was, is checked by PHP code (by isset, $_GET/$_POST).
And if its 1, then the link is not displayed at all else the link is displayed. Do you think this logic is right, and if it is, can u tell me how to do the hidden value incrementing and checking whether it is incremented or not bit.

Thanks
no that will not work, of the user refreshes the page the data of the objects are not sent to the php code unless the user submts a form.

you would have to integrate the code into the moodle app and database to re-try the quiz with the link enabled again.
Avatar of baz86

ASKER

And how am I supposed to do that?

Because I thought, that incrementing the hidden field on click of link by writing a javascript function can be done. And if the value has been incremented from 0 to 1, then link would be displayed else not.

But if you say that this won't work, then how to implement what ever you have mentioned above.

Can you elaborate on that?

Regards
Baz
you will need to use a database to store whether or not the user has used the help in this try of the question, i do not use moodle so i dont know exactly how to implement it into the moodle site, however a standard database could be used and just reset the help file value when the user starts the quiz over again.
Avatar of baz86

ASKER

Thanks for the solution. Would try it out surely. And once again, Thanks a lot so far for all your help and suggestions. I appreciate that.


Regards
Baz