I'm trying to get a session variable to be set when clicking on a link.
The value of the variable is generated out of a DB using PHP. I've tried everything, but noting seems to work.
There is a foreach-loop that displays usernames. Each username is a link to a second page. The username that is clicked on should become the session variable.
The code that loops looks like this:
echo "<a href= 'nominate.php'> ".$myotherusers['username']. "</a><br/>";
goal being that when clicked on the link this code is executed:
$_SESSION['varname'] = $myotherusers['username'];
I just can't seem to be able to figure out how...
thanks!
You could do something like this:
echo "".$myotherusers['username']."<br />";
and then in your action:
$_SESSION['varname'] = $_GET['user'];
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 am creating a set of 3 pages, each with a dropdown menu. The values from the previous dropdown menus will populate the next dropdown menu. I understand that I must use session variables to hold the value of each dropdown menu into later pages. As a simple test, I echo the variables to see if they have been carried over -- and that's where the problem is.
I have three different files: choose_cc.php, choose_uni.php, and choose_major.php
The value from the dropdowm menu in choose_cc.php does get echoed in choose_uni.php -- however the value from choose_cc.php does NOT get carried over into choose_major.php -- despite me storing it into a session variable.
the flow of pages is like this;
choose_cc.php --> choose_uni.php --> choose_major.php
Each php file has it's own form. The problem lies in when I try to call the value from choose_cc.php into choose_major.php, i have issues.
The name of the form on choose_cc.php is choose_cc.
The name of the form on choose_uni.php is choose_uni.
So for example, in choose_uni.php, I retrieve the value from the dropdown menu on the previous page (choose_cc.php) like this:
$_SESSION['choose_cc'] = trim($_GET['choose_cc']); //fetches cc from previous page
$user_cc = $_SESSION['choose_cc'];
echo $user_cc;
and when I echo it as I did above, it works! Okay perfect!
But when I head onto choose_major.php, I try retrieving the value again from the form, but to no avail like this;
echo $_SESSION['choose_uni']; //this works
echo $_SESSION['choose_cc']; //this doesn't work
I have made sure to store to do session_start() on the beginning of each page as well.
Please help me out! this is driving me insane!
Create a new "see.php" with this:
session_start();
print_r($_SESSION);
and execute it after each choose_cc.php, choose_uni.php and choose_major.php to take a look the session you have after you run your programs.
Simple as this:
a) add session_start(); at the beginning of ALL the involved pages. Some weird stuff happens sometimes if you put a space before it, or even a new line. So be sure it is the very first thing in your script.
<?php
session_start();
?>
b) if needed, check the variable to save into session for not empty(); That way you can be sure the session variable contains something, unless of course you want explicitly to be empty.
c) then load the session variable. You can temporary check it with a var_dump();
<?php
session_start();
if (!empty(trim($_GET['choose_cc'])))
{
$_SESSION['choose_cc'] = $_GET['choose_cc'];
}
var_dump($_SESSION['choose_cc']);
?>
I have read a value from a form as input form the user. the form is POST.
<form action="form_handler.php" method="POST">
Then, in the form_handler.php page I saved this value in a variable. $productID=$_POST['product'];
Then, I want to pass this variable via link to another page as:
echo "<a href='products.php?prodID=".$productID."' title='Products
Page' class='whatEver'>click here for product details</a>";
When I click the link, I see the value in the link. But, inside the page products.php I want to make MySQL query for the product details as:
$sql = "SELECT * FROM products WHERE prodID = '$productID'";
I get 0 results. I also tried to echo the $productID value and it seems empty and not the value that I saw in the URL.
What is my mistake please? How can I make the database query to fetch the product details based on the productID variable I passed in the link?
NOTE: I am trying to make a demo for MySQL injection vulnerability. Please, ignore security issues here.
You can not get like that the productID .. You have to put `$productID = $_GET['prodID']; ' the you can use that variable with sql query .
When you sent variables in a link followed by ?var=value
You need to get it with something like
$variable = $_GET['var'];
In your case your products.php
should have $prodid = $_GET['prodID'];
echo "<a href='products.php?prodID=".$productID."' title='Products
Page' class='whatEver'>click here for product details</a>";
Looks to me like you are passing it into products.php as prodID.
Could you try using:
$sql = "SELECT * FROM products WHERE prodID = '$prodID'";
Instead?
Just like before when you used $productID=$_POST['product'];,
you need to do the same thing to use a variable in products.php, except instead of a post variable, it's a get variable (because it's in the url). Use $productID=$_GET["productID"]
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>
I have a feature on my users inbox that allows users to check/uncheck messages in their inbox that they want to make favourite.
I'm currently testing what happens when a user checks the box (clicks on the image and causes it to go from greyed out to colour meaning the box is checked).
Anyway as you can see from the code below when the box ischecked this url is suppose to be loaded: http://mysite.com/messages/favourite_checked
The message_id of the row the user has checked the box on is suppose to be added onto the end of the url this then loads my controller "messages" and method "favourite_checked" which then passes a variable that grabs the message_id from the url, stores it in a variable then sends it the my model and it is used in a mysql query.
Basically I update the favourites column of my messages table and set it to = 1 where the message_id from url matches the one in the messages table in my database. So yea, where the match is found the "favourite" column in that row is updated to 1. 1 = favourite 0 = not favourite.
Any I just thought I would make it clear what was happening..
My problem is nothing happens when I check the box, nothing is updated so I feel I must be doing something wrong where I try to add the id to the url in the javascript function.
I've tried $(post) also.. nothing happens then also.
Maybe someone can spot it because I really don't know what the problem is.
<script type="text/javascript">
// favourite check box
$('input.favourite:checkbox').simpleImageCheck({
image: '<?php echo base_url()?>images/messages/check.png',
imageChecked: '<?php echo base_url()?>images/messages/unchecked.png',
afterCheck: function(isChecked) {
if (isChecked) {
//query to db from php to update favourite number to 1
$.get('http://mysite.com/messages/favourite_checked'+'<?php foreach ($query as $row): ?><?php $row['id']; ?><?php endforeach; ?>');
}
// else (!isChecked)
// {
// //query to db from php to update favourite number to 0
// $.get('http://mysite.com/messages/favourite_unchecked');
// }
}
});
</script>
I think your basic problem is some confusion about when the PHP is running vs the javascript.
The PHP you put on the page is server side, it will load first, then the javascript will run client-side.
This part here:
$.get('http://mysite.com/messages/favourite_checked'+'<?php foreach ($query as $row): ?><?php $row['id']; ?><?php endforeach; ?>');
Seems like you are wanting this to be dynamic based on what you checked, but I don't see how that url is going to show specifically what you are looking for.
About the PHP:
I think you want to replace this:
<?php $row['id']; ?> // does nothing
with this:
<?php echo $row['id']; ?> // echo's the id
Although I´m not sure that that will work as the loop you have there will generate a strange url, just adding all id's...
About the javascript:
I´m not familiar with the simpleImageCheck() function you are calling, but does it have an onClick or onChange event handler? Otherwise I don´t see your code being run at all.