View Single Post
  #4 (permalink)  
Old Oct 1st, 2007, 11:51
Rakuli's Avatar
Rakuli Rakuli is offline
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Recursive programming in php

Your PHP is a little wacky (sorry )

There is no real procedure as the loop hasn't finished before you start checking things.

The script below is yours but I have rewritten it to do as you ask but also work -- it will check each condition and only display error messages if needed after the form is submitted.

PHP: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

</head>
<body>

<h1>My Bills</h1>
<form method="POST">
<table>
<tr>
<td>Item</td>
<td>Amount</td>
</tr>
<?php


for($i=1;$i<5;$i++)
{

$item_name        'items'.$counter;
$amount_number    ='amount'.$counter;



// These will hold the error messages if/when they occur
$item_error false;
$amt_error false;

// This will check to make sure the form has actually been submitted
// Allowing you to display error messages should they be required
if (!empty($_POST['submit_button']))
{
    
// Trim the white space that might trick PHP into thinking there is something when there isn't
    
$item_value     trim($_POST[$item_name]);
    
$amount_value   trim($_POST[$amount_number]);
    
    
// Now check that fields have been set
        
if (empty($item_value))
            
$item_error 'You much enter an item name!';
        
        if (empty(
$amount_value))
            
$amt_error 'You must enter an amount';
            
        
// This will check to make sure that the amount is a number
        
else if (!is_numeric($amount_value))
            
$amt_error 'Your amount must be a number';
        
        
// This will check that it is not less than 99 **OR** greater than 100000
        // In your script you were checking to see if it was less than 99 **AND** greater than 100000 (obviously a tough feat)
        
else if ($amount_value 99 || $amount_value >  100000)
            
$amt_error 'Your amount can\'t be less than 99 or greater than 100000';
            

}



// We will use a ternary to see if there is an error with each field

echo '<tr><td><input type="text" name="'$item_name'" value="',$item_value'" />',/* Is there an error? If yes, display it next to the input */ ($item_error '<br />' $item_error ''), '</td>';


echo 
' <td><input type="text" name="'$amount_number'" value="'$amount_value'">', ($amt_error '<br />' $amt_error ''), '</td>';

// Close the for loop and finish the HTML

?>
</table>
<input type="submit" name="submit_button" value ="Submit" />
</form>

</body>
</html>
Quote:
Originally Posted by split-visionz
My favorite way to validate forms is to actually add a javascript function to validate the form attributes when the submit button is clicked and use javascript to add error messages to the innerHtml of the <td> tags where you want. You would need to give each <td> a unique Id inorder to do it.

If you must do it in PHP use the ISSET() function to check whether a POST variable is set before you print out the error messages.
A server side checking option is really not debatable. Javascript is great to save a round-trip to the server but can be switched off. You can't switch PHP off so it is by far more secure than javascript.

Hope that helps, cheers.
Reply With Quote