1 / 8

PHP : Validating forms and loops

PHP : Validating forms and loops. Some things for helping you to validate input. The isset function can be used to test if a php variable is passed. If the variable $_POST['firstName'] contains a value then isset($_POST['firstName']) would evaluate to true.

yael-perry
Download Presentation

PHP : Validating forms and loops

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. PHP : Validating forms and loops

  2. Some things for helping you to validate input • The isset function can be used to test if a php variable is passed. • If the variable $_POST['firstName'] contains a value then isset($_POST['firstName']) would evaluate to true. • The is_numeric function may be useful during input validation to determine if the user entered a legitimate number. • if you are expecting a number to be typed into an HTML form field named age then is_numeric($_POST['age'] would evaluate to true

  3. Some things for helping you to validate input • The empty function can be used to test if a php variable contains a value or not. • If the variable $_POST['firstName'] contains a value then empty($_POST['firstName']) would evaluate to false. • Other validation include • is_string($variableName) • is_float($variableName) • is_bool($variableName) • is_int($variableName)

  4. Loops: while and do-while $num = 1; while ($num < 10) {   echo "This loop is executed $num times<br />";   $num++; } $num = 1; do while {   echo "This loop is executed $num times <br />";   $num++; } ($num < 10)

  5. Loops: for Loops for ($num=0; $num < 10; $num++) {   echo "This loop is executed $num times<br />" } You can terminate explicitly with a break statement. $desired = 10; for ($num=0; $num<100; $i++) { echo "The loop is on iteration: $num <br />" if ($num == $desired) { echo "Found $desired"; break; // if you reach this part, then the loop is broken } }

  6. pex4.html, pex4.php • Redo ie. loan calculator so that

  7. Formula for amortization table Once you calculate the monthly payment, M, which stays the same. • The interest payment, IP = O * I where O is the outstanding loan & I is the monthlyinterest rate (b) Principle Paid For, P, is the amount of loan that went to principle rather than interest: P = M – IP (c) The new outstanding loan, O will now be O = O – P ie. Previous Outstanding balance – Principle Paid For Repeat the above steps (a) to (c) – you need LOOP here to determine all periods.

  8. Formatting and toggling • PHP allows up to round off numbers e.g. $num =2.467; $num = round($num, 2); // $num should now displayed as 2.47 • Next: How to do alternate colors? There is a concept call modulus or sometimes call quotient. i.e. ($num % 2) means take $num and divided by 2 and tell me the remainder. So the remainder would be 0 or 1. If it is 0 you can do one thing like print the current row in blue and if it is 1 you can print the row in gold. How to do that in php? • echo "<TR bgcolor=blue>"; or • echo "<TR bgcolor=yellow>";

More Related