从数组中选择选择项,并将数值传送到另一个页面


choosing a selection from an array, and carrying a numerical value over to another page

我是一个PHP新手,所以我在这个任务上遇到了麻烦:

简而言之,我想要运行一个查询并将结果返回到一个数组中,然后允许用户从数组中选择一个选项,并将数值传递到另一个php页面以用于其他计算。

下面是目前为止的基本代码:
<?php
 include 'mysql_connect.php';
      $Choices = mysqli_query ($server_connect, "SELECT * FROM Database 
WHERE FLOOR (Item_number) = '$StockCategory' AND name='$name'") 
or die(mysqli_error ($server_connect));?>
            <table cellspacing="1" cellpadding="2" width="20%" border="1">
            <?php while ($selectionlist = mysqli_fetch_array($Choices)){
$orderchoicenumber++; 
$selectnumber = $matchups['Item_number']; ?>
<tr>
<td> <?php echo $selectionlist['size']; ?> </td>
<td>   <?php echo $selectionlist['color'];?> </td>
<td>   <?php echo $selectnumber;?></td>
<td>   <?php echo $orderchoicenumber;?>
                <?php }?>
</td></tr>
   Enter your selection: 
<form name ="selectionprocess" action="precheckout.php" method="POST">
<input type="text" maxlength="2" name="enterselection" >
<input type="hidden" name="selectnumber" value="<?php echo $selectnumber;?>">
</form>
<input type="submit" name="formSubmit" value="Submit" >

目标页面的代码[precheckout.php]:

<?php
 $name = isset($_POST["name"]) ? $_POST["name"] : $_SESSION['name'];
    $mode = $_POST["select"];
       $SelectNumber = $_POST["selectnumber"]; 
       $Item_number = $SelectNumber;
?>
<p>Hello <?php echo $name; ?>!</p> <br>
OK, the item number of what you want is--- <?php echo $Item_number;?> 

当然,这意味着myfetcharray的结果数量将会变化。

第一个方面工作得很好。在这个例子中,下面是我收到的输出(表格格式):

输入您的选择:_____

4x6海军蓝90515.01 1

4 × 8紫红色90515.07 2

6x8赤褐色90515.03 3

2x4黑色90515.02 4

5x7 aqua 90515.08 5

选择号在最右边一列。(十进制数字是我的SQL数据库中的标识项目编号。我不想在最后的程序中显示这个数字,即使我想我可以让用户输入这个数字而不是选择数字来继续,但这不是一个非常友好的方法。

当我尝试输入一个选择编号(在本例中为1-5)时,问题出现了。程序成功地将数值传递到预结账页面[value="<?php echo $selectnumber;?>">],但它始终是myfetcharray输出的最终值(即在最低行中)。在本例中,下一页的输出将是:

你好,约翰!

好,让我们看看你是怎么做的,因为这个项目编号是——90515.08

…无论选择哪个对局号码

理想情况下,我希望有一种更用户友好的方式来选择行,而不是键入选择编号。我尝试过单选按钮方法,但我能做的最好的事情是成功地将值"on"传递到下一页,而不是数值。我愿意使用其他语言,尽管我不知道它们是否能很好地与PHP(即Javascript)配合使用。

任何关于这个的想法都会很感激。谢谢!

首先:一个更友好的方法可能是下拉菜单:试试这个:

<select>
<?php while ($selectionlist = mysqli_fetch_array($Choices)){
$orderchoicenumber++; 
$selectnumber = $matchups['Item_number']; ?>
<option value="<?php echo $selectnumber; ?>"> 
<?php echo $selectionlist['size'] . " " . $selectionlist['color'] . " " . $selectionlist['color'] . " " . $selectnumber . " " . $orderchoicenumber; ?>
</option>
<?php }?>
</select>

当页面加载时,所有PHP代码都在服务器上执行,任何用户与页面的交互都是"客户端"(在用户的机器上),所以你是对的:它需要另一种语言来更新你的selectnumber隐藏输入。jQuery(一个javascript库)是你最好的选择。

你必须已经有一些javascript在你的页面上张贴你的表单precheckout.php -你能把它添加到你的问题,然后我将能够指导你对你需要改变的位?