tefuzz Posted February 22, 2009 Share Posted February 22, 2009 i am a beginner at PHP, so im posting this to ask if this is a no-no, or if this is ok. it's just a snippet but the idea behind it is that user will chose which type of user they are, and the page will load the correct information for that user... <?php if ($_GET['type'] == '') { echo '<a href="register.php?type=tutor">TUTOR</a> or <a href="register.php?type=student">STUDENT</a>'; } elseif ($_GET['type'] == "tutor") { echo 'you have chosen tutor'; } else { echo 'you have chosen student'; } Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/ Share on other sites More sharing options...
blueman378 Posted February 22, 2009 Share Posted February 22, 2009 na mate your on the right track. although use if (empty($_GET['type'])) { bla bla bla instead of if ($_GET['type'] == '') { Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/#findComment-768179 Share on other sites More sharing options...
.josh Posted February 22, 2009 Share Posted February 22, 2009 Your condition structure assumes that your GET var exists. It starts out by assuming that it exists but has a 0 length value. It only happens to work on first load because a variable that doesn't exist coincidentally happens to have a 0 length value. If the rest of your script is setup to always be passing that var via GET, and it's a matter of whether it's '' or something else, then your condition is okay. But if its not, consider using isset You also have it setup to where if $_GET['type'] exists and is anything other than '' or 'tutor', then it must be a student. So if I were to enter this into the url: http://www.yoursite.com/yourscript.com?type=somerandomvalue your student message will appear. If that is your intention, then sure, you're good to go. Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/#findComment-768181 Share on other sites More sharing options...
blueman378 Posted February 22, 2009 Share Posted February 22, 2009 cheers crayon, only reason i didnt point that out is because it seems his question was is it ok to pass a variable on the same page? Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/#findComment-768185 Share on other sites More sharing options...
tefuzz Posted February 22, 2009 Author Share Posted February 22, 2009 ok so i started messing with it and came up with this... <?php $type = $_GET['type']; if (isset($type)) { if ($type == "tutor") { echo 'you have chosen tutor'; } else { if ($type == "student") { echo 'you have chosen student'; } else { echo '<a href="register.php?type=tutor">TUTOR</a> or <a href="register.php?type=student">STUDENT</a>'; } } } ?> the problem however is that it only works if i enter the ?type=tutor if there is no ?type= in the url it dosnt display the 2 links like i want it to. is it a problem with how i set the $type by using the $_GET? Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/#findComment-768196 Share on other sites More sharing options...
blueman378 Posted February 22, 2009 Share Posted February 22, 2009 im proborably wrong but $type = $_GET['type']; if (isset($type)) isnt he setting $type? even if it is set as null? shouldnt it be if (!empty($type)) for what he is doing? ...in comes crayon to make me look dumb Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/#findComment-768199 Share on other sites More sharing options...
tefuzz Posted February 22, 2009 Author Share Posted February 22, 2009 ok something just came to me, and i went ahead and changed the code to this, it now works to display the links, but if i pass a random value it still thinks i have chosen tutor... <?php $type = $_GET['type']; if (isset($type) && $type='tutor') { echo 'you have chosen tutor'; } elseif ($type == "student") { echo 'you have chosen student'; } else { echo '<a href="register.php?type=tutor">TUTOR</a> or <a href="register.php?type=student">STUDENT</a>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/#findComment-768204 Share on other sites More sharing options...
tefuzz Posted February 22, 2009 Author Share Posted February 22, 2009 ok so i went back again after reading your reply blueman387, and came up with this...If i load the page, it displays my links, if i click either link it echo's the right line. as well, if i enter test.php?type=random it echos the links like i wanted it to. now, is this an acceptable way to accomplish this? <?php if (empty ($_GET['type'])) { echo '<a href="register.php?type=tutor">TUTOR</a> or <a href="register.php?type=student">STUDENT</a>'; } elseif (isset($type) && $type="tutor") { echo 'you have chosen tutor'; } elseif ($type == "student") { echo 'you have chosen student'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/#findComment-768211 Share on other sites More sharing options...
.josh Posted February 22, 2009 Share Posted February 22, 2009 If you are wanting it to show tutor message if type==tutor, student message if type==student, and the links if it doesn't exist or if tutor==anything else, you can do this: <?php $type = $_GET['type']; switch($type) { case "tutor" : echo 'you have chosen tutor'; break; case "student" : echo 'you have chosen student'; break; default: echo '<a href="?type=tutor">TUTOR</a> or <a href="?type=student">STUDENT</a>'; } // end switch ?> Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/#findComment-768220 Share on other sites More sharing options...
tefuzz Posted February 22, 2009 Author Share Posted February 22, 2009 crayon that looks good, but i have a question...how is the code i posted working correctly? I mean while reading your reply it caught my eye that i have $type in there, but i never defined a $type in the code...does the $_GET['type'] automatically do so, or...? Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/#findComment-768224 Share on other sites More sharing options...
Q695 Posted February 22, 2009 Share Posted February 22, 2009 I personally like if (!$_GET['type']){ ... } else { } Quote Link to comment https://forums.phpfreaks.com/topic/146317-passing-a-value-inside-the-same-page/#findComment-768323 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.