Jump to content

retaingin pulldown value


wkilc

Recommended Posts

<form>
<select name="trees">
<option value="maple">maple</option>
<option value="oak">oak</option>
<option value="pine">pine</option>
</select>
</form>

 

Is there a simple way to use PHP to "hold" (echo selected?) the last value that was selected by the user?

 

Thank you.

 

~Wayne

Link to comment
Share on other sites

<?php
if(isset($_GET['selected']) && !empty($_GET['selected'])) {
if($_GET['selected'] == 'maple') {
$selected = 'selected'
}
} else {
$selected = '';
}
?>
<form>
<select name="trees">
<option value="maple" <?=$selected?>>maple</option>
<option value="oak">oak</option>
<option value="pine">pine</option>
</select>
</form>

 

do that for all 3 of em, using a switch would be better.

 

Regards ACE

Link to comment
Share on other sites

Assuming your form is posting to itself....

 

<form method="post" action="">
 <select name="trees">
   <option value="maple"<?php echo (isset($_POST['trees']) && $_POST['trees'] == 'maple') ? ' selected="selected"' : ''; ?>>maple</option>
   <option value="oak"<?php echo (isset($_POST['trees']) && $_POST['trees'] == 'oak') ? ' selected="selected"' : ''; ?>>oak</option>
   <option value="pine"<?php echo (isset($_POST['trees']) && $_POST['trees'] == 'pine') ? ' selected="selected"' : ''; ?>>pine</option>
 </select>
</form>

Link to comment
Share on other sites

Thank you.

 

I'm still having trouble, as my form is more complicated than my example.  This is what I've got... no errors, but it's not working to hold the value:

 

<select name="link">
<option value="<? echo "$page_name" ?>&limit=25"<?php echo (isset($_POST['link']) && $_POST['link'] == '$page_name&limit=25') ? ' selected="selected"' : ''; ?>>25</option>
<option value="<? echo "$page_name" ?>&limit=50"<?php echo (isset($_POST['link']) && $_POST['link'] == '$page_name&limit=50') ? ' selected="selected"' : ''; ?>>50</option>
</select>

 

Thank you again.

 

~Wayne

Link to comment
Share on other sites

Thank you once again!

 

The code now reads:

 

<select name="link">
<option value="<? echo $page_name ?>&limit=25"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=25") ? ' selected="selected"' : ''; ?>>25</option>
<option value="<? echo $page_name ?>&limit=50"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=50") ? ' selected="selected"' : ''; ?>>50</option>
</select>

 

But the list still defaults to the first value... and when I view the processed source code, there's no evidence of the "selected".

 

Thanks again,

 

~Wayne

Link to comment
Share on other sites

Does your form use the post or get methods? Also, you still didn't change a part.

 

<select name="link">
<option value="<?php echo $page_name; ?>&limit=25"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=25") ? ' selected="selected"' : ''; ?>>25</option>
<option value="<?php echo $page_name; ?>&limit=50"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=50") ? ' selected="selected"' : ''; ?>>50</option>
</select>

 

Where is $page_name defined? Why do you even need it there?

Link to comment
Share on other sites

$page_name = $_SERVER["REQUEST_URI"];

 

Post, I think.

 

It grabs the current URL, with all the current queries, and then the form is actually a link that takes that URL and modifies "limit", changing the number of records displayed on a page.  It's worked in other instances exactly the way I want it to.

 

Sorry, I was missing the semicolons... I've corrected that, still no dice:

 

<select name="link">
<option value="<?php echo $page_name; ?>&limit=25"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=25") ? ' selected="selected"' : ''; ?>>25</option>
<option value="<?php echo $page_name; ?>&limit=50"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=50") ? ' selected="selected"' : ''; ?>>50</option>
</select>

 

Thanks,

 

~Wayne

Link to comment
Share on other sites

<form name="form" method="post">
<select name="link">
<option value="<? echo $page_name; ?>&limit=25"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=25") ? ' selected="selected"' : ''; ?>>25</option>
<option value="<? echo $page_name; ?>&limit=50"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=50") ? ' selected="selected"' : ''; ?>>50</option>
</select>
 <input type="button" value="Go" onClick="openURL()">
</form>

 

The link is fired by this JS:

<script type="text/javascript"><!--
function openURL()
{
      selInd = document.form1.link.selectedIndex;

      goURL = document.form1.link.options[selInd].value;

      top.location.href = goURL;
}

//-->
</script>

 

Thank you.

 

~Wayne

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.