2016-04-29 5 views
1

Ich habe 2 Select-Box-Option, Stadt und Bezirk. Ich benutze Ajax, um den Distriktnamen zu erhalten, wenn ich in der Stadt wähle. Für diesen Prozess habe ich Erfolg, aber wenn ich auf "Senden" klicke, bekomme ich die Fehlermeldung Undefined index: district, wie dieses Bild zeigt result image.Wie kann ich Wert von Auswahl Option mit Ajax und PHP

Hier ist meine Ajax-Code:


 

 
<form action="" method="post"> 
 
    select city: 
 
    <select name="city" id="city"> 
 
    \t <option value="1">Phnom Penh</option> 
 
    \t <option value="2">Kampong Soam</option> 
 
     <option value="3">Siem Reap</option> 
 
    </select> 
 

 
    select district: 
 
    <select name="distrcit" name="district" id="district"> 
 
    \t <option>Select District</option> 
 
    </select> 
 

 
    <input type="submit" name="submit"> 
 
    </form>

Hier ist PHP-Code:

$(document).ready(function($) { 
    $("#city").change(function() { 
     var search_id = $(this).val(); 
     $.ajax({ 
      url: "search.php", 
      method: "post", 
      data: {search_id:search_id}, 
      success: function(data){ 
       $("#district").html(data); 
      } 
     }) 
    }); 
}); 

hier HTML-Code ist

<?php 
 
if(isset($_POST['submit'])){ 
 
\t echo $_POST['city']; 
 
\t echo $_POST['district']; 
 
} 
 

 
?> 
 

 
//ajax request 
 
<?php 
 
if(isset($_POST['search_id'])){ 
 
\t $search_id = $database->escape_string($_POST['search_id']); 
 

 
\t if(!empty($search_id)){ 
 
\t \t // $output = array(); 
 
\t \t $sql = "SELECT * FROM district WHERE ref_id = '$search_id'"; 
 
\t \t $districts = District::find_this_query($sql); 
 
\t \t foreach($districts as $district){ 
 
\t \t \t // echo $district->id; 
 
\t \t \t // echo $district->district_name; 
 
\t \t \t $output .= "<option value='{$district->id}'>{$district->district_name}</option>"; 
 
\t \t } 
 

 
\t \t echo $output; 
 
\t } 
 
\t 
 
} 
 
?>

+0

Nichts mit AJAX und PHP, aber Sie ein Problem in HTML haben mit ausgewählten Fall, Sie haben "Name" zweimal –

+0

Warum haben Sie doppelt 'name =" "" ' Parameter in dieser Zeile: '