【问题标题】:Fetch select options from database and pass the selected option to PHP variable从数据库中获取选择选项并将选择的选项传递给 PHP 变量
【发布时间】:2018-10-12 07:50:17
【问题描述】:

从数据库查询中填充 HTML 选项的代码摘录:

<?php
// Connect to the database
$mydb = new wpdb('***','***','***','***');

// run the query to fetch the options
$rows = $mydb->get_results("select distinct(Region) from tblusers order by 
Region asc;");
?>

<!-- Form -->
<form method='post' action='success.php'>
<b><h1>Stuur SMS aan Streek</h1></b>

<b>Kies Streek:</b>
<select id='txtReg' name='txtReg'></option>
<?php 
// populate the options
foreach ($rows as $row) { 
echo "<option value=$row->Id>$row->Region ? 'selected="selected"' : '' 
</option>";
} 
?>

</select>
<br/>

<b>Boodskap:</b>
<textarea id='txtMsg' name='txtMsg' rows=5 cols=65 style='width:50%;'> 
</textarea>
<br/>

<input type='reset' value='Kanselleer'> | <input class="button" 
type='submit' value='Stuur SMS aan Streek'>
</form>

必须显示所选选项的success.php页面:

<?php
$txtReg = $_POST['txtReg'];

echo $txtReg;

一切正常,除了我从下拉列表中选择一个选项时,我如何将它传递到成功页面,因为当我尝试回显变量时它没有显示。

【问题讨论】:

  • 请注意:选择中的选项是从数据库查询中填充的。然后从下拉列表中选择一个选项,然后需要将该选项值传递到成功页面。
  • 是的,看看我的答案并进行更改,它应该适合你。

标签: php mysql wordpress variables


【解决方案1】:

您的选择标签以及生成选项标签的方式存在一些语法问题。

更新

<?php
// Connect to the database
$mydb = new wpdb('***','***','***','***');

$query = "SELECT distinct(Region) 
FROM tblusers
Order By 
Region ASC";    

$rows = $mydb->get_results($query);

var_dump($rows);

?>

<!-- Form -->
<form method='post' action='success.php'>

  <b><h1>Stuur SMS aan Streek</h1></b>

  <b>Kies Streek:</b>
  <select id='txtReg' name='txtReg'>
    <?php 
    // populate the options
    foreach ($rows as $row) { 
    echo '<option value="' . $row->Region . '">' . $row->Region . '</option>';
    } 
    ?>
  </select><br/>

  <b>Boodskap:</b>
  <textarea id='txtMsg' name='txtMsg' rows=5 cols=65 style='width:50%;'></textarea><br/>

  <input type='reset' value='Kanselleer'> | <input class="button" type='submit' value='Stuur SMS aan Streek'>

</form> 

您可能遇到的另一个问题是您的查询。看起来您的数据中可能没有返回 ID 字段。如果该字段不在您的查询中,那么您的选项标签中的值将为空。

解决方案可能是将选项标记值中的$row-&gt;Id 更改为$row-&gt;Region

这应该会让你朝着正确的方向前进。

【讨论】:

  • 它仍然不起作用 - 它应该像从 post 变量中获取值一样简单,但它不起作用。难道不是因为选择被回显,而其余的是实际的 HTML 代码?这难道没有效果吗?我试过: $txtReg = $_REQUEST['txtReg'];回声 $txtReg;我试过了: $txtReg = $_POST['txtReg'];回声 $txtReg;
  • 请在循环之前运行var_dump($rows) 并发布您的答案。谢谢
  • 数据库中有很多重复区域,因为很多用户来自同一个地方,因此在这种情况下Id是唯一的。 array(5462) { [0]=> object(stdClass)#3138 (2) { ["Region"]=> string(0) "" ["Id"]=> string(1) "1" } [1 ]=> object(stdClass)#3......
  • 请将当前代码复制并粘贴到您的脚本中并运行。
  • 如果我在没有 ID 的情况下运行上述代码:array(290) { [0]=> object(stdClass)#3138 (1) { ["Region"]=> string(0) "" } [1]=> object(stdClass)#3139 (1) { ["Region"]=> string(13) "Randfontein" } [2]=> object(stdClass)#3140 (1) { ["Region" ]=> string(10) " Alberton " } [3]=> object(stdClass)#3141 (1) { ["Region"]=> string(10) " Bellville".....
【解决方案2】:

您可以为商店选择的选项添加隐藏字段。使用 jquery 或 javascript 更改选项时更新该隐藏字段值。

【讨论】:

    【解决方案3】:

    尝试更改此设置,

    echo "&lt;option value=$row-&gt;Id&gt;$row-&gt;Region ? 'selected="selected"' : ''

    $value = 'whatever your value';
    
    echo "<option value='". $value ."' />
    

    我可以看到您的值中缺少引号。

    【讨论】:

      【解决方案4】:

      您的代码有误。当您想要选择一个选项时,您只需编写一个“已选择”属性,如下所示:&lt;option value="myValue" selected&gt;&lt;/option&gt; 在这种情况下,您正在做的是,您将“选定”分配给选项的值;

      您的代码应如下所示:

        <?php 
       // populate the options
         foreach ($rows as $row) { 
         echo "<option value='".$row->Id."' ";
      
         if($row->id == $row->Region) echo "selected";
         echo "> SomeOption"; // Close the option tag and add any text inside
      
      
         echo "</option>";
         } 
         ?>
      

      【讨论】:

        【解决方案5】:

        您的代码中有 2 个错误,您没有在选项中传递值,选择的逻辑是 == 而不是 &gt;

        <?php
        // Connect to the database
        $mydb = new wpdb('***','***','***','***');
        // run the query to fetch the options
        $rows = $mydb->get_results("select distinct(Region) from tblusers order by Region `asc;"); `
        ?>
        <!-- Form -->
        <form method='post' action='success.php'>
        <b><h1>Stuur SMS aan Streek</h1></b>
        <b>Kies Streek:</b>
        <select id='txtReg' name='txtReg'></option>
        <?php 
        // populate the options
        foreach ($rows as $row) { 
        echo "<option value='".$row->Id."' ".($row->Id==$row->Region) ? 'selected' : ''."></option>"; 
        }
        ?>
        </select>
        <br/>
        <b>Boodskap:</b>
        <textarea id='txtMsg' name='txtMsg' rows=5 cols=65 style='width:50%;'> 
        </textarea>
        <br/>
        <input type='reset' value='Kanselleer'> | <input class="button" type='submit' value='Stuur SMS aan Streek'> 
        </form>
        

        【讨论】:

          猜你喜欢
          • 2015-11-12
          • 1970-01-01
          • 1970-01-01
          • 2019-07-04
          • 2012-07-01
          • 1970-01-01
          • 2011-09-15
          • 2014-07-07
          • 2020-11-10
          相关资源
          最近更新 更多