skip to Main Content

I am making a website for movie ticket booking , I have added several images of the movie posters in the landing page. I wanted to make it as when the user clicks on the image of a particular movie poster it displays the name of the movie on the next webpage. So the problem is I don’t know know how to send the name of the movie the user has clicked on to the next page through php with or without using the form tag.?

I have tried by giving the images of the posters ID’s as the movie name and retrieve the name of the movie from the ID as-:

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

here ‘Name’ is the name of the movie given as the ID of the image in html

but this does not work!`

2

Answers


  1. If the user is only clicking on a link then the general approach is to put a query string value on that link.

    For example:

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

    Then in movie.php you’d get the "123" value with:

    $id = $_GET['id'];
    

    And you’d query your database (or wherever you store your data) using that $id value to get the specific record you’re looking for.

    Login or Signup to reply.
  2. I have created you a simple demo for what you are asking using good php security. You can take what I have done here and improve on it however you like. I have made the code as simple as possible.

    The premise of this code is based on security. For something like this always pass an id / number in the query string and access your data like this:
    test.php?id=3

    I will keep your code secure and easy to manage.

    Create a file named test.php and add the below code

    <?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
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search