Understanding the communication between an HTML and a PHP file

Hi,

I just started reading the book called “Build Your Own Database Driven Web Site Using PHP & MySQL” trying to learn PHP and so far I like it but questions starting to pop-up.

In chapter 3 theres some code that I dont know understand, there is a file called welcome1.html and welcome1.php and what I dont know understand is the relation between the two files, I know you can call php documents from an html page and I do understand whats doing but I just dont know understand how welcome1.php knows about the variable “name=Kevin” if this is part of and html file which in my eyes all its doing is linking to a file called welcome1.php.

Can someone explain how is this link initiating the php with this variable?

How does the php file about this variable if is coming from and html file?

WELCOME1.HTML file:

	<body>
		<p><a href="welcome1.php?name=Kevin">Hi, I&rsquo;m Kevin!</a></p>
	</body>

WELCOME1.PHP file:

	<body>
		<p>
			<?php
			$name = $_GET['name'];
			echo 'Welcome to our web site, ' . $name . '!';
			?>
		</p>
	</body>

Or may be what I dont know understand is how linking to a php files works.

Can you add multiples variables in between the link (<a href=“welcome1.php?name=Kevin, otherVar= someotherVar”>)? If the answer is yes, if this is php shouldn’t the variables start with a $ singn? Or even better is that considere php since its residing in a html file?

Thanks a lot!

Well basically the link to welcome.php has parameters which in this case is indicated by the ?.

When the server receives the request from your browser (Internet explorer, firefox etc) for welcome.php?name=Kevin then it automatically extracts the text before the ? and after the ? and treats them seperately. The first it treats as the filename it needs to open and the second becomes the parameter in the $_GET array. If the file it needs to open is a php file then it will run the php code and print any output to the browser.

The $_GET array is a superglobal meaning it is available to all of your script in any function. $_GET is formed from the parameters passed in the website address after the ?. $_POST is the same with one major difference - the data is passed in the headers instead of the address. You therefore can’t see this data through the browser, but your script can.


//This line takes the 'name' variable from the superglobal $_GET array and gives it a nice short easy name.
$name = $_GET['name'];
//This line then echoes the welcome and the name to the browser.
echo 'Welcome to our web site, ' . $name . '!';

Thats basically it! You won’t actually see how the server internally parses the address because its all done inside the server. All you need to know is that anything after the ? is parsed by the server and made available to you in the $_GET array for you to read and access.

Thanks a lot for your reply!

All you need to know is that anything after the ? is parsed by the server and made available to you in the $_GET array for you to read and access

This answers my question. Now is there a limitation as to how many variables or parameters you can put after the ‘?’ ?

Thanks

I think that urls (web addresses with or without parameters) are limited to 255 characters - I think… but don’t hold me to that.

It’s best to use post… but you can only use that by submitting a form. All hyperlinks are $_GET by default. In a form you can choose how to transmit the data - get or post. Post will send the data invisibly to the browser whilst get will just add it to the url after the ?.

Thanks a lot for your help!

No worries, come back here if we can help any more :wink: