how do i set session variables on click of a link. I have many links on one page. based on the link clicked a session variable has to be set.
For instance:
i have 5 links
<a
href="page1.php">
page1
</a>
<a href="page2.php">
page2</a>
<a href="page3.php">
page3</a>
<a href="page4.php">
page4</a>
<a href="page5.php">
page5</a>
if the first link is clicked then $_SESSION['category']=page1 is to be set
if the second link is clicked then $_SESSION['category']=page2 is to be set and so on..
how do i get this?? any idea??
Your best bet is to set the $_SESSION['category'] at the beginning of each of those php files. If that is not an option, perhaps create a 'page.php' file that accepts a query string.
Something like this:
<?php //example: page.php?p=1
// don't forget to clean $_GET['p']
$p = $_GET['p'];
$_SESSION['category'] = $p;
include("page$p.php");
As gavinbear's suggestion for putting at the top of the page, you could use the following to get a match to your original naming convention:
$requested_uri = $_SERVER['REQUEST_URI']
$requested_path = parse_url($requested_uri)[path]
$_SESSION['category'] = basename($requested_path, ".php")
well you can always mix javascript with php.use the onclick event for the anchor tag. when the link is clicked just set your session in the javascript written for the onclick event.hope this make sense
On each page you will need to set the session.
For example on page1.php :
<?php
session_start();
$_SESSION['category']='page1';
?>
Any content on page1.php
and so on for each page.
If however you are wanting it to sit on the same page, but set the session and use that session to control something on the page (e.g. the category), then you could pass a parameter through the URL and set the session that way, e.g. an example would be page.php?category=page1
<?php
session_start();
if (isset($_GET['category'])) {
$_SESSION['category']=$_GET['category'];
}
?>
Any content on page.php
You don't really need sessions to detect the current page. Instead you can use this:
<?php
$pageName = basename($_SERVER[PHP_SELF]);
?>
I suppose you could then save that to a session variable, but it's going to be overwritten on every page.
Related
TL:DR - I need to pass an anchor tag value to another php page and looking for alternatives to SESSION / COOKIES. Going for forms with POST/GET is discouraged as I would like to avoid having to submit.
Detailed description:
I have a php page which lists a mysqli result set in list form as per the following:
ID fname surname
12 John Doe
13 Carl Brown
Now I would like to have the ID field as a link, which I can achieve using an an HTML anchor tag. Once clicked I would like to redirect to another page and pass the clicked ID (e.g. 12 or 13) for further use.
I know this is achievable through cookies or sessions, but I wanted to check if perhaps there's another way of doing this. Also I would like to avoid forms in order to avoid having to 'submit' the actual form. I'd like to keep the experience as a click and redirect.
To give some context here is my php code:
<?php
include 'functions.php';
session_start(); //Fetches session already initialized in other pages.
$page_fund=new page_fundementals; //simply gets menu items
$conn = new consultant_ops; //executes a mysqli query to get the results used further below
$result=$conn->listPatients($_SESSION['user_id']); //passes the $_SESSION['user_id'] to a listPatients method in order to get the mysqli resultset
foreach($result as $row){
$i=0;
${'p_id'.$i}=$row['p_id'];
${'p_fname'.$i}=$row['p_fname'];
${'p_surname'.$i}=$row['p_surname'];
echo ${'p_id'.$i}." ".${'p_fname'.$i}."<br />";
$i++;
}
?>
have an anchor tag like this
<a href="sample.php?id=<?= $user->id ?>">
<?= $user->name ?>
</a>
then maybe in your sample.php
<?php
if(isset($_GET['id']) {
$user_id = $_GET['id'];
/* your code here to get user from database, maybe
SELECT * FROM users WHERE id = $user_id something like that
then format afterwards */
}
?>
maybe something like that
Why not pass variable in a GET request?
echo "<a href='page.php?pid=".$row['p_id']."'>redirect</a>";
which you can access on the target page.php
$pid = $_GET['pid'];
In anchor tag you can specify query params for eg
12
In this case, when a user clicks on 12 he will be redirected to todo.php?id=12.
In todo.php you can fetch $_GET['id'] and work accordingly.
I hope this isn't stupidly simple. I am completely new to web dev.
I have list items that I styled as buttons.
I want to be able to link to a new page as well as store some information when the list items are clicked. I want to be able to store which list item was clicked in a Session variable.
How do I accomplish this/ is there a better way to accomplish the same thing?
Sessions Step By Step
1- Defining session before everything, Ideally do it on very top of the document so no output is being exposed etc like this
<?php
session_start();
?>
2 - Set your session inside a page and then you have access in that page. For example this is page 1.php
<?php
//This is page 1 and then we will use session that defined from this page:
session_start();
$_SESSION['danish']='danish';
?>
3- Using and Getting session in 2.php
<?php
//In this page I am going to use session:
session_start();
if($_SESSION['name']){
echo 'Your name variable Is Here! :) ';
}
?>
In short its like you assign session variable in a page and then using same declarative syntax instead of assigning you call the variable and PHP do the magic to check if that session variable was created and hold the value so, in short i can write my code like this
First Page
<?php
session_start();
$_SESSION['myvar']='myvalue';
?>
Second page
<?php
session_start();
echo $_SESSION['myvar'];
?>
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
Problem: i need to pass variable value to variable on page 03.php through click on link located on page 01.php and leading to page 02.php. Is it possible? And if it is - how? Thank you.
Store this value in session - use $_SESSION array:
In 01.php:
session_start();
$_SESSION['variable'] = $value;
In 03.php:
session_start();
echo $_SESSION['variable'];
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