避免PHP中出现未定义的索引/偏移错误

时间:2018-12-10 21:32:33

标签: php html select input

我有两个选择输入,从第一个输入获得的值用于填充第二个输入,但是由于页面加载上没有要使用的值,因此我需要提交两次第一个输入然后填充当我收到错误的未定义索引时。

是否可以在页面加载时获取输入并使用该输入,直到用户选择自己的输入为止?如果没有,如何隐藏错误,直到收到inout?

<html>
<form action="#" method="post">
   <select name="make">
    <?php 
    $query = "SELECT * FROM make";  
    $select_make_id = mysqli_query($connection, $query);
    while ($row = mysqli_fetch_assoc($select_make_id)) {
    $make_id = $row['make_id'];
    $make_name = $row['make_name'];
    echo "<option ";
    if($make_id == $_POST['make'])
    echo 'selected ';
    echo "value='$make_id'>{$make_id}</option>";
    }
    ?>
   </select>
   <input type="submit" name="makes" value="Get Selected" />
</form>

<?php
if(isset($_POST['makes'])){
$selected_val_make = $_POST['make'];
    echo "$selected_val_make"; 
} 
?> 

<form action="#" method="post">
   <select name="model" >
    <?php 
    $query = "SELECT * FROM models where make_model = $selected_val_make";   
    $select_model_id = mysqli_query($connection, $query);
    while ($row = mysqli_fetch_assoc($select_model_id)) {
    $model_id = $row['model_id'];
    $model_name = $row['model_name'];
    echo "<option ";
    if($model_id == $_POST['make'])
    echo 'selected ';
    echo "value='$model_id'>{$model_id}</option>";
    }
    ?>  
   </select>
       <input type="submit" name="models" value="Get Selected" />
</form>

<?php
if(isset($_POST['models'])){
$selected_val_model = $_POST['model'];
    echo "$selected_val_model";
} 
?>     
</html>

0 个答案:

没有答案