Trouble understanding php

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
nick2price
Forum Newbie
Posts: 18
Joined: Fri Nov 13, 2009 5:49 pm

Trouble understanding php

Post by nick2price »

Hi, I am new to this site, but hopefully I will get into php quite a bit.

I am having an issue understanding somthing quite simple about a login form. I see all these tutorials, and they put everything in a php file, but then how do I get this displayed in my html. I would of though I have my normal html page, with a form for login, and when the submit button is pressed, it then calls up the php check login page. I suppose I am just struggling with integrating the two languages.

I could understand it better if things were done the way I imagined it. For example, I have this from a tutorial

Code: Select all

 
2. Create file main_login.php.
 
———————————————————————–
 
<table width=”300? border=”0? align=”center” cellpadding=”0? cellspacing=”1? bgcolor=”#CCCCCC”>
<tr>
<form name=”form1? method=”post” action=”checklogin.php”>
<td>
<table width=”100%” border=”0? cellpadding=”3? cellspacing=”1? bgcolor=”#FFFFFF”>
<tr>
<td colspan=”3?><strong>Member Login </strong></td>
</tr>
<tr>
<td width=”78?>Username</td>
<td width=”6?>:</td>
<td width=”294?><input name=”myusername” type=”text” id=”myusername”></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name=”mypassword” type=”text” id=”mypassword”></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type=”submit” name=”Submit” value=”Login”></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
Why would they call this main_login.php. Surely this code should be in my html file, and as I say, when submit is pressed, it calls checklogin.php.

Could someone please enlighten me on what I am missing here. I have created a html file and I have a nice page, can I just use the above code in my html page, to create my login form, and keep it html?
Is it a mistake them calling it php? Any advise severely appreciated :D

cheers
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Trouble understanding php

Post by califdon »

PHP is a server pre-processor that can perform actions at the server, such as interaction with a database, AND is used to send data (HTML, for example) to the browser with echo and print commands. Some PHP scripts don't output anything to the browser. Some PHP scripts output everything to the browser. Many PHP scripts are a mixture of server actions and sending HTML to the browser. It all depends on what you're trying to do. PHP is very flexible. To help you get the concept, concentrate on this flow: when the web server is requested to serve a file with a .php file extension, it first parses it for any PHP sections (between opening <?php and a closing ?> tags) and if it finds such sections, it interprets the PHP code. When it is finished, it sends the results to the browser, just as if it had all been an HTML file. There will be no PHP code in the file when it reaches the browser. Probably the simplest example is a file, let's say it's named test.php:

Code: Select all

<html>
<head><title>TEST</title></head>
<body>
<?php
echo "This is being sent by PHP.<br />";
echo date('M d, Y');
echo "<br />";
?>
Now this is being sent directly as HTML, without any help from PHP.
</body>
</html>
What you should see is:

Code: Select all

This is being sent by PHP.
Nov 13, 2009
Now this is being sent directly as HTML, without any help from PHP.
Anything outside the <?php ... ?> tags is treated just like any HTML file, including Javascript, etc. But any part within such tags is parsed, so in this example it sends the first line to the browser, gets the current date at the server and formats it before sending it, then sends another line break, and finally, the rest of the HTML.

If you are bothered by why you would want to do this, the answer is that instead of just delivering a static document to the browser, PHP can do things as simple as showing the current date, or as complex as database queries and other dynamic operations, even creating web pages that only exist when the PHP script creates them.

I hope that helps you get started.
nick2price
Forum Newbie
Posts: 18
Joined: Fri Nov 13, 2009 5:49 pm

Re: Trouble understanding php

Post by nick2price »

Thats some great advise, thanks. Understand it a lot better now, although I do think there could be some better explained tutorials out there.

So in the case of my original post, where the tutorial tells me to create main_login.php, I can really just add this code directly to my html file. There so called main_login.php contains no php code, just standard html. And please correct me if I am wrong, but the important part of that file is <form name=”form1″ method=”post” action=”checklogin.php”>. When the submit button is pressed, it will call checklogin.php, sending it the required data? How does it know to make a call to checklogin.php when the button is pressed? Is "post" an event created by the button, similar to how a button in java creates an Action Event?

Once again, sorry for all the questions, just getting quite interested in this.

cheers
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Trouble understanding php

Post by califdon »

I hesitate to make such a broad statement as "you can just add the code directly to your HTML file," because everything depends upon the sequence of elements in the file, but in a general sense, yes. If there is no PHP code in it, it's optional whether you give the file a .php or .html filename extension. I typically use the .php extension only for files that do contain PHP code. Others perhaps use the .php extension just in case they later decide to add some PHP code to the file, and don't want to have to change the filename (which might be referenced in other scripts, e.g.).

It seems that you are just adding a web form to your first script. Yes, this is just HTML:

Code: Select all

<form name='something' method='post' action='myfile.php'>Enter something here: <input type='text' name='field1'><br />Something else here: <input type='text' name='field2'><br /><input type='submit' value='Click Here to Submit'></form>
The Method can be either 'post' or 'get', but is most commonly 'post'. The difference is in how the form data is sent to the action script. With 'get', the data is sent as a query string attached to the end of the URL. You've seen this countless times: example.com/someactionscript.php?id=1234&pg=3&dt=2009-11-14. That's a query string with 3 key-value pairs (id=1234, pg=3, and dt=2009-11-14). That means that the data is visible in the address box of the browser, which is often undesirable. With 'post', the data is sent as a hidden array of key-value pairs. PHP can retrieve the array contents with either Method. See http://www.tizag.com/phpT/postget.php.

The "key" values come from the "name" attribute of each <input> or <textarea> element of the form.

And yes, clicking on a "submit" type <input> element automatically sends a request to the "action" URL and sends the form data in either the 'post' or 'get' method. So the "action" URL must be a script that retrieves the data for processing. It is also possible to trigger the submission with the submit() function of the form, using Javascript.
Post Reply