PHP MySQL Tutorial
Learn PHP and MySQL

PHP Forms

66% of people found this useful
PHP Forms

Using forms in a web based application is very common. Most forms are used to gather information like in a signup form, survey / polling, guestbook, etc.

A form can have the method set as post or get. When using a form with method="post" you can use $_POST to access the form values. And when the form is using method="get" you can use $_GET to access the values. The $_REQUEST superglobal can be used to to access form values with method="post" and method="get" but it is recommended to use $_POST or $_GET instead so you will know from what method did the values come from.

Here is an example of HTML form :

 

 

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name : <input name="username" type="text"><br>
Password : <input name="password" type="password"><br>
<input name="send" type="submit" value="Send!">
</form>

The form above use $_SERVER['PHP_SELF'] as the action value. It is not required for the form to perform correctly but it's considered good programming practice to use it.

Below is the PHP code used to access form values :
 

<?php
if(isset($_POST['send']))
{
   echo "Accessing username using POST : " .         $_POST['username'] . "<br>";
   echo "Accessing username using REQUEST : " .         $_REQUEST['username'] . "<br>";

   $password = $_POST['password'];
   echo "Password is $password";
}
?>

The if statement is used to check if the send variable is set. If it is set then the form must have been submitted. The script then print the value of username using $_POST and $_REQUEST

 

Using Array As Form Values

Take a look at the code example below. The form have five input with the same name, language[]. Using the same input name is common for checkboxes or radio buttons.

 

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Select the programming languages you can use<br>
<input name="language[]" type="checkbox" value="C++">
C++<br>
<input name="language[]" type="checkbox" value="Java">
Java<br>
<input name="language[]" type="checkbox" value="PHP">
PHP<br>
<input name="language[]" type="checkbox" value="ASP">
ASP<br>
<input name="language[]" type="checkbox" value="Delphi">
Delphi<br>
<input name="send" type="submit" id="send" value="Send!">
</form>

The PHP code below print the value of language after the form is submitted. Go ahead and try the example. Try checking and unchecking the options to see the effect.

Example : form-array.php
Source code : form-array.phps

<?php
if(isset($_POST['language']))
{
   $language = $_POST['language'];
   $n        = count($language);
   $i        = 0;

   echo "The languages you selected are \r\n" .
        "<ol>";
   while ($i < $n)
   {
      echo "<li>{$language[$i]}</li> \r\n";
      $i++;
   }
   echo "</ol>";
}
?>

From the above code you will notice that $language is an array. This is because in the form code language is repeated several times. When you specify the same input name in a form, PHP will treat it as an array.

There is one important issue you should know when using forms, that is form validation. The code above does not check whether the input is correct such as checking if the user is entering any value into the textbox or is the user selecting any checkboxes. This is actually a bad practice because you should never put a web form without any validation. To learn about validating form input you can go to this page : Form Validation With PHP

Recent Comments

By: john9 Posted on 03-19-2015 7:58 AM
By: zuzuce123 Posted on 08-22-2015 12:33 AM

chanel bags,www.chanelhandbags.net.in

softball bats,www.cheap-baseballbats.net

nfl jerseys,www.cheapjerseys.us.org

nfl jerseys,www.cheap-jerseys.us.org

cheap jordan shoes,http://www.cheap-jordans.net/

michael kors,www.cheapmichaelkors.us.org

nike shoes,www.cheap-nike-shoes.net

oakley sunglasses,http://www.cheapoakley.us.org/

oakley sunglasses,www.cheapoakleysunglasses.ar.com

oakley sunglasses,www.cheap-oakleysunglasses.in.net

ray ban sunglasses,http://www.cheaprayban.com.co/

discount shoes,http://www.cheapshoes.com.co/

uggs outlet,www.cheap-uggboots.in.net

dre beats,www.beatsbydrdre.co.com

beats audio,http://www.beatsbydre.com.co/

beats audio,www.beats-by-dre.com.co

beats audio,www.beats-headphone.in.net

bebe clothing,http://www.bebeclothing.net/

bottega veneta,http://www.bottega.us/

burberry,http://www.burberrycanada.ca/

burberry outlet,www.burberry-handbags.us.org

burberry outlet online,www.burberryoutlet.club

burberry outlet online,www.burberryoutlet.me.uk

burberry outlet,www.burberryoutletonline.ar.com

burberry outlet online,www.burberrys-outlet.in.net

burberry outlet,www.burberrys-outletonline.in.net

calvin klein underwear,http://www.calvinklein.co.com/

celine bags,http://www.celinebags.org/

chanel sunglasses,http://www.chanel-bags.com.co/

polo ralph lauren,www.polo-outlets.com.co

ralph lauren outlet,www.poloralphlaurenoutlet.net.co

prada outlet,www.pradahandbags.com.co

prada outlet,www.pradahandbags.net.co

prada handbags,http://www.pradaoutlet.com.co/

prada outlet,http://www.pradashoes.com.co/

ralph lauren,http://www.ralph-lauren.ca/

ralph lauren,www.ralph-lauren.com.au

ralph lauren uk,www.ralph-lauren.org.uk

ralph lauren outlet,www.ralphlaurenepolo.com

burberry,http://burberry.outletnow.org/

christian louboutin outlet,christianlouboutin.outlet-shoes.net

tory burch sale,toryburch.salesandals.net

abercrombie,www.abercrombie-and-fitch.ca

abercrombie and fitch,www.abercrombieand-fitch.com.co

abercrombie fitch,www.abercrombie-and-fitch.us.com

abercrombie and fitch,www.abercrombiefitch.us.com

abercrombie,www.abercrombie-kids.us.com

adidas,http://www.adidas--canada.ca/

nike huarache,http://www.air-huarache.co.uk/

retro jordans,http://www.airjordans.us/

jordan release dates 2015,www.airjordanshoes2015.com

air max 2014,http://www.airmax-2015.org/

air max,http://www.airmax-90.org/

babyliss pro,http://www.babyliss.us.org/

barbour jackets sale,www.barbour-factory.com

barbour mens jackets,www.barbour-jacketsoutlet.com

basketball shoes,www.basketballshoes.com.co

bcbg max,http://www.bcbgdresses.net/

bcbg max,http://www.bcbg-max-azria.ca/

flat iron,http://www.chiflatiron.net.co/

louboutin,www.christian--louboutin.in.net

louboutin shoes,www.christianlouboutin.org.uk

louboutin outlet,www.christianlouboutinoutlet.net.co

christian louboutin,www.christianlouboutinshoes.ar.com

christian louboutin,www.christian-louboutin-shoes.ca

louboutin shoes,www.christianlouboutinshoes.jp.net

coach factory outlet online,www.coachfactory-outlet.co.com

coach factory outlet,www.coachfactory-outlet-online.in.net

coach purses,www.coach-handbags.com.co

coach outlet,www.coachoutlet-online.com.co

coach outlet,www.coachoutletstore.net.co

coach outlet,www.coachoutletstore-online.com.co

coco chanel,www.coco-chanelbags.com.co

converse shoes,http://www.converse.net.co/

converse,www.converses-shoes.net

handbags outlet,www.designerhandbagsoutlet.com.co

ed hardy,http://www.edhardy.us.org/

glasses online,www.eyeglassesonline.us.org

glasses frames,www.eyeglassframes.us.org

boutique clothing,www.fashion-clothing.us.org

salvatore ferragamo,http://www.ferragamo.com.co/

ferragamo,http://www.ferragamoshoes.net/

straightener,www.ghdhairstraightener.com.co

giuseppe zanotti,www.giuseppezanotti.com.co

gucci handbags,www.guccihandbags.net.co

gucci outlet,http://www.gucci-outlet.biz/

gucci sale,www.gucci-outletsale.in.net

gucci outlet,http://www.guccishoes.com.co/

gucci outlet,http://www.guccishoes.in.net/

gucci outlet,http://www.guccishoes.us.org/

gucci,http://www.gucci-uk.me.uk/

purses and handbags,www.handbagsoutlet.com.co

harrods,www.harrods-london.co.uk

katespade,http://www.kate-spade.net.co/

kate spade,www.katespade-outlet.net.co

levi's jeans,http://www.levisjeans.com.co/

longchamp outlet,http://www.longchamp.com.co/

longchamp handbags,http://www.longchamp.us.org/

longchamp,www.longchamp-handbags.us.com

longchamp,www.longchampoutlet.com.co

christian louboutin,http://www.louboutin.jp.net/

louis vuitton,www.louisvuitton.jp.net

louis vuitton,www.louis--vuitton.org.uk

louis vuitton outlet,http://www.louisvuitton.so/

louis vuitton outlet,http://www.louisvuittonas.com/

louis vuitton,www.louis-vuitton-australia.com.au

louis vuitton outlet,www.louis-vuittonblackfriday.com

louis vuitton,www.louisvuitton-outlet.com.co

louis vuitton,www.louis-vuittonoutletcanada.ca

louis vuitton outlet,www.louisvuitton-outlets.us

louis vuitton outlet,www.louisvuittonsas.com

lululemon,www.lululemon-australia.com.au

lululemon canada,http://www.lululemoncanada.ca/

lululemon,www.lululemonoutlet.com.co

m a c cosmetics,www.maccosmetics.net.co

marc jacobs handbags,www.marcjacobsonsale.com

mcm handbags,http://www.mcm-bags.in.net/

mcm bags,http://www.mcmhandbags.com.co/

michael kors outlet,www.michael-kors.com.co

michael kors outlet,www.michael-kors.net.co

michael kors outlet online,http://www.michaelkors.so/

michael kors outlet online,www.michael--kors.us.com

michael kors,www.michael-kors-australia.com.au

michael kors purses,www.michaelkorsbags.us.org

michael kors canada,www.michael-kors-canada-outlet.ca

michael kors,www.michaelkorshandbags.org.uk

michael kors outlet,www.michael-kors-handbags.us.com

michael kors outlet online,www.michaelkorsoutlet.ar.com

michael kors outlet online,www.michael-kors-outlet.us.org

michael kors outlet,www.michaelkorsoutlet-online.ar.com

hermes belt,http://www.hermesbags.club/

hermes,http://www.hermesbirkin.biz/

hermes birkin,http://www.hermesoutlet.top/

tommy hilfiger,www.hilfigeroutlet.in.net

hollister,http://www.hollister.us.org/

hollister,http://www.hollistercanada.ca/

hollister clothing store,www.hollisterclothing.in.net

hollister clothing,www.hollister-clothingstore.in.net

insanity workout calendar,www.insanityworkout.net.co

instyler ionic styler,http://www.instylers.us.com/

iphone 6 cases,www.iphone-cases.com.co

jimmy choo shoes,http://www.jimmychoo.net.co/

jordan release dates 2015,www.jordanrelease-dates.net

retro jordans,http://www.jordanretro.org/

jordan shoes,www.jordan-shoes.com.co

juicy couture outlet,www.juicycouture.com.co

juicy couture,www.juicycoutureoutlet.net.co

katespade,http://www.kate-spade.in.net/

michael kors outlet,www.michaelkors-outlet-online.com.co

michael kors outlet,www.michael-kors-outlet-online.us.org

michael kors outlet online,www.michaelkorsoutletonline-sale.us.com

michael kors,www.michaelkors-uk.org.uk

mizuno,http://www.mizunorunning.net/

cheap moncler jackets,www.mmoncler-outlet.com

ugg boots,www.ugg-australia.in.net

uggs outlet,http://www.uggboots.net.co/

uggs outlet,http://www.ugg-boots.us.org/

ugg boots,www.ugg-boots-australia.com.au

ugg boots,www.uggbootsclearance.com.co

uggs,http://www.uggscanada.ca/

uggs,http://www.uggsonsale.com.co/

ugg boots,http://www.uggsoutlet.com.co/

coach outlet store,www.uptocoachoutlet.com

vans store,http://www.vans-shoes.net/

prom dresses,www.wedding--dresses.ca

wedding dresses uk,www.weddingdressesuk.org.uk

woolrich womens jakets,www.woolrich-clearance.com

lululemon,http://www.yoga-pants.ca/

lululemon,http://www.yogapants.com.co/

coach bags outlet,http://www.zxcoachoutlet.com/

moncler jackets sale,www.moncler-outlet.us.org

mont blanc pens,www.montblanc--pens.in.net

new balance,http://www.new-balance.ca/

new balance shoes,www.newbalance-outlet.org

new balance outlet,www.newbalance-shoes.org

coach purses factory,www.newoutletonlinemall.com

cheap jerseys,http://www.nfl-jersey.us.com/

air max,http://www.nike-air-max.ca/

air max,www.nike-air-max.com.au

nike air max 2014,http://www.nike-air-max.us/

nike factory outlet,http://www.nikefactory.org/

nike free trainer 5.0,http://www.nikefree5.net/

