Using $_GET function to get $variable from another php file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shure
Forum Newbie
Posts: 7
Joined: Fri Mar 14, 2008 10:06 am

Using $_GET function to get $variable from another php file

Post by shure »

Im trying to get a variable ($username) from an input form, but I only know how to retrieve the data from an input type on that form using the following code:

$username = $_GET['username'];

I have tried adapting it to get a variable from the other php file but i cant see to have much luck :(

I have tried $username = $_GET[$username]; if that explain what im trying to do better. I am using a get method in the other php file and linking it to the php file that the above code is in.

I really appreciate the help on this forum, i do search the internet prior to posting so i dont waste anyone's time too much.

cheers
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Using $_GET function to get $variable from another php file

Post by flying_circus »

Shure,

Generally when you are working with forms, you will retrieve the data with the $_POST['variableName'] syntax.

Code: Select all

# Code for Step 1
<form action="step2.php" method="POST">
  <input name="playerName" type="text">
</form>
The next step is to write the code for step 2. The page for Step 2 is defined in your <form> element from step 1. The forms "action" property should point to step2.php and the method should be "POST".

Code: Select all

# Code for Step 2
<?php
  $playerName = $_POST['playerName'];
  
?>
shure
Forum Newbie
Posts: 7
Joined: Fri Mar 14, 2008 10:06 am

Re: Using $_GET function to get $variable from another php file

Post by shure »

thankyou for the reply, i understand where you are coming from, but i am trying to retrieve a variable instead of something input, so in an ideal then the code should be as below, but it doesnt work :(

$playerName = $_POST['$playerNameFromPage1'];
User avatar
micknc
Forum Contributor
Posts: 115
Joined: Thu Jan 24, 2008 11:13 pm

Re: Using $_GET function to get $variable from another php file

Post by micknc »

The best way to preserve the variable would be through sessions but if you are just needing one variable on one page through a link then you could do it like this:

Code: Select all

 
<a href="somepage.php?<? echo $yourvariable; ?>">Click here</a>
 
Then use your $_GET['yourvariable']; in the linked page.

Again, if need to keep the variable for a bunch of pages read up on sessions but this is a dirty way to do it for a quick link.
shure
Forum Newbie
Posts: 7
Joined: Fri Mar 14, 2008 10:06 am

Re: Using $_GET function to get $variable from another php file

Post by shure »

thankyou again. I am using sessions, but the feature i am developing means the session needs to be destroyed / unset upon accessing the page. The page then form actions a check page where the password entered on this page is compared to the $username variable taken from the session before it was destroyed.

This might sound a bit gobbledegook, but its how I am writing this lock feature which is similar to that of a windows vista lock feature (on start menu), but this time its for a website.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Using $_GET function to get $variable from another php file

Post by flying_circus »

Shure,

I guess I misunderstood you before. In my mind, I see 2 possible solutions, 1 would be cookies, the other would be to extract your session value to a variable (or cookie), destroy the session, then do what you are trying to do. Though, the 2nd possibility really doesnt make any sense to me, but then I dont know what you're trying to do.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: Using $_GET function to get $variable from another php file

Post by deejay »

wouldn't another solution be to put the info into a database and then pull it out of there when needed. or is that not applicable
Post Reply