Forum Moderators: coopster

Message Too Old, No Replies

Extreme newbie needs help with passing arguments

         

fawkes_07

7:32 am on Jan 23, 2009 (gmt 0)

10+ Year Member



Hi gang!

I'm a total amateur webmaster who makes simple pages as a hobby. I came to this forum looking for help with javascript. One of the guys on that board suggested I try to solve my problem with php instead, and recommended the w3schools tutorial. That turned out to be a smooth move on his part, because now I'm hooked on PHP and bringing my utterly basic, lame questions to YOUR board.

However, after doing the tutorial, I need some help with a specific concept:

I want to pass an argument to a new .php file when I open it. I understand that I do this by writing something like

<a href="secondary.php?indexvar=20" "target="_blank">blah</a>

in my primary document.

How do I tell the secondary document that it will be receiving an argument? In other words, what code do I write in the file secondary.php, so that it's prepared to intercept and use an integer named indexvar which is currently barreling toward it from the calling page?

I hope that this is such a basic question it's just a line or two of code. I just need to know what that line is, and where to put it in the file.

Thanks in advance,
Jeanine

dreamcatcher

8:28 am on Jan 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jeanine,

Nice to see you on our forum, welcome. :)

secondary.php?indexvar=20

What you see after the ? is called a 'Query String'. You might have more than one in some cases:

secondary.php?indexvar=20&second=123 etc

To access them on the page they are being appended to, you use the PHP superglobal $_GET.

echo $_GET['indexvar'];

In your example this would show 20. PHP has a number of superglobals.

It is very important you filter content coming in via a $_GET query as anyone with a browser can simply change the value to something else. For example, if your value can only be an integer, make sure its an integer.

I would suggest you try looking at some tutorials first to see how PHP works. Maybe check the PHP website as all the information you need about PHP will be there.

[php.net...]

There are also a number of useful threads throughout this forum, including some in our library.

[webmasterworld.com...]

Good luck.

dc

fawkes_07

6:48 pm on Jan 23, 2009 (gmt 0)

10+ Year Member



Thanks dreamcatcher! Just to be painfully clear, then, all I have to do is type $_GET[indexvar] at some point iin my code, and then I can use $indexvar to parse my arrays?

I thought the $_GET command would ask for user input. So you're saying it will accept the passed argument also?

Cool.

Sorry, I'm an old Fortran programmer and I'm used to high precision language. :)

coopster

7:01 pm on Jan 23, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



http, the protocol, has a method called GET (there are web standards called "RFC" and RFC 2616 has all the gory details regarding HTTP). PHP has a superglobal variable, actually it is an array, named $_GET. So $_GET isn't a command, it is an array. Arrays have indices and they can be either numeric or associative.
$array[0] = 'Value'; // a numeric index 
$array['assoc'] = 123; // an associative index (sometimes called hashes)

Any values passed in a query string, which is what you see appended to a url after the question mark, are magically parsed and put into the $_GET superglobal array for you by PHP.

fawkes_07

9:46 pm on Jan 23, 2009 (gmt 0)

10+ Year Member



oooOOOoooh. I think I'm catching on.

So the array variable $_GET[random index number] will be the same value for all my php documents for a given website... right?

So when I type the ?variable=value at the end of the url, I'm basically manually stuffing a value into some element of the array, which it assigns to the index "variable"...

meaning $_GET[$variable] = "value"...

I think I can wrap my head around that.

OK, for a real life example: In my secondary.php page, I have a multidimensional array of images and text. The primary page is going to get a value for "indexvar" and pass it as above. Then in secondary.php, I'll be going through my multidimensional array like this:

$vaselist = array(
array(
"My Garden Rose",
"18",
"$200",
"shopping cart data",
"IMG_1223.jpg"
),

echo "<img src=\"vases/".$vaselist[$indexvar][4]."\" />";

//shows the image associated with that indexvar

<h1>echo " " .$vaselist[$indexvar][0]." "; ?></h1>

//shows the title associated with that indexvar

echo "Height: ".$vaselist[$indexvar][1]." inches.";

//shows the height of the vase in question,

etc. etc. So, to make all this happen, if I understand you correctly, I actually need to code it like this:

(in the primary page)

<a href="secondary.php?vasenum=20">

and then in the secondary page

$indexvar=$_GET[vasenum] (and $indexvar will have a value of 20 in this case)

Then I can check the value of $indexvar, make sure it's an integer, and use it for all my other nefarious purposes. Right?

Or, if I wanted the code to look even uglier, I could use the index $_GET[vasenum] instead of $indexvar in all those echo statements? As in:

echo "<img src=\"vases/".$vaselist[$_GET[vasenum][4]."\" />";

Do I have it, guys?

daveVk

4:23 am on Jan 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$indexvar=$_GET[vasenum] (and $indexvar will have a value of 20 in this case)

Best practice I think is to use $indexvar=$_GET["vasenum"]; with the quotes.

What is needed in the [] is the name part of the "name=value" from the query part of the url. Do not use $_GET[$vasenum], and while $_GET[vasenum] works, I find it confusing.

As you say, refer to it as $indexvar latter on ( having first checked it is a number ).

see example [w3schools.com...]

fawkes_07

9:36 am on Jan 24, 2009 (gmt 0)

10+ Year Member



Right ON! I've got the page working and it's incredible--does everything I want it to do, and no backtalk. That almost NEVER happens! It's official, I'm a PHP convert now.

Thanks for your patient replies!

--Jeanine