如何在select onchange事件中显示数据库中的记录

时间:2013-09-30 02:08:25

标签: javascript php mysql

我是PHP编码的初学者,只想问下如何使用下面的脚本显示记录?     

$option = '';
 while($row = mysql_fetch_assoc($get))
{
  $option .= '<option value = "'.$row['rfq'].'">'.$row['rfq'].'</option>';
}   
?>
<form>
 <select> 
 <option value="ALL">ALL</option>
<?php echo $option; ?>
</select>
</form>

enter image description here

页面运行时加载所有选项并显示表格中的所有记录,当我选择13-001时,我想显示13-001的记录。怎么做?

2 个答案:

答案 0 :(得分:0)

使用AJAX,老兄。这很酷很容易。

An Example

Another good example using jQuery

答案 1 :(得分:0)

再次执行相同的操作但使用WHERE子句。 另外我希望你使用mysqli而不是mysql - 它很快就会消失。

<form>
    <select name='rfq' onChange='this.form.submit()'>
    <option></option>
    etc...
    </select>
    <input name='Submit' value='Submit' type='submit'>
</form>

<?php

if(isset($_POST['submit'])) {

    $rfq = $_POST['rfq'];

    $query = "SELECT * FROM table WHERE rfq = '$rfq'";
    $result = mysqli_query($link, $query);

    while ($row = mysqli_fetch_array($result)) {
    echo "{$row['id']} <br>";
    echo "{$['field1']} <br>";
    echo "{$row['field2']} <br>";
    etc..
    }
}

?>

你当然需要格式化它,但这应该让你开始。