How do I pass the value with a href
when click href link and go next page? Beside that, on the next page how I retrieve the value?
My code:
<?php
$sql = "SELECT DISTINCT movie.image, movie.name, movie.id
FROM movie
INNER JOIN movie_genre
ON movie.id = movie_genre.movie_id
INNER JOIN genre ON genre.id = movie_genre.genre_id
INNER JOIN movie_date ON movie_date.movie_id = movie.id
ORDER BY timestamp DESC LIMIT 6";
$res = mysqli_query($conn, $sql);
if(mysqli_num_rows($res) > 0) {
while($row = mysqli_fetch_object($res)) {
echo '<label class="label1">' . $row->name . '</label><br><br>';
echo '<a href="#"><img src="' . $row->image . '" class="poster"></a>';
echo '<div class = "section3"><a href="synopsis.php?id="' . $row->id . '" >asd</a>    <a href="">Buy Now</a>';
}
}
?>
in the first page
echo '<a href="next_page.php?next_id='. $row->id.'"><img src="' . $row->image . '" class="poster"></a>';
to retrieve
$_GET['next_id'];
Using $_GET['id'] you will get value pass from URL
You can use _GET of php for this, however there are many methods exist.
First set value in href url as below :
<a href="?key=my_value" .....
Then you can acces value in target php as below:
<?php echo $_GET['key'];//displays my_value
This may help: PHP GET method in <a href>
You simply add your data to the URL in your href and retrieve it on the next page by using the GET Method.
For example in your echo a href
:
echo '<a href="movie.php?id="' . $row->id .'"><img src="' . $row->image . '" class="poster"></a>';
And then in movie.php you would use
$_GET['id'];
To fetch the selected movie.
Assuming your id is an integer.
<?php
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
If you have a query parameter of id, the above will filter and convert the input string into an integer. If it doesn't look like one it will assign false
to $id
.
Note if no value is passed $id
will be null
. So in further code you may need to additionally test:
<?php
if(is_int($id)) {
// Do something.
}