Quote:
quote:Originally posted by hosefo81
i have a addempform.php that will be able to add employee details to the database.
|
Ok.
Quote:
quote:Originally posted by hosefo81
1. i would like to check if the user enter all the necessary information in the textbox when the submit button is clicked.
if yes, then the displayempdata.php is displayed
else the focus is set to textboxt that is empty together with the previously entered data.(means the uncompleted form)
How am i going to do it in php?
|
It's really simple. You iterate across all the user input and make sure that all the required fields are set.
Here's one way:
$required_fields = array("firstName", "lastName");
$required_filled = false;
foreach($required_fields as $field)
{
$required_filled = $required_filled && isset($_POST[$field]);
}
if (! $required_filled)
{
// display the form again.
}
else
{
// process the submitted form.
}
Bear in mind that there's a million ways to do this. Every form that a user submits SHOULD go through some level of validation when it's submitted. (Validation means that you check to make sure all the user input makes sense -- for example, if you want someone to enter an email address, you shouldn't accept "dlsakjflsd" as a valid submission.)
This is why so many PHP forms submit to themselves -- it makes sense that the same page that GENERATES a form knows best about what data is going to be submitted.
This is how such forms typically work:
<?php
// check for user submission:
if (isset($_POST...))
{
// incoming form, validate data.
if (form data is valid)
{
submit form
}
}
generate form here. The form generated here is the same code for both the first-time users (no POST data submitted yet) and for users that have submitted a form with errors.
If a user has previously submitted data, you can pre-populate the form fields with the data the user submitted. If a required field was filled in incorrectly or not filled in at all, you can highlight this field (put it's name in red bold font or whatever).
?>
Take care,
Nik
http://www.bigaction.org/