Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 20th, 2004, 08:14 AM
Authorized User
 
Join Date: Jul 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default return to value in <SELECT> statement

I have several drop down menus in my code that queries the db to pull out titles of people, e.g. Mr, Mrs, Miss, Ms, Dr etc...

I have validated my code to check that certain fields are complete but when the user clicks on my link to go back, the drop down menu reverts back to the top of the list. Is there a way to let the select statement return back to the value the user had selected?

here is the bit of code that I have currently got:

Code:
<?PHP
echo "<SELECT NAME=\"f_person_title\">";
while ($row=mysql_fetch_array($mysql_result))
{
    $title=$row["title"];
    echo "<OPTION>$title</option>";
}
echo "</SELECT>";
?>
Thanks all.
__________________
Michael.
 
Old September 23rd, 2004, 05:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, first off, <Option> elements must always have a value. Some browsers, such as the Mozilla family, always populate the option list implicitly using whatever isbetween the tags, but I think mopre old-fashioned renderers, like IE and Netscape 4.x, will choke on this.

Anyway, how are you preserving the input values from the form? If it's a a session, then try something like:

<?PHP
echo "<select name=\"f_person_title\" value=\"f_person_title\">";
while ($row=mysql_fetch_array($mysql_result))
{
    $title=$row["title"];
    if($title = $_SESSION['title']){
       $selected = " selected";
    }else{
       //Some may rail against such a dirty hack ;)
       $selected = "";
    }
    echo "<option value=\"$title\"$selected>$title</option>";
}
echo "</select>";
?>

Take it easy,
Dan
 
Old September 23rd, 2004, 05:52 AM
Authorized User
 
Join Date: Jul 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Dan,

Im not using sessions as i dont fully understand how they work, though it is something that I need to learn. As the code is only for Internal use by a few people at my work place, I am only passing the variables using forms and including them in the URL e.g.

Code:
project.mycompany.co.uk?f_person_title=$title
I see that you have put in the value tag within the select tag.
Is this one of the possible answer to this problem without using
Code:
$_SESSION[]
code?

Mike.

Michael.
 
Old September 25th, 2004, 08:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mikeuk
I see that you have put in the value tag within the select tag.
Is this one of the possible answer to this problem<snip />?

Mike.

Michael.
Well, its's probably part of it. Like I say, only a few browsers will implicitly insert whatever is between the option tags as the value passed for the overall select element. Either way, though, this should be manifestly apparent, since you won't see the value appended to the URL inthe address bar.

Your real problem, however, lies in the fact that you are allowing the user to go back via their back button. When that happens, the URL is always going to be empty of your input values. A better solution would be to perform all your validation at the top of your validation page and then, should the values fail to validate, explicitly redirect the user's browser back to the input form by using a header('location...') directive (see more about how to do this here http://uk2.php.net/manual/en/function.header.php - be careful about not sending any 'content' before the directive: the white-space-error has to be one of the commonnest in Web programming).

Now, using a header directive to explicitly redirecct the browser gives you greater control over the input system and obviates the need for the user to activate the Back button (you'd be surpsired how many users are unaware of the back button and what it is for!)

Say you redirect to the input pagee, appending all the values from the querystring that your validation page recieved.. Now, you just obtain all your values in the format $_GET['valuename']. Use the sam logic I outlined above, but use $_GET instead of $_SESSION.

I won't go into how you would cater for multiple <select>s, since few users are aware of how to enter them, and so they are sldom implemented. Suffice it to say, that when a <select type="multiple" ... > is entered, its values are entered as an array, even if only one selection was made, so all attempts to handle the values input must take accout of this fact (using in_array(), count() , array_search() and all the other array-handling functions, for instane).

I hope all f that makes some sense. Anyway, have a go, and if you get stuck agai, just post us the code and I'll try to help some more.

Take it easy,
Dan





Similar Threads
Thread Thread Starter Forum Replies Last Post
<select> </select> prabhash_singh11 PHP How-To 2 March 11th, 2008 09:08 AM
SELECT statement nullifying RETURN value Bob Bedell ADO.NET 2 January 18th, 2008 09:17 PM
How to implement a function return List<T> jdang67 C# 2005 2 January 30th, 2006 11:28 AM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
HELP: XSL -> HTML <select selected=true> jedbartlett XSLT 4 October 7th, 2004 11:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.