开发者

how do I use value of input element in a new file (GET method)

开发者 https://www.devze.com 2023-03-11 15:41 出处:网络
<?php // code that connects to database ?> <table> <form method=\"get\" action=\"proce开发者_如何学Pythonssorder.php\">
<?php
// code that connects to database
?>
<table>

    <form method="get" action="proce开发者_如何学Pythonssorder.php">
            <?php
            while (list($pizzaId, $pizzaName, $pizzaNumber, $pizzaPrice) = mysql_fetch_row($resultaat))
                    {

                        echo           "<tr>
                                    <td>".$pizzaName."</td>
                                    <td>".$pizzaNumber."</td>
                                    <td>".$pizzaPrice."</td> 
                                    <td> <input type='text' name='$pizzaId' value='$qty' size='3' /></td>
                                    </tr>";
                    }
            mysql_close($db);        
            ?>
        <input type="submit" value="Order now" />

I would like to display the pizzas of where there is a value in the input element. the processorder.php file would look like: my url shows the pizzaId's with the values after the '='. So I figured I have an associative array on my hands. I thought I'd put a foreach loop in my processorder.php going like

   foreach ($_GET['pizzaId'] as $pizza => $qty)    
            {
                echo $pizza." ".$qty."<br />";
            }

Yet, when I use the foreach loop, the error in my browser says that the argument of my foreach loop is invalid because $_GET['pizzaId'] isn't an array to begin with (I checked with is_array). So how do I get access to those values in my value attribute of the input element?


Your $pizzaId is an integer. I would change the name of the input elements to name="pizzas[$pizzaId]", and then you could access it through PHP like this:

foreach ($_GET["pizzas"] as $pizzaId => $pizzaQty) {
    echo "$pizzaId $pizzaQty<br />";
}

with this method, instead of just plain name="pizzas[]", you also retain the association with the actual pizzaId.


If you need to put an array into $_GET then your url should look like:

//site/page?pizzaID[]=123&pizzaID[]=124

You can achive this with similar usage of name of input field

<input name="pizzaID[]" />
<input name="pizzaID[]" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号