如何在将表单提交到同一个php页面后显示表单中的输入值?

时间:2016-12-09 06:57:29

标签: php html mysql forms

我想在各自的输入字段中显示给定的输入值。我尝试过使用javascript。但它没有帮助。

echo "<form class='navbar-form' method='POST' action='#'>"; 

$sqluser="SELECT DISTINCT username FROM user ORDER BY username ASC";
$resultuser=mysqli_query($conn,$sqluser);

echo "<div class='form-group' style='margin-right:5px;'><input  id='from_date' name='from_date' type='date' data-date-inline-picker='true' /></div>";
echo "<div class='form-group' style='margin-right:5px;'><input  id='to_date' name='to_date' type='date' data-date-inline-picker='true' /></div>";
echo "<div class='form-group' style='margin-right:5px;'><select  id='username' name='username' style='width:170px;height:33px;'>";
echo "<option select='selected'>-- Username --</option>";
while($ro1=mysqli_fetch_array($resultuser))
{
    echo "<option value='".$ro1['username']."'>".$ro1['username']."</option>";
}
echo "</select>";
echo "</div>";
echo "<div class='form-group' style='margin-right:5px;'><input type='submit' value='submit'  onclick='showInput();'></div>";
echo "</form>";

3 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

<form class='navbar-form' method='POST'>
    <?php
        echo "<div class='form-group' style='margin-right:5px;'><input  id='from_date' name='from_date' type='date' data-date-inline-picker='true' /></div>";
        echo "<div class='form-group' style='margin-right:5px;'><input  id='to_date' name='to_date' type='date' data-date-inline-picker='true' /></div>";
    ?>
        <select name="username" type="text" id="username">
            <option value="">--Username--</option>
            <?php
            $sql = "SELECT DISTINCT username FROM user ORDER BY username ASC";
            $result = mysqli_query($conn,$sqluser);

            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    if(isset($_POST["username"]) && $row["id"] == $_POST["username"]){
                        $optionSelected = "selected='selected'";
                    }else{
                        $optionSelected = "";
                    }
                    $userSet = "<option value='" .$row["username"]. "' ".$optionSelected.">".$row["username"]."</option>";
                    echo $userSet;
                }
            }
            $conn->close();
            ?>
        </select>
</form>

<强>解释

这应该是用户的ID

$row["id"]

如果您没有该用户的ID,请将其替换为:

if(isset($_POST["username"]) && $row["id"] == $_POST["username"]){

if(isset($_POST["username"]) {

答案 1 :(得分:-1)

您好,请尝试以下代码...

echo "<form class='navbar-form' method='POST' action=''>"; 

$sqluser="SELECT DISTINCT username FROM user ORDER BY username ASC";
$resultuser=mysqli_query($conn,$sqluser);

if(isset($_POST['from_date']))
    echo "<div class='form-group' style='margin-right:5px;'><input  id='from_date' name='from_date' type='date' data-date-inline-picker='true' value='".$_POST['from_date']."'/></div>";
else
    echo "<div class='form-group' style='margin-right:5px;'><input  id='from_date' name='from_date' type='date' data-date-inline-picker='true' /></div>";

if(isset($_POST['from_date']))
    echo "<div class='form-group' style='margin-right:5px;'><input  id='to_date' name='to_date' type='date' data-date-inline-picker='true' value='".$_POST['to_date']."'/></div>";
else
    echo "<div class='form-group' style='margin-right:5px;'><input  id='to_date' name='to_date' type='date' data-date-inline-picker='true' /></div>";

echo "<div class='form-group' style='margin-right:5px;'><select  id='username' name='username' style='width:170px;height:33px;'>";
if(isset($_POST['username']) && $_POST['username'] == "-- Username --")
    echo "<option selected='selected'>-- Username --</option>";
else
    echo "<option >-- Username --</option>";
while($ro1=mysqli_fetch_array($resultuser))
{
    if(isset($_POST['username']) && $_POST['username'] == $ro1['username'])
        echo "<option selected='selected' value='".$ro1['username']."'>".$ro1['username']."</option>";
    else
        echo "<option value='".$ro1['username']."'>".$ro1['username']."</option>";
}
echo "</select>";
echo "</div>";
echo "<div class='form-group' style='margin-right:5px;'><input type='submit' value='submit'  onclick='showInput();'></div>";
echo "</form>";

答案 2 :(得分:-2)

在php中使用$ _POST方法 输入标记中的value属性如下:

value='<?php echo $_POST['form_date']; ?>'