Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从“获取值”中选择默认值_Php_Select_Html Select - Fatal编程技术网

Php 从“获取值”中选择默认值

Php 从“获取值”中选择默认值,php,select,html-select,Php,Select,Html Select,我的网站使用表单编辑来点击一条记录,然后打开一个表单,允许编辑所有的值,并保存以附加新值。然而,我面临的问题是select,它应该使用$\u GET中的默认值,但是默认值是按字母顺序排列的第一个值,而不是它实际应该是什么。所以问题是如果我编辑并保存,表单会自动在选择框中放置错误的值 我用于选择的代码如下所示: echo "<select name='ClientCode'>"; while ($row = mysql_fetch_array($result)) { e

我的网站使用表单编辑来点击一条记录,然后打开一个表单,允许编辑所有的值,并保存以附加新值。然而,我面临的问题是select,它应该使用$\u GET中的默认值,但是默认值是按字母顺序排列的第一个值,而不是它实际应该是什么。所以问题是如果我编辑并保存,表单会自动在选择框中放置错误的值

我用于选择的代码如下所示:

echo "<select name='ClientCode'>";
    while ($row = mysql_fetch_array($result)) {
    echo "<option  value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."</option>";}
    echo "</select><br>";   //CLOSE DROP DOWN BOX
echo”“;
while($row=mysql\u fetch\u数组($result)){
回显“$row['ClientName']”;}
回声“
”//关闭下拉框
如何编辑上述代码段以允许我的$cli=$\u GET[“ClientCode”]作为页面加载时的默认值显示在选择框中下拉列表代码,如下所示

echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
    $output = "<option  value='" . $row['ClientCode'] ."'";
    if(isset($_GET["ClientCode"]) && $row['ClientCode'] == $_GET["ClientCode"]){
        $output .= " selected='selected'";
    }
    $output .= ">" . $row['ClientName'] ."</option>";
    echo $output;
}
echo "</select><br>";
echo”“;
while($row=mysql\u fetch\u数组($result)){
$output=”“.$row['ClientName']。“”;
echo$输出;
}
回声“
”;
您需要选中$\u进入您的循环并对所需选项执行selected=“selected”

echo "<select name='ClientCode'>";
    while ($row = mysql_fetch_array($result)) {
    if (!empty($_GET["ClientCode") && $_GET["CleintCode"] == $row["ClientCode"]))
    {
     echo "<option  selected='selected' value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."
    </option>";
    }
    else
    {
       echo "<option  value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."
</option>";
     }
}
echo "</select><br>"
echo”“;
while($row=mysql\u fetch\u数组($result)){
如果(!empty($\u GET[“ClientCode”)&&&$\u GET[“CleintCode”]==$row[“ClientCode”]))
{
回显“.$row['ClientName']”
";
}
其他的
{
回显“.$row['ClientName']”
";
}
}
回声“

更改while循环,如:

$cli = isset($_GET["ClientCode"])?$_GET["ClientCode"]:"";
while ($row = mysql_fetch_array($result)) {
    $selected =  ($cli == $row['ClientCode']) ? "selected" : "";
    echo "<option  value='" . $row['ClientCode'] ."' $selected >" . $row['ClientName'] ."</option>";}
$cli=isset($\u GET[“ClientCode”])?$\u GET[“ClientCode”]:“”;
while($row=mysql\u fetch\u数组($result)){
$selected=($cli==$row['ClientCode'])?“selected”:“;
回显“$row['ClientName']”;}
使用此

if($row['ClientCode']=$\u GET[“ClientCode”])
{
?>
价值在这里。。。

@根据OP的问题,
$\u GET[“ClientCode”]
应该存在。但是,我同意你的观点,因此在那里添加了一个额外的检查。当然,但我建议始终进行请求检查并设置默认值,否则可能会出现问题。更重要的是,什么方法更有效或更空isset@Dimash不,我们这里不需要任何默认值。控件不会进一步,并且在 如果
block,如果未设置
$\u GET[“ClientCode”]
,或者它不等于当前的
$行['ClientCode']
,那么它不会导致任何问题。@Dimash这是不正确的。如果URL是example.com/index.php?x,那么
var\u dump(isset($\u GET['y'))
只会返回
false
。请先检查此项。B.Desai,谢谢。我将接受您的回答,因为(1)没有问题。2)与我的想法相反,非常简单,下面的其他答案仍然有效,但行数和复杂性增加了一倍