pandora jewelry,www.pandorajewellery.com.au

pandora charms,http://www.pandorajewelry.top/

ralph lauren uk,www.ralph-lauren-outlet.org.uk

ralph lauren outlet online,www.ralphlaurenoutlet.us.com

ralph lauren outlet,www.ralphlaurenoutletonline.com.co

ralph lauren polo outlet,www.ralphlaurenoutletonline.in.net

polo ralph lauren,www.ralphlaurenoutletonline.us.org

polo ralph lauren outlet,www.ralphlaurenpolos.in.net

ray ban,http://www.ray--ban.ca/

ray ban sunglasses,www.ray-ban-outlet.us.com

raybans,http://www.raybans.us.org/

ray ban,www.rayban-sunglasses.org.uk

ray ban outlet,www.rayban-sunglasses.us.org

ray ban sunglasses,www.ray-ban-sunglasses-outlet.in.net

ray ban sunglasses,www.raybanwayfarer.in.net

replica handbags,www.replicahandbags.com.co

rolex watches,www.replica-watches.us.com

air jordan retro,http://www.retro-jordans.com/

replica watches,www.rolex-watches.me.uk

replica watches,www.rolex-watches.us.com

rolex watches,www.rolex-watches-canada.ca

replica watches,www.rolexwatchesforsale.us.com

nike roshe,http://www.rosherun.org.uk/

nike roshe,http://www.rosheruns.us/

nike free,http://www.nikefree-run.net/

free run,www.nikefree-run.org.uk

mercurial superfly,www.nikemercurial.in.net

roshe run,http://www.nikerosherun.us/

nike shoes,www.nike-shoes-canada.ca

nike outlet,http://www.nikestore.us/

the north face,http://www.northface.us.org/

the north face outlet,www.northfaceoutlet.com.co

oakley sunglasses,www.oakleyoutlet.us.org

oakley,www.oakley--sunglasses.com.au

cheap oakley sunglasses,www.oakleysunglasses.gr.com

oakley prescription,www.oakley-sunglasses.top

oakley canada,www.oakley-sunglasses-canada.ca

oakley sunglasses,www.oakleysunglassescheap.in.net

rolex watches,www.omegawatches.us.com

p90x3,http://www.p90xworkout.in.net/

pandora bracelet,www.pandora-charms.org.uk

pandora jewelry,www.pandora-charms-canada.ca

ferragamo,www.salvatoreferragamo.in.net

soccer shoes,http://www.soccer-shoes.org/

baseball bats,http://www.softball-bats.us/

supra shoes,http://www.suprashoes.in.net/

swarovski,www.swarovski-australia.com.au

swarovski canada,http://www.swarovskicanada.ca/

swarovski jewelry,www.swarovskicrystal.com.co

swarovski crystal,www.swarovskijewelry.com.co

swarovski uk,www.swarovski-uk.org.uk

north face,http://www.the-north-face.ca/

north face,www.the-northface.com.co

north face,www.thenorth-face.org.uk

north face,www.thenorthface.us.org

north face,www.the-northface.us.org

north face jackets,www.thenorthfacejackets.in.net

thomas sabo uk,http://www.thomas-sabo.me.uk/

tiffany and co,www.tiffany-andco.com.au

tiffany jewelry,www.tiffanyandco.net.co

tiffany and co,www.tiffanyandco-canada.ca

tiffany and co,www.tiffanyjewelry.us.org

timberland outlet,www.timberlandboots-outlet.in.net

timberland outlet,www.timberlandshoes.com.co

tommy hilfiger outlet,www.tommyhilfiger.net.co

tommy hilfiger,www.tommy-hilfiger-canada.ca

tommy hilfiger,www.tommy-hilfigeroutlet.com

toms shoes,http://www.toms-outlet.net.co/

toms shoes,www.toms-outletsale.in.net

toms outlet,http://www.toms-shoes.com.co/

toms shoes,www.toms-shoes-outlet.net

tory burch sandals,www.tory-burch-outlet.in.net

tory burch sandals,www.tory-burchoutlet.us.com

tory burch sandals,www.toryburchsale.com.co

true religion outlet,www.true-religion.com.co

cheap true religion,www.truereligionjeans.net.co

cheap true religion,www.truereligion-outlet.com.co

true religion jeans outlet,www.truereligion-outlet.us.org

Powered by Community Server (Non-Commercial Edition), by Telligent Systems