Reply to Re: Form validation with PHP/Javascript

Your name:

Reply:


Posted by Jerry Stuckle on 08/15/06 23:42

Chris wrote:
> Hi,
>
> I have a form for uploading documents and inserting the data into a mysql
> db. I would like to validate the form. I have tried a couple of Javascript
> form validation functions, but it appears that the data goes straight to the
> processing page, rather than the javascript seeing if data is missing and
> popping up an alert. I thought it may be because much of the form is
> populated with data from the db (lists, etc.), but when I leave out data in
> a simple textbox, it still doesn't see it. I've tried a couple of different
> things, that didn't work, so I simplified and tried to address one form
> element at the simplest level at a time - still no luck. Is there something
> special I need to do to make the Javascript work before the PHP? Is there a
> good way to do client side validation with PHP?
>
> Here's most of the code:
>

<code snipped>

Chris,

As Rik is trying to tell you, there is no interaction between javascript
and PHP (unless you're using Ajax - but that's another story). PHP can
create the JS - just like you can type it out. But JS doesn't call PHP
functions.

I don't rely on js for form validation. The user can turn it off too
easily. I validate everything server side. Even when I validate with
JS, I revalidate on the server.

There are two ways to validate on the server - on the current page or on
the next page. Both are done in PHO.

Validating on the current page means you submit the form to the same
page. At the beginning of your page, check to see if the page has been
submitted. If not, go display your form. If it has, validate the data.
If it validates, redirect to the next page. If not, display the
errors. The advantage here is the validation code is on the same page
as the form it's validating - nice and compact. The downside is you
can't post the data to the next page, so you either need to use GET or
store the data in a session object.

Validating on the next page means you do the actual validation in the
page receiving the form. You can post the data from the first form to
the second one, but if you detect an error it's more difficult to go
back to the first page.

I use both, depending on the circumstances.

But the bottom line is - always validate server side, even if you fist
validate client side.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[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

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