dongxun5349 2013-05-27 15:17
浏览 47
已采纳

使用存储的会话变量预填充PHP动态选择选项

Im scratching my head once again and need your help.

What I have is a form that submits to a second page, with sessions enabled, i am storing the name value 2 fields on the form and setting their value names in the session so that if a user returns to a page their Username will already be populated in the text field. I have this working ok ..

The second page i am storing

$_SESSION['captured_by'] = $_POST['captured_by'];
$_SESSION['prid'] = $_POST['prid'];

HOWEVER..

Where i am stuck is getting a select option that is populated from a query to have the same functionality, so that when a user returns to the page, the selected option which has been saved in the session will keep that selection in the box rather than having to select it again.

Here is what i have as follows:

Form Page: This does work and does return the captured_by text when i return to the page

<fieldset><legend>Lesson Added By *</legend>
<p class="multiple">(Required - Logon ID)</p>
<input name="captured_by" maxlength="12" id="searchfield" type="text" value="<?php 
if(isset($_SESSION['captured_by'])){print stripslashes($_SESSION['captured_by']);}
else{print "   ";} ?>">
</fieldset>

The problem code is this

<select id="searchfield" name="prid">

<?php
 $query = "SELECT project_name, project_id FROM ll_project ORDER BY project_name ASC;";
 $result = mysql_query($query) or die(mysql_error());

 while($row = mysql_fetch_assoc($result)) {

 echo '<option value="'.$row['project_id'].'">'.$row['project_name'].' | '.$row['project_id'].'</option>';
 }
?>
</select>
</fieldset>

I need to edit this last section so that it picks up the previously selected option value from the stored session value.

Hope someone can help? Many Thanks. Tazzy

  • 写回答

2条回答 默认 最新

  • dongshou9878 2013-05-27 15:31
    关注

    Try this,

    <select id="searchfield" name="prid">
    
    <?php
     $query = "SELECT project_name, project_id FROM ll_project ORDER BY project_name ASC;";
     $result = mysql_query($query) or die(mysql_error());
    
     while($row = mysql_fetch_assoc($result)) {
    
     echo '<option value="'.$row['project_id'].'"'. ((isset($_SESSION['prid']) && !empty($_SESSION['prid']) && ($_SESSION['prid'] == $row['project_id'])) ? 'selected="selected"' : '') .'>'.$row['project_name'].' | '.$row['project_id'].'</option>';
     }
    ?>
    </select>
    </fieldset>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line