Re: Form validation issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Tedd,

If you are using a post method using $_SERVER['PHP_SELF'], then values are
present in the POST array, hence, you would write your html with
interspersed php like so:

<input type="text" name="username" value="<?php if
(isset($_POST['username'])) echo $_POST['username'] ?>" id="username" />

I sometimes use a function for the echoing of these values if I use the same
form for first time (ie. registration) and editing (update), and the
function checks for a $_POST value, then secondly for an existing database
value variable (ie. $row['username']). If either exist, populate the input
with it (precedence given to POST), otherwise it is empty.

The function looks something this:

function echoValue($post=null, $row=null) {
    if (isset($post)) {
        echo $post;
    } elseif (isset($row)) {
        echo $row;
    }
}

and is used like this:

<input type... value="<?php echoValue($_POST['username'], $row['username'] )
?>" id="username" />

after performing a query on a query-string variable (eg. profile.php?id=57
---> 'SELECT * FROM `users` WHERE `id` = '.$_GET['id'] ) etc.

On Mon, Dec 21, 2009 at 8:03 AM, tedd <tedd.sperling@xxxxxxxxx> wrote:

> At 9:43 PM -0500 12/20/09, Ernie Kemp wrote:
>
>> Good Day,
>>
>>                I need help in in validating a form.
>>                The for is valdated be a javascript frist then if all the
>> fields are filled in its valaded be PHP.
>>
>>                The Form starts with:
>>                <form name="myForm" action="<?php echo
>> $_SERVER['PHP_SELF'];?>" method="post" onsubmit='return formValidator()' >
>>
>> The "formValidator()" goes to a javascript and does display the missing
>> information in this case BUT then the page gets reloaded and clears all the
>> javascript error messages and does the PHP validation.
>>
>> The PHP only runs if the fields are set by testing using 'isset".
>>
>> Without puting on numeric lines of go can you suggest things I must have
>> overlooked. Silly request but there must be something I'm overlooking.    I
>> have simular code on other programs but this one is casuing me trouble.
>>
>> Thanks every so much..
>>
>>
>
> Ernie:
>
> Client-side javascript can help populate fields and correct any problems a
> user might have, but once the form is submitted to the server, then the data
> is sent and evaluated server-side, hence validation.
>
> However, if the server-side evaluation fails and the page is refreshed,
> then all the previous values are lost -- UNLESS -- you keep them in a
> cookie, database, or session. I suggest using a session.
>
> Cheers,
>
> tedd
>
> --
> -------
> http://sperling.com  http://ancientstones.com  http://earthstones.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux