HI

I have a drop down box after selecting it page must be refreshed and it must show the selected value.

How to do it

example I select one item in a dropdown and page must be refresh and show the selected value in drop down after refresh?

thanks

Member Avatar for diafol

javascript submit

php to reselect the value in the dropdown

javascript submit

php to reselect the value in the dropdown

I didnt got you

i have no button .

Member Avatar for diafol

Use the onchange attribute torun a .submit

Use the onchange attribute torun a .submit

My code

<select name="item" onchange="this.form.submit();" >
<?php

$data = @mysql_query("select * from Item");
$array = array();
while ($row = mysql_fetch_assoc($data))
{
$id = $row;
$Itemname= $row;
echo '<option value='.$id.'>'.$Itemname.'</option>'."\n";
}
?>
</select>

Problem -

page is refreshing but not showing selected value in drop down box.

Member Avatar for diafol

You need to get the selected value from $_POST or $_GET depending on your form method attribute.

<SCRIPT LANGUAGE="javascript">
function update_post()
{
document.frm.submit();
}
</SCRIPT>
 
<select id="productid" name="productid" onChange="update_post();">
<?php
$sql_all ="select * from product";
$qry_selectall=mysql_query($sql_all);
while($res_selectall=mysql_fetch_array($qry_selectall))
{
$content_all=$res_selectall['id'];
$code = $res_selectall['page_title'];
?>
<option id="prodid" value="<?php echo $content_all;?>" <?php if($content_all==$page_id) echo 'selected' ?> ><?php echo $code;?></option>
<?php
}
?>
</select>
<SCRIPT LANGUAGE="javascript">
function update_post()
{
document.frm.submit();
}
</SCRIPT>
 
<select id="productid" name="productid" onChange="update_post();">
<?php
$sql_all ="select * from product";
$qry_selectall=mysql_query($sql_all);
while($res_selectall=mysql_fetch_array($qry_selectall))
{
$content_all=$res_selectall['id'];
$code = $res_selectall['page_title'];
?>
<option id="prodid" value="<?php echo $content_all;?>" <?php if($content_all==$page_id) echo 'selected' ?> ><?php echo $code;?></option>
<?php
}
?>
</select>

i tried but not working...

<?php
$sql_all ="select * from Item ";
$qry_selectall=mysql_query($sql_all);
while($res_selectall=mysql_fetch_array($qry_selectall))
{
$id=$res_selectall['Item_id'];
$Itemname = $res_selectall['Item_name'];
?>

<option id="itemid" value="<?php echo $id;?>" <?php if($id==$page) echo 'selected' ?> ><?php echo $Itemname;?></option>  
 <?php }
?>
 </select>

Page refresh but not retaining select value.

my page - http://web.nmsu.edu/~madhira/Project/Edititem.php

Here you try by selecting books.

Thank you

Member Avatar for diafol
<?php
$data = @mysql_query("select * from Item");
$array = array();
$options="";  
while ($row = mysql_fetch_assoc($data)){
   $id = $row['Item_id'];
   $Itemname= $row['Item_name'];
   $sel = (isset($_POST['item']) && intval($_POST['item']) == $id) ? ' selected="selected"' : ''; 
   $options .= "<option value=\"$id\"$sel>$Itemname</option>\n";
}
?>
<form method="post">
  <select name="item" onchange="this.form.submit();" >
    <?php echo $options;?>
  </select>
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.