如何将点击图片的名称发送到下一个php页面?

qoefvg9y  于 2023-05-27  发布在  PHP
关注(0)|答案(2)|浏览(91)

我正在制作一个电影票预订网站,我在登陆页面中添加了几张电影海报的图片。我想让它当用户点击一个特定的电影海报的图像时,它会在下一个网页上显示电影的名称。所以问题是我不知道如何通过php使用或不使用表单标签将用户点击的电影名称发送到下一页。
我已经尝试通过给海报ID的图像作为电影名称,并从ID中检索电影的名称为-:

<?php
   $name = $_POST['Name']
?>

这里的'Name'是电影的名字,作为html中图像的ID
但这不管用!”

nwlls2ji

nwlls2ji1#

如果用户只是点击一个链接,那么一般的方法是在该链接上放置一个查询字符串值。
例如:

<a href="movie.php?id=123">your image here</a>

然后在movie.php中,你会得到“123”的值:

$id = $_GET['id'];

您可以使用该$id值查询数据库(或存储数据的任何地方),以获取所需的特定记录。

cnwbcb6i

cnwbcb6i2#

我已经为你创建了一个简单的演示你所要求的使用良好的php安全。你可以把我在这里所做的,并改善它,但你喜欢。我已经尽可能地简化了代码。
此代码的前提是基于安全性。对于这样的事情,总是在查询字符串中传递一个id /数字,并像这样访问数据:
test.php?id=3
我会让你的代码安全和易于管理。
创建一个名为test.php的文件并添加以下代码

<?php
// create array of movie posters, 
// you can also get the list of posters from a database and loop over them the same way
$movie_poster[1] = ['image' => 'image_1.jpg', 'title' => 'Forest Gump'];
$movie_poster[2] = ['image' => 'image_2.jpg', 'title' => 'Lord of the Rings'];
$movie_poster[3] = ['image' => 'image_3.jpg', 'title' => 'Mission impossible'];
$movie_poster[4] = ['image' => 'image_4.jpg', 'title' => 'Indiana Jones'];

// check if user clicked on image
// for security purposes we check if variable is set then
// convert it to an integer. If it is not a number it will equal 0.
// This is for security purposes.
if (isset($_GET['id']) && (int)$_GET['id'] != 0) {
    // user clicked so we display title of image
    $id = (int)$_GET['id']; // convert to integer/number for security
    ?>
    <h1><?php echo $movie_poster[$id]['title']; ?></h1>
    <?php
}

// display the images on the page
foreach ($movie_poster as $id => $mp) {
    ?>
        <div>
            <a href="test.php?id=<?php echo $id; ?>">
                <img src="<?php echo $mp['image']; ?>" />
                <br /><?php echo $mp['title']; ?>
            </a>
        </div>
    <?php
}

相关问题