使用db的for循环中的预选选项

时间:2019-04-17 09:40:22

标签: php ajax mysqli

我有一个select标记,其中选项的数据来自数据库,现在我想预选用户曾经选择过的选项字段。

$id     = $_GET['edit'];
$obj  = new employees();
$result = $obj->select_for_update($id);
    if (!empty($result)) {
        foreach ($result as $values){

  <?php 
    $result=$obj->select_country();
        foreach ($result as $values1){
            ?>
    <option value="<?php echo $values['country']; ?>" <?php if($values['country'] == $values1['country_id'] ) { echo 'selected="selected"'; } ?>>
<?php
        echo $values1['country_name'];
?>
   </option>
    <?php
        }  

?>

    </select>        

   <select id="state" name="state" >

  <?php 
    $result=$obj->select_state();
        foreach ($result as $values1){
            ?>

       <option value="<?php echo $values['state']; ?>" <?php if($values['state'] == $values1['state_id'] ) { echo 'selected="selected"'; } ?>>
<?php
        echo $values1['state_name'];
?>


       </option>
<?php
        }  

?>
</select>


<select id="city" name="city" value="<?php echo $values['city']; ?>" >

<?php 
    $result=$obj->select_city();
        foreach ($result as $values1){
            ?>

  <option value="<?php echo $values['city']; ?>" <?php if($values['city'] == $values1['city_id'] ) { echo 'selected="selected"'; } ?>>
<?php
        echo $values1['city_name'];
?>


       </option>
<?php
        }  

?>
</select>

用于选择国家/地区城市的功能

  function select_country(){
          $sth =  $this->con->prepare("SELECT  * from country");
          $sth->execute();
          $result = $sth->fetchAll(PDO::FETCH_ASSOC);
           return $result;
      }

      function select_state(){
          $sth =  $this->con->prepare("SELECT  * from state");
          $sth->execute();
          $result = $sth->fetchAll(PDO::FETCH_ASSOC);
          return $result;
      }

      function select_city(){
          $sth =  $this->con->prepare("SELECT  * from city");
          $sth->execute();
          $result = $sth->fetchAll(PDO::FETCH_ASSOC);
          return $result;
      }

选择功能

function select_for_update($id){
    $sth =  $this->con->prepare("SELECT * FROM employees
    LEFT JOIN country ON employees.country = country.country_id
    LEFT JOIN state ON employees.state = state.state_id
    LEFT JOIN city ON employees.city = city.city_id WHERE emp_id='$id'");
    $sth->execute();
    $result = $sth->fetchAll(PDO::FETCH_ASSOC);

    return $result;
}

我正在从数据库获取数据,并希望打印选定的数据。

就像我一旦选择了印度后,印度就存储在数据库中,现在,当用户进行编辑时,选择标签将在选项标签中显示预先选择的印度

脚本来调用ajax

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <script type="text/javascript">
    $(document).ready(function(){
        $("#country").change(function(){
            var countryID = $(this).val();
            if(countryID){
                $.ajax({
                    type:'POST',
                    url:'ajaxData.php',
                    data:'country_id='+countryID,
                    success:function(html){
                        $('#state').html(html);
                        $('#city').html('<option value="">Select state first</option>'); 
                    }
                }); 
            }else{
                $('#state').html('<option value="">Select country first</option>');
                $('#city').html('<option value="">Select state first</option>'); 
            }
          });

        $('#state').on('change',function(){
            var stateID = $(this).val();
            if(stateID){
                $.ajax({
                    type:'POST',
                    url:'ajaxData.php',
                    data:'state_id='+stateID,
                    success:function(html){
                        $('#city').html(html);
                    }
                }); 
            }else{
                $('#city').html('<option value="">Select state first</option>'); 
            }
        });
    });
    </script>    

ajaxData.php

<?php
$dbHost     = 'ops';
$dbUsername = 'dhruv_thakkar';
$dbPassword = 'pass';
$dbName     = 'dhruv_thakkar';
$con= new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}


$country_id=$_POST['country_id'];
$state_id=$_POST['state_id'];

if(isset($_POST['country_id'])){
$query=mysqli_query($con,"select * from state where country_id='$country_id'");    

$rowCount = $query->num_rows;
if($rowCount > 0){
    echo '<option value="">Select state</option>';
            while($row = $query->fetch_assoc()){
                     echo '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
             }
}
else{
    echo '<option value="">State not available</option>';
}
}

if($_POST['state_id']){

    $query=mysqli_query($con,"select * from city where state_id='$state_id'");
    $rowCount = $query->num_rows;
    if($rowCount > 0){
        echo '<option value="">Select city</option>';
        while($row = $query->fetch_assoc()){
            echo '<option value="'.$row['city_id'].'">'.$row['city_name'].'</option>';
        }
    }else{
        echo '<option value="">City not available</option>';
    }

}
?>

2 个答案:

答案 0 :(得分:1)

在编辑时,您需要将saved country更改为compare

  

如果ADD/EDIT具有相同的页面,则需要传递$country_id = 0;在ADD时间,因此不会出现不确定的变量错误

$country_id = '1';//assume this is from your DB
<select value="" id="country" name="country">
    <option>Select Country</option>
    <?php $result=$obj->select_country();
    if (!empty($result)) {
        foreach ($result as $values){ ?>
            <option value="<?php echo $values['country_id']; ?>" <?php if($values['country_id'] == $country_id) { echo 'selected="selected"'; } ?> >
            <?php echo $values['country_name'];?>
            </option>
        <?php } 
    }else{
        echo '<option value="">Country not available</option>';
    }
    ?>
</select>

答案 1 :(得分:0)

在这一行上,

$country_id = "1"; // considering 1 is country_id of India
<option value="<?php echo $values['country_id']; ?>"   
<?php (!empty($country_id) && $country_id == $values['country_id'] ? 'selected' : ''; )?> >

像我使用过的上述三元运算符一样检查。