2013-12-12 2 views
0

저는 PHP로 간단한 검색 페이지를 만들었습니다. 양식 제출시 드롭 다운 목록의 선택 가치를 어떻게 유지할 수 있는지 알아야합니다. 현재이 값은 첫 번째 인덱스로 재설정됩니다.PHP에서 양식 제출 후 드롭 다운의 선택된 항목의 값을 유지하는 방법은 무엇입니까?

클라이언트 측 스크립트를 사용하지 않고 PHP를 통해이 작업을 수행 할 수 있습니까? 일반적으로이 같은

<?php 

mysql_connect('localhost','root',''); 

mysql_select_db('hotel'); 

$sql = "SELECT * FROM customers"; 

$result = mysql_query($sql); 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 


<form method="get"> 

<select name="field" id="field"> 

<?php 

/*if($field == 'Active') 
'selected="selected"'; 
*/ 

while($rows = mysql_fetch_array($result)) 
    echo '<option>'.$rows['customer_id'].'</option><br>'; 
?> 


</select> 


<?php 



if (isset($_GET['Search']) && $_GET['action']=='search') 
{ 




    $sql="SELECT * FROM customers WHERE customer_id=".$_GET['field']; 

    $result = mysql_query($sql); 
    $row = mysql_fetch_assoc($result); 
    echo '<br>Customer Name: '.$row['customer_name'].'<br>'; 
    echo 'Email Address: '.$row['Email_Addr'].'<br>'; 
    echo 'Contact No: '.$row['Contact_No'].'<br>'; 

} 


?> 


<input type="hidden" name="action" value="search" /> 
<br><input type="submit" value="search" name="Search" onclick="" /> 

</form> 

</body> 
</html> 

답변

2

: 여기

는 코드입니다.

echo '<option'; 
if ($_GET['field'] == $rows['customer_id']) echo " selected"; 
echo '>'.$rows['customer_id'].'</option>'; 

그리고 당신은 배우고 특히, 새로운 코드를 작성하는 mysql_* 기능을 사용하지 마십시오. mysql_* 함수는 더 이상 사용되지 않게되며 현재 버전의 PHP에서 제거 될 예정입니다. 대신 mysqli_* 또는 PDO 개체를 사용하십시오.

while($rows = mysql_fetch_array($result)) 
    echo '<option value="'.$rows['customer_id'].'" '.($rows['customer_id'] == $_GET['field']?'selected="selected"':'').'>'.$rows['customer_id'].'</option><br>'; 
0

확인할 수 있습니다 :

while($rows = mysql_fetch_array($result)){ 
    if(!empty($_GET['field']) && $_GET['field'] == $rows['customer_id']){ 
     echo '<option selected="selected">'.$rows['customer_id'].'</option><br>'; 
    } 
    else { 
     echo '<option>'.$rows['customer_id'].'</option><br>'; 
    } 
} 
0

이 옵션을 선택을 인쇄 할 때이 값을 확인 할 수 어쩌면이 같은 선택에 어떤 mathing 옵션을 설정 가져 오기 값이 선택 값보다 같은 경우

관련 문제