View Full Version : PHP form validation

02-19-2004, 10:43 PM
Hi, I am used to validating forms using javascript functions and onSubmit, but I have reached a stage where I really need to do it using PHP instead. Is there any way of calling a PHP function to validate a form on submission ?

02-19-2004, 11:53 PM
well, first of all what type of things are you going to be in need of validating? text boxes? textareas? radio buttons? check boxes?

02-20-2004, 12:10 AM
I am basically wanting to compare a textbox value and radio button values, against some database values. It would be way too messy to get the database values and store them as a big string in a hidden field to pass to some JS function so i really need a php function to do the sql and the validation...

Hope that makes a bit of sense at least :D

02-20-2004, 12:55 AM
There are many ways to approach this.

Usually, the way I go about this is by having the form submit to the same page. For example, if your form is stored in something.php, the form action would be something.php

Here is a simple example, that you might help:

if(isset($_POST["submit"])) {
//validation code
if(strcmp($_POST["name"], "heaps21") == 0) {
//go to the next page since we validated
echo "<SCRIPT>location.href = 'something_else.php?name=$name';</SCRIPT>";
else {
//error comments

<FORM METHOD="POST" ACTION="something.php">
Name: <INPUT TYPE="TEXT" NAME="name">
<INPUT TYPE="submit" NAME="submit">

I didn't test this code, but that's the idea. If it doesn't work let me know..

Hope that helps,

02-20-2004, 01:31 AM
Great! I dont know y i didnt think of that - guess i've been doing so much programming recently its starting to give me mental blocks! Will try and implement something tomorrow but I totally get what you're saying there.