Passing Variables

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
TARCON
Forum Newbie
Posts: 12
Joined: Wed Dec 02, 2009 3:34 pm

Passing Variables

Post by TARCON »

See http://www.homejotter.com/new.index.php.

I am new to PHP. I want to get the college that the person has chosen and then I want to convert that college to the id.
Then when the person presses "Search" the results.php would be results.php?college=$collegeID, thus showing results only for that specific college. I know how to display the results, but I am just asking how to pass the variable.

This is the code relating to that college combo box and the search button. It is in HTML:

Code: Select all

<select name="college1" class="textfield" id="college1" value="" tabindex="2"></select>
<a href="results.php?college="$college1""><input name="search2" type="submit" class="search" id="search2" value="Search »"/></a>
Any help is wanted...

Thanks in advance.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Passing Variables

Post by Weiry »

You have 2 options to pass form data, either POST or GET.

GET passes data through the URL so a URL would look like index.php?myVar=aValue
To retrieve GET form data, you would use something like:

Code: Select all

if(!empty($_GET['myVar'])){
    $myVar = $_GET['myVar'];
}else{
    $myVar = "";
}
POST passes data to the next page without displaying it, which is what you would use for a login system.
To get POST data, its almost exactly the same as the $_GET method.

Code: Select all

if(!empty($_POST['myVar'])){
    $myVar = ${POST['myVar'];
}else{
    $myVar = "";
}
So you would need to 'ideally' create a form and pass the data that way

Code: Select all

<form name='myForm' method='get' action='results.php'> <!--- method is either GET or POST -->
<select name="collegeName" class="textfield" id="college1" value="" tabindex="2">
    <option value='collegeName1'>College 1</option>
    <option value='collegeName2'>College 2</option>
    <option value='collegeName3'>College 3</option>
</select>
<input type='submit' name='myFormSubmit' value='Search!'/>
</form>
TARCON
Forum Newbie
Posts: 12
Joined: Wed Dec 02, 2009 3:34 pm

Re: Passing Variables

Post by TARCON »

Thanks for the help. I actually figured it out before reading your reply. I tried searching for this topic, but I didn't really know what to search for or if there were a specific name for this process... I literally had no clue whatsoever.

Thanks for your reply.
Post Reply