【问题标题】:Dropdown value is not selected on submitting form in php在php中提交表单时未选择下拉值
【发布时间】:2017-09-24 13:03:11
【问题描述】:

场景是 - 我有一个包含多个输入字段和文本区域的表单,每当我发布此表单时,所有这些值都会被发布。但是,我也有一个从我的数据库创建的下拉列表。当我得到值时,这个下拉值会正确显示。我有第二个提交按钮,它应该采用这个选定的值并发布到其他页面。 我试过$_POST='name of select option',但没有帮助。

我有一个用于将表单定向到其他页面以更新数据库的链接。 我对 php 还很陌生,所以可能是 _POST 的使用不正确。

<form name="f1" class="formoid-solid-blue" method="GET">
         <div class="title">
            <h2></h2>
            <h2>Tracking & Receiving</h2>
         </div>
         <div class="element-input">
            <label class="title"></label>
            <div class="item-cont">
               <input class="small" type="text" name="store" placeholder="Store #"/>
               <span class="icon-place"></span>
            </div>
         </div>
         <div class="element-input">
            <label class="title"></label>
            <div class="item-cont">
               <input class="medium" type="text" name="userid" placeholder="UserId"/>
               <span class="icon-place"></span>
            </div>
         </div>
         <div class="element-input">
            <label class="title"></label>
            <div class="item-cont">
               <input class="large" type="text" name="order" placeholder="Order Number"/>
               <span class="icon-place"></span>
            </div>
         </div>
         <div class="submit">
            <input type="submit" name="Send" value="Send"/>
         </div>
         <div class="element-separator">
            <hr>

            <h3 class="section-break-title">Tracking Numbers</h3>
         </div>
         <div class="element-multiple">
            <label class="title"></label>
            <div class="item-cont">
               <div class="large">
                  <select data-no-selected="Nothing selected" name="updTR" multiple="multiple">

                      <option name="op" value="<?php require 'connection.php'; ?>"> //getting value form db
                         <?php
                               echo $trackID; //DB value
                               $trackID = $_GET['updTR'];  //getting the variable from the form

                        ?></option> 
                  </select>
                  <span class="icon-place"></span>
               </div>
            </div>
         </div>
         <div class="submit">
            <input type="submit" onclick="f1.action='UpdateTR.php'; return true;" name="UpdateTR" value`enter code here`="Submit"/>
         </div>
      </form>

【问题讨论】:

    标签: php html forms post


    【解决方案1】:

    好吧,看看表单,您说您正在使用 POST,并使用其他与 POST 相关的方法进行了测试,但您的表单正在使用 GET,如上面的代码所示。

    由于您是 PHP 新手,例如,如果您正在使用 post,并且一个变量正在另一个页面上等待从您的表单中收集此信息,那么您可以这样做:

    这是表单域示例:

    <input type="text" name="XYZ">
    

    然后在将收集此信息的页面上,它将是

    $someVariable = $_POST[ 'XYZ' ];
    

    现在,如果你想使用 GET,那么它也是一样的,但你使用这个

    $someVariable = $_GET[ 'XYZ' ];
    

    希望这能消除混乱。

    -- 编辑 2-- 好的,在阅读了您的评论后,由于我还没有看到您如何在数据库中迭代该“选项”列表/菜单中的选项,所以我要说您不能将“连接”作为值这部分:

    <option name="op" value="<?php require 'connection.php'; ?>"> 
    

    因为假设“connection.php”正在连接到数据库,那么这对您没有帮助,那就去别处了。相反,一旦你建立了那个连接(在其他地方,最好是在它上面的某个地方的标题中),你就必须有循环通过数据库的代码,"WHILE" 它的循环,将结果吐出到那个选项字段中。让我举一个 PSEUDO 代码的例子。

    如果正常的选择/选项代码如下所示

    <select>
       <option> option 1 </option>
       <option> option 2 </option>
    </select>
    

    等等,那么你需要 php 循环遍历你的数据库结果,并将结果放入该选择中的“选项”中,

    这是一些伪代码:

    <select>
       <?php 
          while($row = your fetch code here){
             echo "<option>your line items here</option>";
          }
       ?>
    </select>
    

    或者.....

    如果"$row" 有一个您想从数据库中使用的特定值,并且您希望将其添加到该列表中,那么您可以执行与上述类似的操作,但可以使用以下内容:

    <select>
       <?php 
          while($row = your fetch code here){
             echo "<option>your line items here "' . $row['some value from DB here'] . '"</option>";
          }
       ?>
    </select>
    

    等等

    本质上,您希望您的“while 循环”遍历您的数据库并在执行此操作的同时将其数据输入到您的选项 html 中。

    另外,如果您想从数据库中获取特定值,请将其放入 GET 变量中,以用于处理页面(正如我在上面一直提到的),然后同样,您可以执行以下操作:

       <?php 
          while($row = your fetch code here){
             echo "<a href='linkHere.php?yourVariableToTrackWithGetHere='".$row['yourDBresutlToTrack']."'> Your text here </a>";       }
       ?>
    

    这样,当您单击链接时,数据库值会添加到该变量中,您可以稍后在处理页面中调用该变量。

    有点啰嗦,但是,这应该让你 100% 笔直。 希望这会有所帮助。

    【讨论】:

    • 感谢您的解释。正如建议的那样,我使用了 _GET 方法(上面更新了)。当我点击提交时,这是发送的 URL UpdateTR.php?store=506&userid=TEST&order=999999999244&updTR=&UpdateTR=Submit 。如您所见,所有其他值都被拾取,除了下拉列表中的值 - updTR。此外,在它被发送到的页面上,我有以下 - $updTR = $_GET['updTR'] 来收集信息。我的问题是,我提交表单时没有选择该值。
    • 好的,我想我明白你的问题了,让我找一些代码,因为我最近做了一个这样的项目,但是在这里发布太长了,我很快就会编辑我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    相关资源
    最近更新 更多