How to keep form items selected after post request.using onchange

I have a problem when submit form value from the second select don’t show. Can someone explain me what I’m doing wrong because in new in this (maybe is because i use onchange event). Thanks in advance.

index.php

 <?php 
 include("includes/conn.php");

     $sql = "SELECT * FROM opstina";
     $result = $conn->query($sql);
     if($result->num_rows > 0){
         $opstineArr = array();
         while($row = $result->fetch_assoc()){
             $opstineArr[] = $row;
         }
     }

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

         $opstine = $_POST["opstine"];
         $ime = $_POST["ime"];

         if(isset($opstine)){
             if(empty($opstine)){
                 $error["opstine"] = "izaberite opstinu";
             }
         }


         $naselja = $_POST["naselja"];
         if(isset($naselja)){
             if(empty($naselja)){
                 $error["naselja"] = "izaberite naselje";
             }
         }


         if(isset($ime)){
             if(empty($ime)){
                 $error["ime"] = "izaberite ime";
             }
         }

         if(count($error) == 0){
             echo 2222;
         }

     }


     include("includes/header.php");

 ?>

 <form action="" method="POST">
     <select name="opstine" class="form-control" id="select_id" onchange="getNaselja(this.value)">
         <option>..Izaberite Opstinu..</option>
         <?php foreach($opstineArr as $key => $opstina): ?>
         <option value="<?php echo $opstina['OpstinaID']; ?>" <?php if(isset($opstine) && $opstine==$opstina['OpstinaID']) echo "selected"; ?> ><?php echo $opstina['NazivOpstine']; ?></option>
         <?php endforeach; ?>
     </select>
     <?php if(isset($error["opstine"])) echo $error["opstine"]; ?>

     <p>
         <select name='naselja' id='naseljeLista' class='form-control' style='margin-top:10px;'>
         </select>
         <span><?php if(isset($error["naselja"])) echo $error["naselja"]; ?></span>
     </p><br>

     <input type="text" name="ime" value="<?php if(isset($ime)) echo $ime; ?>" class="form-control col-md-7 col-xs-12"><?php if(isset($error["ime"])) echo $error["ime"]; ?><br><br>
     <input type="submit" name="addNesto" class="btn btn-sm btn-success">
 </form>
 <?php include("includes/footer.php"); ?>

getNaselja.php

 global $conn;
 include("includes/conn.php");

     if(!empty($_POST["opstina_id"])){

         $opstinaID = $_POST["opstina_id"];

         $sql1 = "SELECT * FROM naselje WHERE OpstinaID=".$opstinaID;
         $result1 = $conn->query($sql1);
         if($result1->num_rows > 0){
             $naseljaArr = array();
             while ($one = $result1->fetch_assoc()) {
                 $naseljaArr[] = $one;
             }
         }

         echo "<option value=''>---Izaberite naselje---</option>";
         foreach ($naseljaArr as $key => $value) {
             if(isset($_POST["naselja"]) && $value["NaseljeID"]==$_POST["naselja"]){
                 echo "<option value='".$value['NaseljeID']."' selected='selected' style='display:block;'>".$value['NazivNaselja']."</option>";
             }else{
                 echo "<option value='".$value['NaseljeID']."'>".$value['NazivNaselja']."</option>";
             }
         }

     }

footer.php

 <script>

         function getNaselja(val){

             $.ajax({
                 type: 'POST',
                 url: 'getNaselja.php',
                 data: 'opstina_id='+val,
                 success: function(data){
                     $('#naseljeLista').html(data);
                     console.log(data);
                 }
             });

         }


     </script>

example of form after submit:

When i click on first select i i have valid values for the second select but after submiting the form I loose value from the second select ( if added data via ajax they doesn’t persist after a sumbit)…How can i fix this. Many thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.