Ad:
 
  • PHP Discussion Thread
  • Unsolved
  • Views: 527
  • PHP RSS
Similar Threads
Apr 30th, 2010
0

PHP form validation

Expand Post »
Hey this is a code to print the times table of a particular number entered by a user, The thing is I want to validate the form so that it checks to see if what is entered is numeric, and ranges from 1 to 12 if not I want it to display an error.
PHP Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8. <body>
  9. <form method="post" "<?php echo $PHP_SELF;?>">
  10. Enter Number:<input type="text" name="Number" />
  11.  
  12. <input type="submit" value="Show Time table"/>
  13.  
  14. </form>
  15.  
  16. </body>
  17. </html>
  18. <?php
  19. if (array_key_exists('Number',$_POST))
  20. {
  21. $numbers=range(1,12);
  22. $Number = $_POST['Number'];
  23. foreach($numbers as $times)
  24. {
  25. echo '<tr>';
  26.  
  27. $output = $Number * $times;
  28. echo "<tr style='text-align: center'>$Number x $times = $output<br>
  29. </tr>";
  30. echo '</tr>';
  31. echo '</tr>';
  32. }
  33. }
  34. ?>
KBL
Reputation Points: 10
Solved Threads: 0
Newbie Poster
KBL is offline Offline
19 posts
since Mar 2010
Apr 30th, 2010
0

Re: PHP form validation

PHP Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8. <body>
  9. <form method="post" action="<?php echo $PHP_SELF;?>">
  10. Enter Number:<input type="text" name="Number" />
  11.  
  12. <input type="submit" value="Show Time table"/>
  13.  
  14. </form>
  15.  
  16. </body>
  17. </html>
  18. <?php
  19. $numbers=range(1,12);
  20. $Number = $_POST['Number'];
  21. if ($Number < 1 or $Number > 12) {
  22. // insert code to display an error
  23. }
  24. else {
  25. foreach($numbers as $times) {
  26. echo '<tr>';
  27.  
  28. $output = $Number * $times;
  29. echo "<tr style='text-align: center'>$Number x $times = $output<br>
  30. </tr>";
  31. echo '</tr>';
  32. echo '</tr>';
  33. }
  34. }
  35. ?>
This should do it. A form also needs an action. As you can see, there is no need to test for numeric input. If you enter anything else no output is generated.
Reputation Points: 33
Solved Threads: 51
Posting Whiz
colweb is offline Offline
316 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Sortable table
Next Thread in PHP Forum Timeline: combining tables in PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Build Custom RSS Feed


Follow us on Twitter


© 2010 DaniWeb® LLC