简体   繁体   English

PHP <a href>如何在单击href时将值传递给另一个页面?</a> <a href>以及如何从另一个页面检索值?</a>

[英]php <a href> how to pass value to another page when click the href? and how to retrieve the value from another page?

How do I pass the value with a href when click href link and go next page? 单击href链接并转到下一页时,如何通过a href传递值? 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>&nbsp &nbsp <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']; 

You can use _GET of php for this, however there are many methods exist. 您可以为此使用_GET的php,但是存在许多方法。

First set value in href url as below : 首先在href网址中设置值,如下所示:

<a href="?key=my_value" .....

Then you can acces value in target php as below: 然后,您可以按如下所示在目标php中访问值:

<?php echo  $_GET['key'];//displays my_value

This may help: PHP GET method in <a href> 这可能会有所帮助: <a href>中的PHP GET方法

You simply add your data to the URL in your href and retrieve it on the next page by using the GET Method. 您只需将数据添加到href中的URL,然后使用GET方法在下一页上将其检索。

For example in your echo a href : 例如,在您的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 然后在movie.php中,您将使用

$_GET['id'];

To fetch the selected movie. 获取选定的电影。

使用$ _GET ['id'],您将从URL获得价值传递

Assuming your id is an integer. 假设您的ID是一个整数。

<?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. 如果您的查询参数为id,则上面的代码将过滤并将输入字符串转换为整数。 If it doesn't look like one it will assign false to $id . 如果它看起来不像一个,它将为$id分配false

Note if no value is passed $id will be null . 请注意,如果未传递任何值,则$id将为null So in further code you may need to additionally test: 因此,在进一步的代码中,您可能需要另外测试:

<?php
if(is_int($id)) {
    // Do something.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM