Trying to keep a dropdown selection sticky

View: New views
7 Messages — Rating Filter:   Alert me  

Trying to keep a dropdown selection sticky

by Michael S. Dunsavage :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a form I want to keep sticky, but I can't figure out how. I got
all the <INPUT>'s to be sticky....

The select script


<?php
echo '<strong>State</strong><br>';
echo '<select name="State">';
        foreach ($state_list as $key => $value) {
                echo "<option value=\"$key\"> $value</option>\n";
        }
echo '</select>';
echo '<br>';



?>


so now how do I keep that sticky?

--
Michael S. Dunsavage


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Trying to keep a dropdown selection sticky

by Peter Jackson-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Michael S. Dunsavage wrote:

> I have a form I want to keep sticky, but I can't figure out how. I got
> all the <INPUT>'s to be sticky....
>
> The select script
>
>
> <?php
> echo '<strong>State</strong><br>';
> echo '<select name="State">';
> foreach ($state_list as $key => $value) {
> echo "<option value=\"$key\"> $value</option>\n";
> }
> echo '</select>';
> echo '<br>';
>
>
>
> ?>
>

If I'm reading this correctly you mean if the value has been selected
before select it again?
If so way I did it was by checking the database value
  eg
echo "<option value=\"$key\"
if ($row[id] == $key) {
       print "SELECTED"; }
echo "> $value </option>\n";

Peter Jackson


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Re: Trying to keep a dropdown selection sticky [solved] - Forgive the length of message, please

by Michael S. Dunsavage :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, 2008-07-05 at 21:30 +1000, Peter Jackson wrote:

> Michael S. Dunsavage wrote:
> > I have a form I want to keep sticky, but I can't figure out how. I got
> > all the <INPUT>'s to be sticky....
> >
> > The select script
> >
> >
> > <?php
> > echo '<strong>State</strong><br>';
> > echo '<select name="State">';
> > foreach ($state_list as $key => $value) {
> > echo "<option value=\"$key\"> $value</option>\n";
> > }
> > echo '</select>';
> > echo '<br>';
> >
> >
> >
> > ?>
> >
>
> If I'm reading this correctly you mean if the value has been selected
> before select it again?
> If so way I did it was by checking the database value
>   eg
> echo "<option value=\"$key\"
> if ($row[id] == $key) {
>        print "SELECTED"; }
> echo "> $value </option>\n";
>
> Peter Jackson

What I mean is when the users clicks submit, and the form is handled and
some information is lacking, it retains what information was given.

What I eventually did was:


foreach($state_list as $key=>$value){
if($selected_state == $key){
$sel = 'selected="selected"';
}
else{
$sel = '';
}
echo "<option $sel value=\"$key\">$value</option>";
}
echo '</select>';
?>
 

And then put
if (!empty($_POST))
  {
    $selected_state = $_POST['State'];
  }


At the beginning of the scripts.


Full script:


<?php

if (!empty($_POST))
  {
    $selected_state = $_POST['State'];
  }

 


if (isset($_POST['SUBMIT'])) { // Handle the form.



        $message = NULL; // Create an empty new variable.
       
        // Check for a name.
        if (strlen($_POST['FirstName']) > 0) {
                $FirstName = TRUE;
        } else {
                $FirstName = FALSE;
                $message .= '<p>You forgot to enter your first name.</p>';
        }


        if (strlen($_POST['LastName']) > 0) {
                $LastName = TRUE;
        } else {
                $LastName = FALSE;
                $message .= '<p>You forgot to enter your last name.</p>';
        }
       
        // Check for an email address.
        if (strlen($_POST['Email']) > 0) {
                $Email = TRUE;
        } else {
                $Email = FALSE;
                $message .= '<p>You forgot to enter your email address.</p>';
        }

        //Check for Address
                if (strlen($_POST['Address']) > 0) {
                $Address = TRUE;
        } else {
                $AddreSS= FALSE;
                $message .= '<p>You forgot to enter your address.</p>';
        }
       
        //Check for City
       
                if (strlen($_POST['City']) > 0) {
                $City = TRUE;
        } else {
                $City = FALSE;
                $message .= '<p>You forgot to enter your city.</p>';
        }
       
        //Check for Zip
       
                if (strlen($_POST['Zip']) > 0) {
                $Zip = TRUE;
        } else {
                $Zip = FALSE;
                $message .= '<p>You forgot to enter your zip code.</p>';
        }
       
        //Check for Comments
       
                if (strlen($_POST['Comments']) > 0) {
                $Commentsbody=$_POST['Comments'];
                $Comments = TRUE;
        } else {
                $Comments = FALSE;
                $message .= '<p>You forgot to enter any comments.</p>';
        }
       
        if ($FirstName && $LastName && $Email && $Address && $City &&  $Zip &&
$Comments) { // If everything's okay.
                // Register the user.
               
                // Send an email.
                $body = "Your comments were: \n\n ' $Commentsbody ' \n\n We will
repsond to your e-mail shortly.\n\nSincerely,\nMSD Computing";
                mail ($_POST['Email'], 'Thank you for contacting MSD Computing.',
$body, 'From: admin@...');
               
                header ('Location: thankyou.php');
                exit();
        } else {
                $message .= '<p>Please try again.</p>';
        }

}

