繁体   English   中英

从数据库中获取选择选项,并将选择的选项传递给PHP变量

[英]Fetch select options from database and pass the selected option to PHP variable

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

<?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
// 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字段。 如果该字段不在查询中,则options标记中的值为空。

一个解决方案可能是将选项标记的值中的$row->Id更改为$row->Region

那应该使您朝正确的方向前进。

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

尝试改变这个,

echo "<option value=$row->Id>$row->Region ? 'selected="selected"' : ''

$value = 'whatever your value';

echo "<option value='". $value ."' />

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

您的代码有误。 当您希望选择一个选项时,只需编写一个“ selected”属性,如下所示: <option value="myValue" selected></option>在这种情况下,您正在将“ selected”分配给该选项的值;

您的代码应如下所示:

  <?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>";
   } 
   ?>

您的代码中有2个错误,没有在option中传递值,选择的逻辑是== not >

<?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM