Forum Topic: Php: Basic Php Forms.

(1,367 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

Rellizate

Reply To Post Reply & Quote

Posted at: 10/4/06 01:04 PM

Rellizate EVIL LEVEL 08

Sign-Up: 02/27/06

Posts: 481

PHP Main

Hello.
In this tutorial, I will be showing you how to use forms with PHP, and saying some erros that you might make with basic forms.

There are 2 ways in which to use forms. The first (more common), is to have 2 separate files. This tutorial will be using that method, but I will probably do a tutorial on using only one file.
Right, enough chat. Let's start coding!

Ok first, let us start with a very simple textbox form. Here is the code:

<form name="form1" method="post" action="recievefile.php">
<input type="text" name="username" />
<input type="submit" name="submit" value="Done!">
</form>

Right, so let's take a closer look at this code.

The first line is the following.
<form name="form1" method="post" action="recievefile.php">

The name of the form is what you need to get data sent from this form. If you have no name, or an invalid one (contains spaces e.t.c), your form will not work correctly.
The method can be either "post" or "get". Get is very different from post, but this tutorial is based on the "post" method.
And finally, the action. The action is basically the file to go to after the user has clicked the submit button.

The second line is this:

<input type="text" name="username" />

This is quite simple. The type is the type of field, or box this is, and the name is an "ID tag", which you need to grab data from this field.

The third line is:

<input type="submit" name="submit" value="Done!">

This is just the submit button.
The value is what appears on the button, and you know what the name is.
In this case, the type is "submit" which tells the web browser that this field is a submit button.

And finally, the last line is this:

</form>

This just stops the form tag.

Right, so save the following code as form.html:

<form name="form1" method="post" action="recievefile.php">
<input type="text" name="text1" />
<input type="submit" name="submit" value="Done!">
</form>

Now for recievefile.php:

<?php
$username = $_GET<'username'>;
echo ("Hello, $username. <br /> Have a nice day!");
?>

The first line is the following:

<?php

This just tells everyone that the following code is PHP.

The second line is this:

$username = $_GET<'username'>;

This sets the $username variable to the field with the name of "username";

And the last 2 lines are this:

echo ("Hello, $username. <br /> Have a nice day!");
?>

This just outputs "Hello, {--WHATEVER-YOU-TYPED-INTO-THE-TEXT-BOX-EA
RIER--}. Have a nice day!"; and then ends the PHP code.

That is pretty much the basics of forms. There are many different field types, including textareas and checkboxes, but I will not go into them now.

Thanks a lot, hope you enjoyed the tutorial.

If you have any questions feel free to contact me:
AIM: Rellizate
MSN: meltdown10@hotmail.co.uk


None

citricsquid

Reply To Post Reply & Quote

Posted at: 10/4/06 01:52 PM

citricsquid DARK LEVEL 23

Sign-Up: 06/25/05

Posts: 15,859

Wow, for the first time i actually know everything covered in thsi tutorial, sorry to say it, but this is actually all covered in spammy's get and post superglobals tutorial.


None

authorblues

Reply To Post Reply & Quote

Posted at: 10/4/06 01:58 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,363

At 10/4/06 01:04 PM, Rellizate wrote: $username = $_GET<'username'>;

are you sure you dont mean $_GET['username'];
i mean, unless theres something that no one is telling me

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Rellizate

Reply To Post Reply & Quote

Posted at: 10/4/06 02:36 PM

Rellizate EVIL LEVEL 08

Sign-Up: 02/27/06

Posts: 481

are you sure you dont mean $_GET['username'];
i mean, unless theres something that no one is telling me

damn it. That is because I posted it on TheSmurk (a site that allows BBCode), and I was using dreamweavers find and replace for changing the [s to <s.


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 10/4/06 05:13 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

By the way, the name attribute is not needed for a form if you aren't going to be manipulating it in any way with client-side code. Even then, it is not needed because you could access the forms[] array in javascript.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 10/4/06 06:52 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

When I say that, I mean the name attribute is not needed for the <form> tag.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

DarkMana

Reply To Post Reply & Quote

Posted at: 10/4/06 08:24 PM

DarkMana DARK LEVEL 23

Sign-Up: 04/04/05

Posts: 2,720

By the way, your form method is POST, but when you used the $_GET superglobal to access it.

Asus P5Q PRO, Intel E8400 @ 3.60 GHz, 4GB DDR2-1000, ATI HD4850

BBS Signature

None

DannyIsOnFire

Reply To Post Reply & Quote

Posted at: 10/5/06 12:01 PM

DannyIsOnFire DARK LEVEL 19

Sign-Up: 04/14/05

Posts: 7,191

At 10/4/06 06:52 PM, Kurt wrote: When I say that, I mean the name attribute is not needed for the <form> tag.

Dont you need the name tag for the form tag to be W3 valid ?
Or maybe not ? :P


All times are Eastern Standard Time (GMT -5) | Current Time: 08:56 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!