Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

html - 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...