PHP on link click effects - php

I have one question which is somewhat two-parted (though the parts go hand in hand). I've started picking up PHP, and I wanted to do two things when an image is clicked.
I want the click to
Increment a session variables, say $_SESSION['entry'].
Reload the current page (say index.php).
How should I go about this?
To be clear, I'm not asking for someone to code this for me, I'd just like to be pointed in the right direction because I'm not too sure what the best way would be.

Well, anchor links "reload" the page if the href points to the same page. So, all you need to do is tell PHP you want to increment the session variable. You could use a GET variable to do this:
Increment the counter
And then in your index.php:
if (isset($_GET['increment']) && $_GET['increment'] == 'true') {
$_SESSION['counter']++;
}
This assumes you've already initialized the session variable counter at some point. You can check out the wonderful PHP docs to explain the functions used above if you aren't familiar with them.

The way to do this would be to link the image to "itself" $_SERVER['PHP_SELF'] perhaps or just to /index.php, and check the session to see if that value is set, and if so increment it.
<?php
session_start();
if (isset($_SESSION['entry'])) {
$_SESSION['entry']++;
} else {
$_SESSION['entry'] = 1;
}
// if entry is greater than some value in your DB, then set it back to 1

<img src=.../>
<?php if($_GET['incr']) $_SESSION['entry']++; ?>
this should give you the idea.

You could do an AJAX call to a PHP script that increments $_SESSION['entry'].

