danmaxito Posted January 22, 2007 Share Posted January 22, 2007 Hey guys,Problem: I have an html form that has a dropdown list. When the user click on a name from the dropdown list and hits submit, the name is stored in a session. Now, I am trying to write some php code that says:If name = Freddy then $age = 4elseifname = Alex then $age = 6elseifname = Derek then $age = 2How would I go about in doing this? ThanksDanny Quote Link to comment Share on other sites More sharing options...
trq Posted January 22, 2007 Share Posted January 22, 2007 [code]<?php $name = isset($_POST['name']) : $_POST['name'] ? ''; switch ($name) { case 'Freddy': $age = 4; break; case 'Alex': $age = 6; break; case 'Derek': $age = 2; break; }?>[/code] Quote Link to comment Share on other sites More sharing options...
Lyleyboy Posted January 22, 2007 Share Posted January 22, 2007 An even simpler way, just in case you can't get your head round the case function is just to do exactly what you suggested.if ($name == "freddy") {$age = "4"}if ($name == "alex") {$age = "6"}if ($name == "derek") {$age = "2"} Quote Link to comment Share on other sites More sharing options...
Nelak Posted January 22, 2007 Share Posted January 22, 2007 try to use the switch tho its much better programming Quote Link to comment Share on other sites More sharing options...
danmaxito Posted January 22, 2007 Author Share Posted January 22, 2007 Awesome! Thanks guys for your help. As I was putting this code together (before I posted it here) I used the SWITCH method. I just wrote the code incorrectly, and since I am new to PHP, I was curious if I was doing things wrong.Thorpe - I used your code (Thanks by the way), but I had to edit some of the code for it to work:I changed [code]$name = isset($_POST['name']) : $_POST['name'] ? '';[/code]to[code]$name = $_POST["name"];[/code]Am I doing something wrong here?Danny Quote Link to comment Share on other sites More sharing options...
trq Posted January 22, 2007 Share Posted January 22, 2007 Sorry, there is a typo in my code. Should be....[code=php:0]$name = isset($_POST['name']) ? $_POST['name'] : '';[/code]Your code will produce a warning if $_POST['name'] is not set. Eg, The form hasn't been sent. Quote Link to comment Share on other sites More sharing options...
danmaxito Posted January 23, 2007 Author Share Posted January 23, 2007 ok, thanks. I will modify my code then. - On another note -Can I do something like this:[code]switch ($name) { case 'Freddy': $age = 4; $state = "FL"; $gender = "Male"; break;[/code]ThanksDanny Quote Link to comment Share on other sites More sharing options...
danmaxito Posted January 23, 2007 Author Share Posted January 23, 2007 I guess so... I tried it, and it seems to be working fine. Danny Quote Link to comment Share on other sites More sharing options...
danmaxito Posted January 23, 2007 Author Share Posted January 23, 2007 thorpe, I appreciate all your help. If at anytime, I am bugging you, let me know. Here is another problem that I am not understanding:I set everything to run smoothly. User goes to webpage, selects his name, and a session is then created with the users info. Once the users goes back to the homepage, his previous session is destroyed, and he is asked for is name again. Why is it destroying the session when it was not instructed to do so???I created an if statement on the home page that says, "If a session is created, then tell them that they are logged in as Freddy(Example) and give them the option to continue as Freddy, or to destroy the session and log in as someone else. Quote Link to comment Share on other sites More sharing options...
danmaxito Posted January 23, 2007 Author Share Posted January 23, 2007 Nevermind. I had a misspelling in my code :)It all works now.THANKS EVERYONE!!Danny Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.