Jump to content

[SOLVED] how to retain the post value


suyesh.amatya

Recommended Posts

<form name="houston" action="" method="post">

<select name="houston"  onchange="this.form.submit();">

<option value="">--Houston Criminal Attorney--</option>

<?

include('../include/cls_blog.php');

$objBlog=new blog;

$result=$objBlog->selectAllSites();

while($rows=mysql_fetch_array($result)){

 

?>

 

<option value="<?=$rows[site]?>"><? echo $rows[site];?></option>

 

<?

  }

?>

</select>

</form>

 

 

The select box has been populated by the values from database ie $rows[site].

Now on posting the form I want to preserve the post value which is selected.How can I do this in the above code.

Link to comment
https://forums.phpfreaks.com/topic/118228-solved-how-to-retain-the-post-value/
Share on other sites

Well, you need to check if the current value in the loop is equal to the selected.

 

Something like this perhaps:

<form name="houston" action="" method="post">
<select name="houston"  onchange="this.form.submit();">
<option value="">--Houston Criminal Attorney--</option>
<?php
include('../include/cls_blog.php');

$selected = "";

$objBlog=new blog;
$result=$objBlog->selectAllSites();

while($rows=mysql_fetch_array($result)){

if(isset($_POST['houston'])){
//They have submitted the form
if($rows['site'] == $_POST['houston']){
$selected = "selected='selected'";
}else{
$selected = "selected='selected'";
}

}else{
//Not submitted, no default selection
$selected = "";
}
?>

<option value="<?php echo $rows['site']; ?>" <?php echo $selected; ?>><?php echo $rows['site']; ?></option>
      
<?php
}
?>
</select>
</form>

 

Good luck.

<form name="houston" action="" method="post">

<select name="houston"  onchange="this.form.submit();">

<option value="">--Houston Criminal Attorney--</option>

<?

include('../include/cls_blog.php');

$objBlog=new blog;

$result=$objBlog->selectAllSites();

while($rows=mysql_fetch_array($result)){

 

?>

 

<option value="<?=$rows[site]?>" selected="<? if($_POST[houston]==$rows[site]) echo "selected"; else echo "";?>"><? echo $rows[site]?></option>

 

<?

  }

?>

</select>

</form>

Now it always displays the last select option

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.