2014-02-12 2 views
0

Вот мой код. Я сделал скрипт для получения значений в «list_cust_city» из выбранного элемента в «list_cust_name» через запрос в php. Я не получил никаких значений в «list_cust_city» для города. Я сделал city.php.выбранный элемент в запросе из раскрывающегося списка в php

<script> 
    $('#list_cust_name').change(function(){ 
     alert("heyyy"); 
     $.ajax({ 
      url:'city.php', 
      data:{cust_name:$(this).val()}, 
      success: function(data){ 
       $('#list_cust_city').html(data); 
      } 
     }); 
    }); 
</script> 

<label style="color:#000">Name </label> 

<?php 
    $query_name = "SELECT DISTINCT cust_name FROM customer_db ORDER BY cust_name"; //Write a query 
    $data_name = mysql_query($query_name); //Execute the query 
?> 
<select id="list_cust_name" name="list_cust_name"> 
    <?php 
     while($fetch_options_name = mysql_fetch_assoc($data_name)) { //Loop all the options retrieved from the query 
     $customer=$fetch_options_name['cust_name']; 
    ?> 
    <option value="<?php echo $fetch_options_name['cust_name']; ?>"><?php echo $fetch_options_name['cust_name']; ?></option> 
    <?php 
     } 
    ?> 
</select> 

city.php

<body> 
    <?php 
     include('dbconnect.php'); 
     db_connect(); 
     $cust_name1=$_GET['cust_name']; //passed value of cust_name 
     $query_city = "SELECT DISTINCT cust_city FROM customer_db WHERE cust_name='$cust_name1'ORDER BY cust_city"; //Write a query 
     $data_city = mysql_query($query_city); //Execute the query 
     while($fetch_options_city = mysql_fetch_assoc($data_city)) { //Loop all the options retrieved from the query 
    ?> 
    <option value="<?php echo $fetch_options_city['cust_city']; ?>"><?php echo $fetch_options_city['cust_city']; ?></option> 
    <?php 
     } 
    ?> 
</body> 
+0

вам нужно добавить второй выпадающий на главной странице –

+0

в city.php '' тег не требуется –

ответ

0

PHP использует . для Concat строк. Изменить запрос:

$query_city = 'SELECT DISTINCT cust_city FROM customer_db WHERE cust_name="'.$cust_name1.'"ORDER BY cust_city'; 

добавить Также это ваш первый файл PHP:

<select id="list_cust_city" name="list_cust_city"></select> 

Вот полный код.

PHP 1:

<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script> 
$(function() { 
$('#list_cust_name').change(function(){ 
    $.ajax({ 
      url:'city.php', 
      data:{cust_name:$(this).val()}, 
      success: function(data){ 
        $('#list_cust_city').html(data); 
      } 
    }); 
}); 
}); 
</script> 

<label style="color:#000">Name </label> 
<?php $data_name = mysql_query("SELECT DISTINCT cust_name FROM customer_db ORDER BY cust_name");?> 

<select id="list_cust_name" name="list_cust_name"> 
<?php while($fetch_options_name = mysql_fetch_assoc($data_name)) { ?> 
<option value="<?php=$fetch_options_name['cust_name']; ?>"><?php=$fetch_options_name['cust_name']; ?></option> 
<?php } ?> 
</select> 

<select id="list_cust_city" name="list_cust_city"></select> 

city.php:

<?php 
    include('dbconnect.php'); 
    db_connect(); 
    $cust_name1=$_GET['cust_name']; 
    $data_city = mysql_query('SELECT DISTINCT cust_city FROM customer_db WHERE cust_name="'.$cust_name1.'" ORDER BY cust_city'); 
    while($fetch_options_city = mysql_fetch_assoc($data_city)) { 
    ?> 
    <option value="<?php=$fetch_options_city['cust_city'];?>"><?php=$fetch_options_city['cust_city'];?></option> 
    <?php 
    } 
?> 
+0

я добавил полный код, попробуйте. Я предлагаю вам использовать идентификаторы для клиентов и городов вместо строковых значений. – Jokey

+0

сделал sae все еще не получая .. – Yash

+0

Проверьте ваши заявки. Или вы можете предоставить некоторую информацию о своей структуре таблицы customer_db? – Jokey

0

Вы должны использовать документ готов, потому что DOM не сработал.

$(document).ready(function() { 
    $('#list_cust_name').change(function(){ 
    alert("heyyy"); 
    $.ajax({ 
    url:'city.php', 
    data:{cust_name:$(this).val()}, 
    success: function(data){ 
    $('#list_cust_city').html(data); 
    } 
    }); 
    }); 
}); 
Смежные вопросы