如何在发布请求后保留表单项?

时间:2010-04-05 11:08:39

标签: php html list forms select

我有一个简单的html表单。在PHP页面上。表格上有一个简单的清单。我将此表单(选定列表项)提交到此页面,以便我刷新页面。我希望在提交表单后选择POSTED的项目。

对于我的表单,我使用这样的代码,并且它工作正常,但它是最优的还是你可以为我的代码建议一些优化?

<form action="FormPage.php" method="post">
   <select id="Streams" class="multiselect ui-widget-content ui-corner-all" multiple="multiple" name="Streams[]">
     <?php      
     $query = "
SELECT s.streamId, s.userId, u.username
FROM streams AS s
JOIN user AS u ON s.userId = u.id
LIMIT 0 , 30
  ";
$streams_set = mysql_query($query, $connection);
    confirm_query($streams_set);    
    $streams_count = mysql_num_rows($streams_set);

while ($row = mysql_fetch_array($streams_set)){


      if (isset($_POST['submitForm'])){

      $array =  $_POST[Streams];
$count = count($array);
echo ",sid=" ;
for ($i = 0; $i < $count; $i++) {
 if($array[$i] == $row['streamId']){  echo '<option value="' , $row['streamId'] , '" selected="selected" >  ' , $row['username'] , ' (' , $row['streamId'] ,')' ,'</option> ';    }else
     {  echo '<option value="' , $row['streamId'] , '">  ' , $row['username'] , ' (' , $row['streamId'] ,')' ,'</option> ';  }
 }
} else { echo '<option value="' , $row['streamId'] , '">  ' , $row['username'] , ' (' , $row['streamId'] ,')' ,'</option> ';}

}
      </select>
      <br/>
      <input type="submit" class="ui-state-default ui-corner-all" name="submitForm" id="submitForm"  value="Play Stream from selected URL's!"/>    
  </fieldset>
</form>

4 个答案:

答案 0 :(得分:4)

当您收到POST时,您需要正确更新数据库并在selected元素上设置<option>属性。例如:

$streams = $_POST['Streams'];
$selected = array_combine($streams, array_fill(0, count($streams), true);
$query = <<<END
SELECT s.streamId, s.userId, u.username
FROM streams AS s
JOIN user AS u ON s.userId = u.id
LIMIT 0 , 30
END;
$streams_set = mysql_query($query, $connection);
confirm_query($streams_set);    
$streams_count = mysql_num_rows($streams_set);
while ($row = mysql_fetch_array($streams_set)) {
  $id = $row['streamId'];
  $sel = $selected[$id] ? ' selected' : '';
  echo '<option value="' , $row['streamId'] , '"' . $sel . '>  ' , $row['username'] , ' (' , $row['streamId'] ,')' ,'</option> ';
}

话虽如此,这不被视为最佳做法,因为如果用户使用浏览器上的后退按钮,它可能会无意中提交表单。出于这个原因,许多人更喜欢使用POST+REDIRECT+GET pattern

在您的情况下,脚本将看到它是POST并将数据保存到数据库。然后它会将HTTP重定向发送回用户以重新加载页面或加载不同的URL(根据需要)。该GET请求将包含已保存的信息。这导致额外的服务器往返,但通常会有更好的用户体验。

答案 1 :(得分:2)

<form action="FormPage.php" method="post">
   <select id="Streams" class="multiselect ui-widget-content ui-corner-all" multiple="multiple" name="Streams[]">
     <?php      
     $query = "
SELECT s.streamId, s.userId, u.username
FROM streams AS s
JOIN user AS u ON s.userId = u.id
LIMIT 0 , 30
  ";
$streams_set = mysql_query($query, $connection);
    confirm_query($streams_set);    
    $streams_count = mysql_num_rows($streams_set);

while ($row = mysql_fetch_array($streams_set)){
 echo '<option value="' . $row['streamId'] . '"' . (in_array($row['streamId'], $_POST['Streams']) ? ' selected' : ''). '>  ' . $row['username'] . ' (' . $row['streamId'] .')' .'</option> ';
} ?>
      </select>
      <br/>
      <input type="submit" class="ui-state-default ui-corner-all" name="submitForm" id="submitForm"  value="Play Stream from selected URL's!"/>    
  </fieldset>
</form>

答案 2 :(得分:0)

选择1.使用AJAX删除刷新并实现表单提交

选择2.当作为URL参数/或其他一些使用会话传递机制的其他变量重新加载时,将post参数发送回表单,并在页面重新加载时通过从URL / session获取值来在表单中显示它们。

答案 3 :(得分:0)

首先,select字段的名称必须是“Streams []”而不是“Streams”。或者你不会有多种选择。 此外,您的代码对我来说太过分了。我建议你把它分成两部分:业务逻辑和表示逻辑 首先,我们获得所有数据:

<?php      
$query = "
SELECT s.streamId, s.userId, u.username
FROM streams AS s
JOIN user AS u ON s.userId = u.id
LIMIT 0 , 30
";
$streams_set = mysql_query($query, $connection);
confirm_query($streams_set);    
$streams_count = mysql_num_rows($streams_set);

while ($row = mysql_fetch_array($streams_set)){
  if (in_array($row['streamId'], $_POST['Streams'])) {
    $row['sel'] = 1; 
  } else {
    $row['sel'] = 0;
  }
  $select1[]=$row;
}

然后,输出

<form action="FormPage.php" method="post">
   <select id="Streams" class="multiselect ui-widget-content ui-corner-all" multiple="multiple" name="Streams[]">
   <? foreach ($select1 as $row): ?>
     <option value="<?=$row['streamId']?>" <?if($row['sel'])?>selected<?endif?> >
       <?=$row['username']?> ( <?=$row['streamId']?> )
     </option>
   <? endforeach ?>
   </select>
      <br/>
      <input type="submit" class="ui-state-default ui-corner-all" name="submitForm" id="submitForm"  value="Play Stream from selected URL's!"/>    
  </fieldset>
</form>

看起来很整洁,不是吗?