2012-09-04 57 views
0

我想customer_address,customer_city,customer_statecustomer_zip自動填充onChangecustomer_name下拉列表。我環顧四周,但我很難過。如何從mysql查詢結果onChange下拉菜單自動填充文本框的值?

請幫忙。

<form action="" method="post" name="booking_form" class="booking" style="width: auto"> 
<select name="customer_name" onChange="???????"> 
<option value="index.php">Click to select your school </option> 
<?php 

// Make a MySQL Connection 

$username=""; 

$password=""; 

$database=""; 

mysql_connect('localhost',$username,$password); 

@mysql_select_db($database) or die("Unable to select database"); 

$query = "SELECT * FROM table"; 
$result = mysql_query($query) or die(mysql_error()); 
while($row = mysql_fetch_array($result)){ 

$id = $row['id']; 
$school = $row['customer_name']; 
$phone = $row['customer_phone']; 
$address = $row['customer_address']; 
$city = $row['customer_city']; 
$state = $row['customer_state']; 
$zip = $row['customer_zip'];  
$notes = $row['customer_notes']; 
?> 
<option value="<?php echo $school; ?>"><?php echo "$school".", "."$city"; ?></option> 
<?php } 

?></select> 
<input type="text" name="customer_address"> 
<input type="text" name="customer_city"> 
<input type="text" name="customer_state"> 
<input type="text" name="customer_zip"> 
</form> 
+1

那麼你嘗試過這麼遠嗎? –

+0

http://stackoverflow.com/questions/4344747/jquery-load-form-elements-through-ajax-from-onchange-event-of-drop-down-box?rq=1 – Nathan

+0

編輯你的問題,並在那裏添加代碼在您的評論 – Yaroslav

回答

0

自從我原來的帖子以來的幾件事... mysql_已不再安全。此外,將所有內容放在一個文件中並不好,最好將您的視圖中的sql &業務邏輯分開,查找MVC模式。然而,這裏是快速&骯髒的方法: 你可以在這裏看到最終結果:http://jsfiddle.net/webaholik/kcce6Ls9/

<?php 
$sql = "SELECT id, 
    customer_name AS name, 
    customer_phone AS phone, 
    customer_address AS address, 
    customer_city AS city, 
    customer_state AS state, 
    customer_zip AS zip, 
    customer_notes AS notes 
    FROM table"; 
$schoolInfo = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); 

$array = array(); 
foreach ($schoolInfo as $item) { 
    $array[$item['id']] = $item; 
} 
$json = 'var SchoolInfo = ' . json_encode($array) . ';'; 
?> 
<form action="" method="post" name="booking_form" class="booking" 
style="width: auto"> 
<select id="customer_name" name="customer_name"> 
    <option value="index.php">Click to select your school</option> 
<?php foreach($schoolInfo as $row){?> 
    <option value="<?php echo $row['id']; ?>"><?php echo $row['name'].", ".$row['city']; ?></option> 
<?php } ?> 
</select> 
<input type="text" name="customer_address"> 
<input type="text" name="customer_city"> 
<input type="text" name="customer_state"> 
<input type="text" name="customer_zip"> 
</form> 
<script> 
<?php echo $json;?> 

$("#customer_name").change(function(){ 
    var sid = $(this).val(); 
    $("input[name='customer_address']").val(SchoolInfo[sid].address); 
    $("input[name='customer_city']").val(SchoolInfo[sid].city); 
    $("input[name='customer_zip']").val(SchoolInfo[sid].zip); 
}); 
</script> 
0

嘗試發佈一個PHP腳本並與Mysql建立連接,然後返回值。

<select name="customer_name"> 
<option id='selects' value="index.php">Click to select your school </option> 

的Javascript

 $("select[name='customer_name']").change(function(){ 
      $.post("-YOUR PHP FILE-", customer_name: $(this).val(), function(data){ 
       schools = data.split(","); 
       for(var i in schools){ 
       $("select[name='customer_name']").append("<option>"+schools[i]+"</option>"); 
       } 
      }) 
     }) 

PHP

 <?php 
      //You connected and got an array contains your school names($schools) 
      $schools = implode(",",$schools); 
      echo $schools; 
     ?> 

我認爲你正在嘗試做這樣的事情。

+0

沒有...不這麼認爲,我試圖讓我相應的$地址[] $ city [] $ state []到我的文本字段值onchangebox – webaholik

1

可以生成包含所有SQL結果像這樣的JavaScript對象:

var schoolInfo = { id1 : {name:"Name 1", address:"address line" ...}, 
id2: {} .... } 

然後設置各爲包括定製屬性 'SID':

<option value="school" sid="id1">school, city</option> 

對於使用jQuery元素的onchange事件處理程序:

.change(function(){ 
    var sid = $(this).attr('sid'); 
    $("input[name='customer_address']").val(schoolInfo[sid].address); 
    $("input[name='customer_city']").val(schoolInfo[sid].city); 
    $("input[name='customer_zip']").val(schoolInfo[sid].zip); 
}); 
相關問題