Recursive programming in php

This is a discussion on "Recursive programming in php" within the PHP Forum section. This forum, and the thread "Recursive programming in php are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 29th, 2007, 21:44
New Member
Join Date: Sep 2007
Location: San Diego, CA
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Recursive programming in php

I'm very very very new to PHP.
I have a PHP doc.
What I cannot do:
There is a form inside the php file.
When user enteres values into the form I want php to analyze the values and if there is a problem to right next to the form field what the problem is. But instead of it before the values are entered into the form the message comes up. How to make the message to analyze the data after the form is processed.
There are many more problems with the document but this is the most disturbing one.
Thank you guys.
This is the code:
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++)
           {
            
$counter=$i;
            
$item_name='items'.$counter;
            
$item_value $_POST[$item_name];
            
            
$amount_number='amount'.$counter;
            
$amount_value $_POST[$amount_number];

           echo 
"<tr><td><input type=text name =$item_name value=$item_value></td>";
              
                   
            echo
" <td><input type=text name =$amount_number value= $amount_value></td>";

              
           

</
table>
   <
input type="submit" name ="submit_button" value ="Submit" />
</
form>
$amount_number=$_POST[$amount_number];
           if(empty(
$amount_number)){ 
           echo
"<td>Please enter an amount</td>";/* I don't want this sentence to be here, but i don't know how to leave the space in between the brackets empty.*/
             
}
             
           if(!
is_numeric($amount_number)){

            
 
           echo
"<td> Amonut $amount_number is not a number</td>";/*How to make this statment to appear only when the info is already entered?*/
           
$counter++;
           echo
"Errors: $counter";}
   
          if(
$amount_number<99 && $amount_number>100001)/* This statemnt doesn't work at all!!!! */
{
          
                    echo
"<td> Amount $amount_number should be between 100 and 100000</td></tr>";
                    
$counter++;
                    echo
"Errors: $counter";
                  }
           
          
 
 
 
 
          }

      
?>
 
    </body>
</html>

Last edited by karinne; Oct 1st, 2007 at 19:16. Reason: Please use the vBcode [ php ] when inserting PHP code in your post.
Reply With Quote

  #2 (permalink)  
Old Sep 30th, 2007, 03:35
Junior Member
Join Date: Jun 2006
Location: Here
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Recursive programming in php

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.
Reply With Quote
  #3 (permalink)  
Old Sep 30th, 2007, 19:07
simonb's Avatar
Blog Moderator

Join Date: Dec 2006
Location: Norwich
Posts: 667
Blog Entries: 4
Thanks: 4
Thanked 2 Times in 2 Posts
Send a message via Skype™ to simonb
Re: Recursive programming in php

Can you put the code in the php bbc
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Reply With Quote
  #4 (permalink)  
Old Oct 1st, 2007, 11:51
Rakuli's Avatar
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.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #5 (permalink)  
Old Oct 1st, 2007, 12:37
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: Recursive programming in php

Quote:
Originally Posted by Rakuli View Post
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.
Or use both. The client-side check increases Usability and the server-side increases safety....
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Programming SuseLinux PHP Forum 6 Nov 4th, 2006 14:15
[XSLT]: Recursive Function to Add/Multiply ThyGizmo Other Programming Languages 0 Oct 9th, 2006 08:15
programming-designs.com programming-designs Free Web Site Critique 5 Aug 27th, 2006 19:31
ASP/SQL programming help squashed_Frog Classic ASP 3 May 15th, 2006 03:49
NewBie to Web Programming ruwanr Introduce Yourself 7 Dec 1st, 2005 20:32


All times are GMT. The time now is 07:18.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43