help with retaining form data within dropdown fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gpittingale
Forum Newbie
Posts: 12
Joined: Fri Jan 23, 2009 5:58 am

help with retaining form data within dropdown fields

Post by gpittingale »

Hi everyone i cant seem to get my drop down boxes to retain the data the user selected is this code right?

Code: Select all

<select name="bap1" id="bap1" onchange='this.form.submit()' value="" >
      <option>Select One</option>
      <option value='Energy Efficient Cooking' <?php if($_POST['bap1'] == 'Energy Efficient Cooking') echo "selected"?>>Energy Efficient Cooking</option>
 
<select name="bap1" id="bap1" onchange='this.form.submit()' value="" > <option>Select One</option> <option value='Energy Efficient Cooking' <?php if($_POST['bap1'] == 'Energy Efficient Cooking') echo "selected"?>>Energy Efficient Cooking</option>
Thanks in advance anyone!!
sdas
Forum Newbie
Posts: 4
Joined: Fri Jan 30, 2009 4:46 am

Re: help with retaining form data within dropdown fields

Post by sdas »

<?php $temp = "";
if(trim($_POST['bap1']) == 'Energy Efficient Cooking') $temp = " selected" ;?>
<select name="bap1" id="bap1" onchange='this.form.submit()' value="" > <option>Select One</option> <option value='Energy Efficient Cooking' <? echo $temp;?> >Energy Efficient Cooking</option>
mubeena
Forum Newbie
Posts: 17
Joined: Fri Jan 30, 2009 4:26 am

Re: help with retaining form data within dropdown fields

Post by mubeena »

<script>
function selectcategory(sel1)
{
var selectedvalue=document.getElementById('sel').value;
document.getElementById('Filter2').value=selectedvalue;
document.admin.submit();
}
</script>

<?php
print "<form name='admin' id='admin' method='post'>";
print "<input type=hidden name='Filter2' id='Filter2' value='$Filter2'>";

print "<tr>";
if($passVal=='')
$passVal= -1;
$Filteroptions = array(0 => ' --------Select --------- ',1 => ' Author ',2 => ' Editor ',3 => ' Admin ');
$Filter=count($Filteroptions);


print " <td>";

print "<select class=tabledata3 name='sel' id='sel' onChange='selectcategory(this.form.sel)'>";

if($Filter>0)
{
for($i=0;$i<$Filter;$i++)
{
if($i==$Filter2)
{
print "<option value='$i' selected> $Filteroptions[$i] </option>";
}
else
print "<option value='$i' > $Filteroptions[$i] </option>";
}
print "</select>";
}

print "</td></tr>";

Check this sample code................. It may be useful for us
gpittingale
Forum Newbie
Posts: 12
Joined: Fri Jan 23, 2009 5:58 am

Re: help with retaining form data within dropdown fields

Post by gpittingale »

Its Not working :banghead: this mad why wont it work?
mubeena
Forum Newbie
Posts: 17
Joined: Fri Jan 30, 2009 4:26 am

Re: help with retaining form data within dropdown fields

Post by mubeena »

it is working fine for me........
gpittingale
Forum Newbie
Posts: 12
Joined: Fri Jan 23, 2009 5:58 am

Re: help with retaining form data within dropdown fields

Post by gpittingale »

The thing is i need a visable table so i have used html for the table instead of php becuse that way we dont start on a blank page so I need to do it that way.
mubeena
Forum Newbie
Posts: 17
Joined: Fri Jan 30, 2009 4:26 am

Re: help with retaining form data within dropdown fields

Post by mubeena »

hai,

i am not clear with your question?
gpittingale
Forum Newbie
Posts: 12
Joined: Fri Jan 23, 2009 5:58 am

Re: help with retaining form data within dropdown fields

Post by gpittingale »

well with php when you first navigate to the page, the page is blank because you have not called anything from the the server yet wher as i need it to be visable like this
http://www.brucesenvironment.co.uk/scdc ... letion.php
therefore i have already created the table with html and i am a bit of a novis at this so i could not intergrate the above code just to contain the bits needed to make it work.
sorry about last post just read it back to myself!!
mubeena
Forum Newbie
Posts: 17
Joined: Fri Jan 30, 2009 4:26 am

Re: help with retaining form data within dropdown fields

Post by mubeena »

<script>
function selectcategory(sel1)
{
//alert('sel1');
var selectedvalue=document.getElementById('sel').value;
//alert(selectedvalue);
document.getElementById('Filter2').value=selectedvalue;
document.admin.submit();
}

</script>
<?php

if(!isset($Filter2))
{
$Filter2 = '';
}


if(!isset($passVal))
{
$passVal = '';
}
print "<form name='admin' id='admin' method='post'>";
print "<input type=hidden name='Filter2' id='Filter2' value='$Filter2'>";




print "<table border=1 ><tr>";

print "<tr><td style='background-color:white;color:black;font-family:verdana;font-size:13px;font-weight:bold;'><font style='background-color:white;color:red;font-family:verdana;font-size:9px;font-weight:normal;' >*</font>&nbsp;&nbsp;Category</td>";
print " <td>";
if($passVal=='')
$passVal= -1;
$Filteroptions = array(0 => ' --------Select ---------- ',1 => ' Author ',2 => ' Editor ',3 => ' Admin ');
$Filter=count($Filteroptions);

print "<select class=tabledata3 name='sel' id='sel' onChange='selectcategory(this.form.sel)'>";
//<option value='' >---------Select---------</option>";

if($Filter>0)
{
for($i=0;$i<$Filter;$i++)
{
if($i==$Filter2)
{
print "<option value='$i' selected> $Filteroptions[$i] </option>";
}
else
print "<option value='$i' > $Filteroptions[$i] </option>";
}
print "</select>";
}


print "</td></tr>";
print "<table>";
?>
Post Reply