Reply to Re: Help Required in PHP, For validating the form and displaying error on same page.

Your name:

Reply:


Posted by Balazs Wellisch on 12/31/05 09:19

One way to do this is to have a separate controller script that presents the
appropriate view based on the current state of your form.

For example, if index.php is your controller, when you invoke it the first
time, by default it will present a blank form. Your form action would point
back to index.php. Now when you press the submit button its name will be
available in the $_POST structure. You test for the existence of this field
using the isset() function. If it is there you execute the validation logic.
If validation succeeds you process the input. If it doesn't you present your
error messages and the form again with the values defaulted to what the user
submitted.

In index.php you'd have... (pseudocode)

if( isset($_POST['process'] )
{
$isValid = false;
include('validate.php');
if( $isValid )
include('process.php');
else
include('form.php');
}
else
include('form.php');

validate.php has...

if( validate fields according to your rules )
$isValid = true
else
$isValid = false

process.php has...

SQL to insert fields into db, or whatever you want to do with them

form.php has...

//default values
$field_1 = '';
$field_2 = '';
....
if( isset($_POST['field_1'])
$field_1 = $_POST['field_1'];
if( isset($_POST['field_2'])
$field_1 = $_POST['field_2'];
....

<form action="index.php" method="post">
<input type="text" name="field_1" value="<?php print $field_1; ?>">
<input type="text" name="field_2" value="<?php print $field_2; ?>">
...
<input type="submit" name="process" value="Process Form">
</form>

I cut some corners with the above example for brevity's sake but that's one
way of doing it. I hope this helps. Once you get familiar with this you can
expand the controller to do other stuff too. Like delete, update and insert
actions, reusing the same code snippets.

Balazs



"Jatin" <jnnaik2002@yahoo.co.in> wrote in message
news:1136008698.091244.159160@g44g2000cwa.googlegroups.com...
>
> hug wrote:
>> "Jatin" <jnnaik2002@yahoo.co.in> wrote:
>>
>> >Hello everybody,
>> >
>> >I am new to PHP, and i want to validate the form data posted from one
>> >html form. if any error occurs then it should display the error message
>> >on the same page. can anybody suggest me something?
>>
>> I have lots of code that does that, but there's a bunch of proprietary
>> infrastructure involved to make it easy for me, and as a result I've
>> forgotten a few of the details and might need to look them up.
>>
>> What exactly seems to be the problem? The basic structure of the code
>> will probably turn out to be something like:
>>
>> // read inputs (if any) posted from form data and validate
>>
>> // issue any necessary error messages
>>
>> // display form using previous inputs if any
>>
>> --
>> http://www.ren-prod-inc.com/hug_soft/store.php?action=contact
>
> Thanks for great help.now is it possible to validate on the same page?
> Means i want to Accept data from user and this form will validate the
> data and will show an error message on the same page itself. If no
> error occurs then should forward to next page. I have found the similar
> solution but i lost that link....
>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация