0

我确实有两个下拉菜单,称为源和活动,这两个下拉菜单显示来自数据库的数据。我也有其他输入字段。我担心的是我想在填充后保存这些数据在给定的输入和选择下拉数据中,它必须被保存,但在保存数据后,下拉菜单必须显示我在单击保存按钮时选择的选择,但它显示的是默认选项。我的代码如下:这是源代码:

 $result= mysql_query("SELECT * FROM infosources where kunnr = '".$_SESSION["kunnr"]."' order by sort_order asc");
    $model["source"]=array();
    while($row = mysql_fetch_array($result)){
        array_push($model["source"],$row);

}

这是针对广告系列:

$result= mysql_query("SELECT * FROM s_campaigns WHERE kunnr ='".$_SESSION["kunnr"]."' and active = 'true' order by name asc");
$model["campaign"]=array();
while($row = mysql_fetch_array($result)){
    array_push($model["campaign"],$row);

}

我的下拉列表如下:

<select name="srcid"> <?php foreach($model["source"] as &$obj){?>
                       <option  value=<?php echo $obj["srcid"];?>>    <?php echo $obj["srcname"];?> </option> 
                           <?php }?></select>

另一个下拉菜单是

<select name="camp_id"> <?php foreach($model["campaign"] as &$obj){?>
                       <option <?php if($model["selected"]==$obj[""]){?>selected <?php }?>  value=<?php echo $obj["id"];?>>    <?php echo $obj["name"];?> </option> 
                           <?php }?></select>

请在这方面给我建议...

4

2 回答 2

1

为此,您必须在刷新页面时确定视图中的值

于 2013-01-15T07:10:32.757 回答
0

这是一个非常简单的示例,如果您将表单发送到同一个文件,那么它只是工作......如果您将表单发送到任何其他文件,或者如果您将 $selectedCampId 变量从其他文件传递回此 html 表单

<?php

// fetch the database to get all possible campaings always... before form was saved and also afterwards
$result= mysql_query("SELECT * FROM s_campaigns WHERE kunnr ='".$_SESSION["kunnr"]."' 
    and active = 'true' order by name asc");

$model["campaign"]=array();
while($row = mysql_fetch_array($result)){
   array_push($model["campaign"],$row);
}

// initiate $selectedCampId .... if the form is sent ... this variable will be filled with the campaign_id so that we know which option was selected... otherwise it wil remain empty..
$selectedCampId= '';

// if the save button was pushed, the form method is POST and the camp_id is not empty
// save the value of the camp_id input field to the variable so that in the next step 
// we know which one was selected
if(!empty($_POST['camp_id'])){
    // validate the input and save the data to database
    // check which campaign id is selected and write it to variable
    $selectedCampId= $_POST['camp_id'];
}

?>
<?php // the form method is POST otherwise use $_GET to fetch the camp_id after the form was sent ?>
<form method="post">
<select name="camp_id"> 
    <?php foreach($model["campaign"] as &$obj): ?>
        <option 
           <?php // if form is sent and $selectedCampId is not empty ... echo selected = "selected" ---- otherwise echo empty string ?>
           <?php echo ($obj['id'] == $selectedCampId) ? 'selected = "selected"' : ""; ?>>
           <?php echo $obj["name"];?> 
        </option> 
    <?php endforeach;?>
</select>
</form>
于 2012-12-01T12:26:46.307 回答