Page 1 of 1
Using $_GET function to get $variable from another php file
Posted: Mon Mar 24, 2008 4:28 pm
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
Re: Using $_GET function to get $variable from another php file
Posted: Mon Mar 24, 2008 5:21 pm
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'];
?>
Re: Using $_GET function to get $variable from another php file
Posted: Mon Mar 24, 2008 5:59 pm
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'];
Re: Using $_GET function to get $variable from another php file
Posted: Mon Mar 24, 2008 6:10 pm
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.
Re: Using $_GET function to get $variable from another php file
Posted: Mon Mar 24, 2008 6:20 pm
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.
Re: Using $_GET function to get $variable from another php file
Posted: Mon Mar 24, 2008 10:53 pm
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.
Re: Using $_GET function to get $variable from another php file
Posted: Tue Mar 25, 2008 9:12 am
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