Skip to content
Advertisement

How do I pass these variables to another php file

I’m currently making a shopping website and I need to be able to pass two variables to the next page, the code may be badly written because I’m new to this, but I’m trying to pass the number from the drop-down menu and the “row[‘pid’]” to another page. As shown below I have attempted to use a form button but it can only transfer the number from the dropdown. There is database connected so if you try to load it up, it may not load anything. This issue is specifically focussing on the button which the form is linked to at the end. Thank you for your time.

<!DOCTYPE html>
<html lang="en">

<head>
    <script src="https://kit.fontawesome.com/9114d9acc8.js" crossorigin="anonymous">

    </script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="includes/navbar/navbar.css">
    <link rel="stylesheet" href="css/style.css">
    <title>Document</title>
</head>

<body>
    <?php include "includes/navbar/navbar.php"; ?>
    <div id="arranging">
        <?php
    require 'includes/dbh.php';
    $query = sprintf('SELECT * FROM produce');
    $result = mysqli_query($conn, $query);
    //$var_value = 'hello';
    //$_SESSION['varname'] = $var_value;
    while ($row = mysqli_fetch_assoc($result)) { 
        if ($row['Quantity'] == 0) {

        }
        else {
    ?>
        <div class="itemTemplate">
            <a id="title" onclick="ContentPage(<?php echo $row['pid']; ?>)"><?php echo $row['Name'] ?></a>
            <a onclick="ContentPage(<?php echo $row['pid']; ?>)" id="myid"><img src="<?php echo $row['img'] ?>"
                    alt="<?php echo $row['Name'] ?>"></a>
            <span>Price: £<?php echo $row['Price'] ?></span>
            <form action="includes/quickBasket.php" method="POST">
                <div class="flex-b">
                    <label for="">Quantity:</label>
                    <select name="quantity">
                        <?php
                
                if ($row['Quantity'] > 8) {
                    for ($x = 1; $x <= 8; $x++) {
                        echo "<option value='$x'>$x</option>";
                      }
                }
                else {
                    for ($x = 1; $x <= $row['Quantity']; $x++) {
                        echo "<option value='$x'>$x</option>";
                      }
                }
                ?>

                    </select>
                    <button id="button" type="submit" name="cart" class="fas fa-shopping-basket"></button>
                </div>
            </form>
        </div>
        <?php
    } 
}
    ?>

    </div>

    <script>
        function ContentPage(elem) {
            location.href = "Product.php" + "?id=" + elem;
        };
    </script>

</body>

</html>

Advertisement

Answer

If you want to transfer the PID when the form is submitted then you need a field for it within the form, e.g. a hidden field like this:

<input type="hidden" name="pid" value="<?php echo $row['pid']; ?>"/>

Then when you submit the form, it will be accessible as $_POST["pid"] (just like the value from the dropdown is accessible as $_POST["quantity"]).

This applies to any value – if you want it to be submitted with the form, then there needs to be a proper field for it within the form (or least associated with the form via the necessary attribute).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement