内部选择的属性

时间:2013-07-20 11:45:48

标签: php html mysql

我有一个脚本从表中获取所有ID并在选项选择表单上打印它们,我想重新加载具有我在选项上选择的ID的页面。这是脚本:

<?php
include('include/menu.php');
include('include/mysql.php');
if ($db_found) {
echo "<form action='' name='form' method ='get'>
<select name='funcionario'>";
        $SQL = "SELECT * FROM funcionarios";
        $result = mysql_query($SQL);

        while ( $db_field = mysql_fetch_assoc($result) ) {
$idfunc = $_GET['funcionario'];
$selected = ($idfunc==$idfunc->$db_field['idfunc']) ? ' selected="selected"' : '';
                echo "<option value'".$db_field['idfunc']."' ".$select." onclick='document.form.submit();' >".$db_field['nomefunc']."</option>";
        }
        echo "</selected></form>";
        echo $idfunc;
} else {

print "Database NOT Found ";
mysql_close($db_handle);

}
?>

但是脚本总是只返回所选的第一个id。

2 个答案:

答案 0 :(得分:0)

$idfunc->

中删除$idfunc->$db_field['idfunc']

value'".$db_field['idfunc']."'替换为value='".$db_field['idfunc']."'

您已经定义了$idfunc = $_GET['functionario']这是一个字符串,而不是一个类对象。

您还定义了$selected,但在回显结果时使用的是$select

要进一步调试,请使用脚本顶部的error_reporting(E_ALL);。我想这就是你试图执行这个脚本时没有得到错误的原因。

这是完整的脚本:

<?php
include('include/menu.php');
include('include/mysql.php');
if ($db_found) {
echo "<form action='' name='form' method ='get'>
<select name='funcionario'>";
        $SQL = "SELECT * FROM funcionarios";
        $result = mysql_query($SQL);

        while ( $db_field = mysql_fetch_assoc($result) ) {
          $idfunc = $_GET['funcionario'];
          $selected = ($idfunc==$db_field['idfunc']) ? ' selected="selected"' : '';
          echo "<option value='".$db_field['idfunc']."' $selected onclick='document.form.submit();'>".$db_field['nomefunc']."</option>";
        }
        echo "</selected></form>";
        echo $idfunc;
} else {

print "Database NOT Found ";
mysql_close($db_handle);

}
?>

答案 1 :(得分:0)

$selected = ($idfunc)==$db_field['idfunc'] ? ' selected="selected"' : '';
  echo "<option value'".$db_field['idfunc']."' ".$selected." onclick='document.form.submit();' >".$db_field['nomefunc']."</option>";

我希望这是你想要的!试试这个!