在php中提交表单时未选择下拉值

时间:2016-06-03 16:58:40

标签: php html forms post

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

我有一个onlick用于将表单定向到其他页面以更新数据库。 我对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>

1 个答案:

答案 0 :(得分:0)

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

由于您不熟悉PHP,例如,如果您正在使用post,并且变量正在等待另一页从表单中收集此信息,那么您可以这样做:

这是表单字段示例:

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

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

$someVariable = $_POST[ 'XYZ' ];

现在,如果你想使用GET,那么你可以使用这个

$someVariable = $_GET[ 'XYZ' ];
希望这可以解决这个困惑。

- 编辑2 - 看完你的评论后好了,因为我还没有看到你如何通过你的数据库迭代那些选项&#34;选项&#34;列表/菜单,我会说你不能放&#34;连接&#34;作为这部分的价值:

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

因为假设&#34; connection.php&#34;是连接到数据库,然后这不会帮助你,这是其他地方。相反,一旦你建立了这种连接(其他地方,最好是在某个地方的标题之上),你就必须拥有循环数据库的代码,然后"WHILE"将其循环,将结果吐出到该选项中领域。让我举一个 PSEUDO 代码的例子。

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

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

等,然后你需要php来循环你的数据库结果,并将结果放入&#34;选项&#34;在那个选择中,

这里有一些伪代码

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

OR .....

如果"$row"具有您想要在该列表中添加的数据库中使用的特定值,那么您可以执行与上面类似的操作,但是可以使用以下内容:

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

基本上,你想要你的&#34; while循环&#34;迭代你的数据库,WHILE做它,把它的数据输入你的选项html。

另外,如果你想从你的数据库中获取一个特定值,放入一个GET变量,用于处理页面(正如我上面提到的那样),那么同样,你可以这样做:< / p>

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

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

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

相关问题