2012-12-01 86 views
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> 

和其他降down is

<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> 

请在此建议我...

+0

什么是这两个表的表模式? –

+0

这个来源有它自己的被称为“srcid”,并且该活动拥有它自己的“camp_id” –

+1

是的,但是你正在装载一张桌子,并寻找“selected”列。我假设在桌子上。这就是为什么如果你可以发布'DESCRIBE s_campaigns'和'DESCRIBE infosources',那将是一件好事。 –

回答

0

这是一个非常简单的示例,只是在您将表单发送到同一文件时工作...如果您将表单发送到任何其他文件或者如果您通过从另一个文件回到这个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> 
+0

看看我有一个保存按钮,这两个下拉菜单和一些输入文本第一个我必须在输入文本中输入一些数据,然后从数据库中选择其中的一个下拉列表,然后单击保存按钮。所有数据都将被保存,但关注点在于点击保存按钮后刷新后,相同的数据显示在这个过程的每次下拉,我希望相同的数据或选择必须显示在下拉列表中我点击保存按钮之前选择。请帮助我为了这。你给定的代码是nt wrkng –

+0

看我有一个保存按钮,这两个下拉和一些输入文本首先我必须把一些数据输入文本,然后选择任何一个下拉是哪些来自数据base.and然后点击保存按钮。所有的数据正在保存,但关注点击保存按钮后,页面刷新,并在它之后相同的数据显示在这个过程的每一次下拉,我希望必须在下拉菜单中显示相同的数据或选择内容,然后点击保存按钮。请为我提供帮助。你给的代码是nt wrkng –

1

的,你必须在你刷新页面的时间来决定鉴于价值$ selectedCampId变量