Click to See Complete Forum and Search --> : Required Fields


BOB101
12-11-2005, 02:50 PM
What script can I use to make some of my text fields and check boxes required. Thanks.

Sheldon
12-11-2005, 03:17 PM
if(isset($_POST['feild_name'])){
print "sweet as";
}else{
print "This feild is required";
}

kiwibrit
12-11-2005, 03:19 PM
You can simply write in plain text they are required. You can error trap them only once the form has been posted. Javascript can be used, of course. With a <noscript> comment that it would be helpful to enable javascript. However, you should still error trap in the action file as a backstop if javascript is not enabled.

kiwibrit
12-11-2005, 03:21 PM
I think Sheldon and I are saying the same thing. In php you cannot error trap the required fields before posting - which is what I thought BOB101 was after.

chazzy
12-11-2005, 03:37 PM
yeah, it needs to be done on the server side (obviously) if you're using php

you can have something like this though:

if($_POST['required_field_1'] == "" || $_POST['required_field_2'] == "" || ..... || $_POST['required_field_n'] == ""){
//put your error here
}
else{
//do your processing
}

NogDog
12-11-2005, 04:11 PM
I find the best way to handle this under PHP is to have both the form and the action in the same page. In pseudo code, something like

function display_form($error = "")
{
if(!empty($error))
{
Display error message $error
}
display the input form
}
# main processing
if(post data received)
{
validate the inputs
if(inputs are valid)
{
process the form
}
else
{
display_form("some descriptive error message")
}
}
else
{
display_form()
}