$page_title="Contact MSD Computing";
include ('../phpinclude/header.inc');

if (isset($message)) {
        echo '<strong></strong><font color="#9B1003" size=4>', $message,
'</font></strong>';
}
?>

--
Michael S. Dunsavage


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Re: Trying to keep a dropdown selection sticky [solved] - Forgive the length of message, please

by tedd-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

At 11:31 AM -0400 7/5/08, Michael S. Dunsavage wrote:
>What I eventually did was:

-snip-

You might also consider adding a javascript routine to do the same
thing. Why? Because it's a bit quicker doesn't hit the server until
all the fields are filled.

Like this:

http://webbytedd.com/c/form-submit/

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: Trying to keep a dropdown selection sticky

by Warren Vail :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The selection will stick until you do a form submit.  The most common
practice in PHP is to update the database with the value from the form or at
least save it in a session.  After the update you can redisplay your form
after fetching the DB contents or getting the selection from the session
into a variable (say $choice) and modify the loop below in your form so that
the echo inside the loop is broken into the following;

       foreach ($state_list as $key => $value) {
          echo "<option value=\"$key\"";
          if($key == $choice) echo " selected"; // now it's sticky
;-)
          echo "> $value</option>\n";
       }

When you fail to indicate which item in a select list is "selected" most
browsers will default to showing the first entry (effectively unsticking
your choice).

Good luck,

Warren Vail

> -----Original Message-----
> From: Michael S. Dunsavage [mailto:mikesd@...]
> Sent: Friday, July 04, 2008 6:32 PM
> To: php-general@...
> Subject: [PHP] Trying to keep a dropdown selection sticky
>
> I have a form I want to keep sticky, but I can't figure out
> how. I got all the <INPUT>'s to be sticky....
>
> The select script
>
>
> <?php
> echo '<strong>State</strong><br>';
> echo '<select name="State">';
> foreach ($state_list as $key => $value) {
> echo "<option value=\"$key\"> $value</option>\n";
> }
> echo '</select>';
> echo '<br>';
>
>
>
> ?>
>
>
> so now how do I keep that sticky?
>
> --
> Michael S. Dunsavage
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: Trying to keep a dropdown selection sticky

by Michael S. Dunsavage :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, 2008-07-05 at 21:27 -0700, Warren Vail wrote:
> When you fail to indicate which item in a select list is "selected"
> most
> browsers will default to showing the first entry (effectively
> unsticking
> your choice).
>
> Good luck


ahhh! I was wondering where the "selected" came fro :)


Thanx for the explanation.
--
Michael S. Dunsavage


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: Trying to keep a dropdown selection sticky

by Michael S. Dunsavage :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, 2008-07-05 at 21:27 -0700, Warren Vail wrote:
>        foreach ($state_list as $key => $value) {
>           echo "<option value=\"$key\"";
>           if($key == $choice) echo " selected";         // now it's
> sticky
> ;-)
>           echo "> $value</option>\n";
>        }

Just another quick ?..

where is $choice coming from?


or is that the name of the select in <SELECT NAME='namehere'>
--
Michael S. Dunsavage


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php