1 / 23

Forms

Forms. The Objective :. Learn how to build forms, and how to capture the data from a form with a PHP program. What is a form ?. It’s a special HTML page that Contains one or more fields to capture data. The most common kinds of fields are: Text input boxes Checkboxes

naif
Télécharger la présentation

Forms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Forms

  2. The Objective: Learn how to build forms, and how to capture the data from a form with a PHP program

  3. What is a form? • It’s a special HTML page that • Contains one or more fields to capture data. The most common kinds of fields are: Text input boxes Checkboxes Radio Buttons SUBMIT Buttons Continue

  4. Form code: HTML <form method=“get” action=“http://me.com/capture.php” > the ‘form’ field tags go here ... </form>

  5. What’s a GET? What’s A POST? There are two ways for data to flow from the form to the script: ‘post’ is invisible. ‘get’ is visible in the submitted string. <form method=“get” action=“http://me.com/capture.php” > <input type="text" name=“namefield" />Name <input type="submit" name="controlnextstep" value="Continue" /> </form> Name Continue

  6. What’s a POST? A GET? If you submit a ‘get’ the URL will be http://me.com/capture.php?namefield=Mike <form method=“get” action=“http://me.com/capture.php” > <input type="text" name=“namefield" />Name <input type="submit" name="controlnextstep" value="Continue" /> </form> Name Continue

  7. What’s a POST? A GET? If you submit a ‘post’ the URL will be http://me.com/capture.php <form method=“post” action=“http://me.com/capture.php” > <input type="text" name=“namefield" />Name <input type="submit" name="controlnextstep" value="Continue" /> </form> Name The data travels ‘incognito’ Continue

  8. How do you catch the data? form1.html <form method=“post” action= “http://localhost/lecture6/capture1.php” > <input type="text" name=“namefield" />Name <input type="submit" name="controlnextstep" value="Continue" /> </form> capture1.php <?php $name_in=$_POST[‘namefield’]; print “You sent in the name $name_in”; ?> $_POST (or $_GET) are built-in global arrays.

  9. Example 2: Check Boxes form2.html <form method="post“ action= "http://localhost/lecture6/capture2.php"> <input type='checkbox' name='check1' value=1 /> Box One <input type="submit" name="controlnextstep" value="Continue" /> </form> capture2.php <?php $checkval=$_POST['check1']; print "You sent in the checkbox value :$checkval:."; ?>

  10. Analyzing the Data Flow Client (Browser) Server (Apache) url:form2.html Send back form2.html Fill in The form Run the script capture2.php send back HTML: "You sent in the checkbox value ...." Render HTML, See the Results. End of story url:capture2.php

  11. Example 3: Combine Form & Capture Loop3.php <form method="post“ action=“http://localhost/loop3.php"> <input type='checkbox' name='check1' value=1 /> Box One <input type="submit" name="controlnextstep" value="Continue" /> </form> <?php $checkvalue=$_POST['check1']; print "You sent in the checkbox value :$checkvalue:."; ?> This works … but check disappears every time

  12. Setting the Checkbox Here's the syntax for FORCING a checkmark <input type='checkbox' name='check1' value=1 checked /> Box One The value is what will be returned to $_POST[‘check1’] when the form is submitted. If you can place the word ‘checked’ here, the box will be displayed with a check in it. SO – our PHP should insert ‘checked’ when 1 comes in.

  13. Example 4: Setting the Checkbox Loop4.php <?php $checkvalue=$_POST['check1']; print "You sent in the checkbox value $checkvalue."; if ($checkvalue){ $checkmark=‘checked’; } ?> <form method="post" action="loop4.php"> <input type='checkbox' name='check1' value=1 <?php print $checkmark; ?> /> Box One <input type="submit" name="controlnextstep" value="Continue" /> </form> This works. View Source Code to SEE it working.

  14. Example 4a: PHP or HTML? Loop4a.php Putting everything inside PHP <?php $checkvalue=$_POST['check1']; print "You sent in the checkbox value $checkvalue."; if ($checkvalue) { $checkmark=‘checked’; } print ‘<form method="post" action="loop4a.php">’; print “<input type='checkbox' name='check1' value=1 $checkmark /> Box One"; print ‘<input type="submit" name="controlnextstep" value="Continue" /> </form>’; ?>

  15. Example 4a: PHP or HTML? Loop4a.php Sometimes put the ' on the outside and the " on the inside. print '<form method="post" action="loop4a.php">'; print "<input type='checkbox' name='check1' value=1 $checkmark /> Box One print '<input type="submit" name="controlnextstep" value="Continue" /> </form>'; ?>

  16. Example 4a: PHP or HTML? Loop4a.php Sometimes put the ' on the outside and the " on the inside. Sometimes put the " on the outside and the ' on the inside, To use included variables like $checkmark. print '<form method="post" action="loop4a.php">'; print "<input type='checkbox' name='check1' value=1 $checkmark /> Box One print '<input type="submit" name="controlnextstep" value="Continue" /> </form>'; ?>

  17. Example 4b: Using a Hidden Input Loop4b.php First or Second Pass? <?php $checkvalue=$_POST['check1']; $isrunning = $_POST['running']; // first pass: this has no value. if ($isrunning) { print "You sent in the checkbox value $checkvalue.";} if ($checkval) { $checkmark=‘checked’; } print ‘<form method="post" action="loop4a.php">’; print “<input type='checkbox' name='check1' value=1 $checkmark /> Box One"; print ‘<input type="submit" name="controlnextstep“ value="Continue" /> <input type="hidden" name="running" value=1 /> </form>’; ?>

  18. Analyzing the Data Flow Client (Browser) Server (Apache) url:loop4b.php Run loop4b: First pass?Don't send feedback. Send empty form Fill in The form See the Results, Send New Input url:loop4b.php Running (pass 2)? Send feedback. Send updated form url:loop4b.php Continue …

  19. Example 5: Two Inputs Loop5.php How much is that Lollipop? <?php # PART 1: Input Processor $sale1=$_POST['sale1']; if ($sale1) $checkmark1='checked'; $seniorcitizen=$_POST['seniorcitizen']; if ($seniorcitizen) $checkmark2='checked'; if ($sale1) { if ($seniorcitizen) { print 'Your lollipop will cost 5 cents.<br />'; } else { print 'Your lollipop will cost 10 cents. <br />';} } else { print 'You did not buy any lollipops. <br />'; }

  20. Example 5: Two Inputs # Part 2: Producing the Form: print " <form type='POST' action='http://localhost/loop5.php'> <input type='checkbox' name='sale1' value=1 $checkmark1 /> Luscious Lollipop = 10 cents (5 cents for Senior Citizens.) <br /> <input type='checkbox' name='seniorcitizen' value=1 $checkmark2 /> Check here if you are a Senior Citizen <br /> <input type='submit' name='controlnextstep' value='Continue' /> "; ?>

  21. Challenge 6: Numeric Input Loop6.php Selling lots of pops Modify the loop5 program below into loop6, How many lollipops do you want? 10 cents each (5 cents for senior citizens.) Check here if you are over 60 years old. Program prints "Your lollipops will cost 50 cents." Continue

  22. Challenge 6: Numeric Input Loop6.php Selling lots of pops Modify the loop5 program below into loop6, How many lollipops do you want? 10 cents each (5 cents for senior citizens.) Check here if you are over 60 years old. Program prints "Your lollipops will cost 50 cents." Continue Hint: <input type='text' size=2 name='sale1' value=$sale1 />

  23. Challenge 7: Graphic Output Loop7.php graphical pops Create a function drawpop($n) that draws however many pops you bought.

More Related