当我提交表单时,我没有得到select选项的值,我尝试使用POST和session但它总是不显示
<form role="form" method="POST" action="test.php">
<?php if($id == 1 OR $id==2){
echo" <p> No data</p> ";}else{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata )
or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data )) {
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
$_SESSION['data_id']= $data_id;
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
$dataID = isset($_POST['data_id']) ? $_POST['data_id'] : '';
echo "data is $dataID";
答案 0 :(得分:1)
您的输入类型名称选择为data
,并且您使用data_id
访问该名称,因此您必须使用$_POST['data']
代替$_POST['data_id']
答案 1 :(得分:0)
使用名称“data”获取选择框的值,因为您在html的name="data"
框中设置了<select>
:
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is".$dataID;
答案 2 :(得分:0)
<强> main.php 强>
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
}
?>
</select>
<?php
}
?>
<button type="submit" class="btn btn-primary">show</button>
<强> test.php的强>
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is $dataID";
尝试这个..
答案 3 :(得分:0)
检查以下几点可能会产生问题。
首先检查您的<select class="form-control" name="data">
名称是否为data
,以便您可以使用$_POST['data']
而非data_id
访问该名称。
检查<option value="'.$row['data_id'].'">
可能是data_id没有给出正确的值尝试检查静态值。
答案 4 :(得分:0)
我为你修改了这段代码,请使用这段代码。它对我有用,我希望这段代码也适合你。
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
?>
<option value="<?php echo $row['data_id']; ?>"><?php echo $dataName ?></option>
$_SESSION['data_id']= $row['data_id'];
<?php
}
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
&#13;
<?php
if(isset($_POST['data']))
{
echo $_POST['data'];
}
?>
&#13;