Klaus Jasper
Klaus Jasper

Reputation: 89

PHP - How to retrieve session in php

I created a table that contains id - names - jobs and page that shows the names only and beside each name there is button Job and session that contains the id. this is my code

$query = mysql_query("SELECT * FROM table");
while($fetch = mysql_fetch_array("$query")){
$name = $fetch['names'];
$id = $fetch['id'];
echo '</br>';
echo $name;
$_SESSION['name'] = $id;
echo "<button>Job</button>";
}

I want when the user click on button Job redirect to a page that contains the job of that session. so how can I do it?

Upvotes: 0

Views: 265

Answers (3)

Ivin
Ivin

Reputation: 4845

There's an easier way than using session. Because the way you're using session, only one ID can be saved at once.

Instead of button, try using a link with ID as a param.

<a href="link-to-target-page?id=$id">Job</a>

And on that target page

$id = $_GET['id'];

Now you have the ID in the target page. If you want to load lot more stuff than just a job, try pulling from DB.

On the other hand if you want to show only a job here, this entire thing is a bad implementation. Cause dont make the user redirect to a new page just to see a job title. Allow him to see it in the first page itself.

Upvotes: 0

vladkras
vladkras

Reputation: 17227

you rewrite your $_SESSION variable each loop, so it will contain only tha last id from table

the only way I see, is to make a link <a href="?job_id=$id>Job</a> and query exact job on the page like SELECT * FROM table WHERE id=$_GET['job_id']

anyway you have to write it somewhere in html, because server-side script need to know what id are you looking

Imo you don't really understand what's happening between client and server sides, but I cannot provide you a link to a tutorial, because it is a long time when I used them last time

Upvotes: 0

AMADANON Inc.
AMADANON Inc.

Reputation: 5929

Replace the echo line with:

echo "<button onclick='document.location=\"show_job.php?id=$id\">Job</button>";

By the way, storing the $id in $_SESSION won't work - there is only one $_SESSION per web session, and since you are listing different jobs, $_SESSION['id'] will contain the LAST $id.

Upvotes: 1

Related Questions