Jump to content

passing a value inside the same page...


tefuzz

Recommended Posts

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';
}

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>';
}
?>

 

 

Link to comment
Share on other sites

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'; }
?>

 

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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...?

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.