Load page with image that has a link around it: "?imageClick=1" for instance
On image click the page is therefor automatically loaded
If $_GET[ 'imageClick' ] equals 1 increment the session variable
Redirect to same page without the imageClick variable
If you are concerned that index.php?imageClick=1 may be remembered by the browser in it's history, and therefor can be used to reload without an actual image click:
Load page with a form that has method POST and an input element of type image, named imageClick (acting as a submit button) with value 1
On image button click the form is submitted to the same page
If $_POST[ 'imageClick_x' ] or `$_POST[ 'imageClick_y' ] is set and increment the session variable
Redirect to same page

Related

How can I transfer a value from a page to another?without changing pages

I'm trying to build a website with only 2 pages.
Page 1
In this page theres a buttom and everytime the buttom is clicked it increases the value of the label on page 2.
Page 2
In this page there is only one label a value that is increased by page one
Could someone give me an example
consider the following link as a solution to your problem using AJAX and jQuery.
http://tutorialzine.com/2009/09/simple-ajax-website-jquery/
Go through sessions
In your page one
<?php
session_start();
$_SESSION['myValue']=10; // here your value.
?>
In page two
<?php
session_start();
echo $_SESSION['myValue'];
?>
Another way
<a href='pageone.php?myValue=12'>click</a>
then in any other page, you can access
$_GET['myValue'];
Depending on what resources you have available, you could use AJAX to save the value to the database/XML file on PAGE 1. Then write a script to retrieve the value on PAGE 2.
Alternatively, you could look into saving a SESSION variable on PAGE 1 and retrieve that on PAGE 2.
Session Links:
PHP SESSION Tutorial
PHP Manual - SESSIONS
Js for the button page
var counter=0;
function buttonclick(){
counter++;
window.postMessage(counter);
}
Js for display:
window.onload=function(){
window.addEventlistener("message", handle,false);
}
function handle(event){
alert(event.data);
}
This uses the browsers internal messaging system to transfer the value from one window to another. If you want to transfer it even if page2 is not opened use AJAX to send it to the server and use SESSIONS to store it.

Session variable passing

I've got a php script which builds a html table via echoing data, But i want to add a link onto one of the values and pass that value to the next page.
<td><a href='redirect.php'><?php $_SESSION['WR'] = $row['WorkOrdRef'];echo $row['WorkOrdRef'];?></a></td>
is the line in question but this will only pass the last value added to the table.
Oh, it doesnt work like this. the php code gets executed no matter if you click the link.
I guess the easiest way to do this is to pass it as a get parameter.
html page:
<?=$cellContent?>
redirect.php:
$clickedcell = $_GET['clickedcell']
now the $clickedcell will have the data from the previous page about what cell did the user click.
If you want to use session for some reason, you still have to pass it with GET or POST and store it after the user clicks.
hopefully this is understandable and good luck with your project.
you can change the session by get method also it is possible building by javascript
in the same page add this
if(isset($_GET["clicked"])){
$_SESSION['WR'] = $row['WorkOrdRef'];
$redirect'<META HTTP-EQUIV="REFRESH" CONTENT="0;URL='.$adres.'/"> ';
return $redirect;
}
and then change your url
<td><?php echo $row['WorkOrdRef'];?></td>

Pass url variable on to next page?

I need some help on passing a url php variable onto the next page. I've tried searching throughout the site for help and I've spent a lot of time trying to figure this out with no luck. Basically I need to be able to change the paypal link button id on page 2 with the url variable from page 1.
The variable is initially passed along with the URL: http://www.example.com?p=paypalbuttonid
I would like to store and pass that "p" variable on to the next page. I don't want to pass the variable onto page 2 with a link. I would prefer to store the variable and recall it on page 2.
Page 1 code (above html):
<?php
session_start();
$_SESSION['paypal'] = $_GET['p'];
?>
Page 2 code (above html):
<?php
session_start();
$p = $_SESSION['paypal'];
?>
I'm calling the variable in a link on page 2 (body):
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=<?php echo $p ;?>" target="_blank" class="btn">
I'm not sure what I'm dong wrong but I'm a complete newbie to PHP so please help! The variable shows up blank in the URL on page 2. Thank you! - Chad
First, you should make sure you dont have any output before session_start(), it is safe to put session_start () at the top of the page , especially if you use php code in .html, since you may have output without awareness, and session wont work if you have anything output to the browser before session_start()
according to php.net:
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
and you should check if you cookie is enabled.
Secondly, var_dump ($_SESSION); to see if you can get anything

PHP: Load content dynamically based on the link clicked on the previous page?

I've been Google-ing this for the past 24 hours, pretty much. I can't seem to find an answer that fits my scenario, even though I know it's widely used.
I have many links on one page (let's call it "page 1") of my website and I would like to them all to head to one, blank page (and this "page 2") and then have the content dynamically load, depending on what link was clicked on the previous page.
All I know is that I need to be able to set a PHP variable with the hyperlinks on page 1 and use some kind of if(isset($_SESSION['phpVariable'])){ include('path/content.fileExt')} on page 2 page so that it's ready to receive the variable and load the content accordingly.
I just have no clue how to do this, as I only started learning PHP yesterday. Help? :( :L
You could use the $_GET[] variable. On page1, the links could be to page2?food=bacon or page2?food=foo. On page2, you could say...
if ($_GET['food']=='bacon')
{
// Stuff here that prints what you want.
}
elsif ($_GET['food']=='foo')
{
// Stuff here that prints what you want.
}
You can pass an identifier of the content to be loaded through the URL.
Ex:
Set a value for one of the GET parameter to be the identifier for the content of page 2
Link to Page 2
And then in page 2 just check for the value and load the content accordingly.
include_once 'content/' . $_GET['pageid'];
http://www.w3schools.com/php/php_get.asp
www.example.com/index.php?page=something_here
$_get["page"]
Would return "something_here"

Executing PHP: detecting when a link has been clicked

I've got a bit of a dilemma with some PHP code. The site I'm working on has a "Back to Previous Page" option and I'd like it to behave much like a browser's back button. As it stands right now, I'm using a $_SESSION variable to track what the current and previous pages are. I've also "refresh-proofed" the variables so that I don't end up with both the previous and current pages being the same.
So here's the issue:
With the current implementation, if I go to one page, say "register.php" and then go to "forgot.php", the previous page will be "register.php" which is fine. However, if I click "Back to Previous Page" I'll end up back at "register.php" with the previous page being "forgot.php" which now leaves me with a 2-page loop with going back.
I tried implementing SplQueue to help me keep track of variables and I tried using the dequeue() function in my links to get the last page to show up as the link. The problem comes in when the dequeue is actually called and causes the element to disappear so that if I refresh, the element is no longer in the queue and the link changes. I fixed this by "refresh-proofing" the function that calls the dequeue for me and it works as I would like it to. The problem is now forward-linking. If I direct myself to another page, I don't want the old links to dequeue information.
Ex:
I'm on register.php and my previous page is "forgot.php". The "Back to Previous Page" link accurately shows that "forgot.php" is the page it will direct to, but now it's no longer in the queue, so if I go to another page, say "profile.php" and then use the back button to go back to "register.php", it will no longer show "forgot.php" as the page that you will go to if you hit "Back to Previous Page" again.
So, I guess my question is really how I can make a link call a PHP function without actually calling that function UNTIL the link has been clicked. I've tried having the link point to a JavaScript function, but the JS functions tend to tell me that my queue is empty, which is completely wrong.
As a side note, the pages are a mix of HTML and PHP. The HTML is supplied to me and I've been adding the PHP in to add functionality to fields and to get data from a database. I have no problem using PHP to echo the HTML links if I have to, and if it can be done in HTML with a small <?php someCode(); ?>, that's fine too.
I thank you for your time to try and help me out.
EDIT:
So to try and clarify a bit, I have a structure that is currently tracking pages that the user has already been to as they visit them. It creates a mini history of the pages. My issue is that I have code like this:
Back To Previous Page
And I don't know what "somelink" is since it will change depending on your history. I know I can do something like:
Back To Previous Page
If I do anything like the above, the function is executed as the page is being displayed, so it makes it difficult to use an array_pop() or a dequeue() but again, the PHP will be executed as soon as the page is displayed. What I'm looking for is a way to display the link and then remove it from the history if and only if the "Back to Previous Page" link is clicked. As of right now, I'm storing an array in $_SESSION as was suggested below and since it's an array, I can show the last element in the array as the link, so the only real problem is to find a way to remove elements from the array when the link is clicked.
EDIT 2:
I've been scouring the internet and decided upon using JavaScript with AJAX to call a PHP file. This allows me to us an onClick on the links I have so that I can control when I array_pop from my $_SESSION['links'] variable.
I don't think my AJAX is actually doing anything sadly, so the code I'm using is below.
<script src="js/jquery-1.4.2.min.js" type="text/javascript">
function dequeue()
{
$.ajax({
type: "POST",
url: "common.php",
data: {action: "rem"},
success: function(output) {
alert(output);
}
});
}
</script>
and the PHP is
switch($_POST['action'])
{
case "rem":
array_pop($_SESSION['links']);
break;
default:
if(isset($_SESSION['current']) && $_SESSION['current'] != $_SERVER['REQUEST_URI'])
{
array_push($_SESSION['links'], $_SESSION['current']);
}
$_SESSION['current'] = $_SERVER['REQUEST_URI'];
break;
}
As far as I can tell, this will allow me to add a link to the history in the session variable unless I'm clicking on the "Back to Previous Page" link since that link will have the "rem" code. I'm also a bit suspicious of the $_SESSION['current'] = $_SERVER['REQUEST_URI']; and where it should be placed.
You can store array in a session and treat the array like a stack (use array_push and array_pop accordingly). When the user hits something but the back button, push the current page to the stack. otherwise, pop it.
I would do it like this if I had to:
$_SESSION["history"] = array();
And within the header of every "rememberable" page:
if(in_array($this_page, $_SESSION["history"])) {
unset($_SESSION["history"][array_search($this_page, $_SESSION["history"])]);
}
array_push($_SESSION["history"], $this_page);
What this does is: "If the page exists in the history, remove it from wherever it is and put it as the last page of the history. If not, just put it as the last page of the history". That way you won't have any loops.

Categories