Wrox Programmer Forums
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2004, 04:38 AM
Authorized User
 
Join Date: Oct 2003
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default validate data

i have a addempform.php that will be able to add employee details to the database.
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?
<?
include "common_db.inc";
$link_id = db_connect();
if(!$link_id) die (sql_error());
mysql_select_db("project", $link_id) or die (sql_error());
?>
<html>
<head>
    <title>Employee Form</title>
     <style>
    h1 {color:white; font-size:24pt; text-align:center; font-family:arial, san-serif}
    p {color:black; font-size:12pt; text-align:justify; font-family:arial, sans-serif}
    p.foot {color:white; font-size:9pt; text-align:center; font-family:arial, sans-serif; font-weight:bold}
    #textbox {position:absolute; left:200px};
    #butreset{ position:absolute; left:200px; top:350px; width:90px; height:30px; background:antiquewhite;cursor:hand}
    #butedit { position:absolute; left:300px; top:350px; width:90px; height:30px; background:antiquewhite;cursor:hand}
    #butsubmit { position:absolute; left:400px; top:350px; width:90px; height:30px; background:antiquewhite;cursor:hand}
    #butexit { position:absolute; left:500px; top:350px; width:90px; height:30px; background:antiquewhite;cursor:hand}
    </style>
</head>
<body onLoad="document.addempform.reset()" >
<form name="addempform" method=POST action="displayempdata.php">
Employee ID&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nb sp&nbsp&nbsp
<?php
$query1 = "select emp_id from Employee order by EMP_id desc limit 1";
$result1 = mysql_query ($query1);
if(!$result1) error_message (sql_error());
while($row1 = mysql_fetch_array($result1))
    $EMP_ID= $row1["emp_id"];
$ID = substr($EMP_ID, -3);
echo EMP. ++$ID;
?>

Employee First Name <input id="textbox" type="text" name="empfname" size="20"><br><br>
Employee Last Name <input id="textbox" type="text" name="emplname" size="20"><br><br>
Employee Gender <select id="textbox" name="empgender">";
                         <option value=\"Male\" >Male </option>
            <option value=\"Female\" >Female </option>
        </select><br><br>
Employee Address <input id="textbox" type="text" name="empadd" size="30"><br><br>
Employee Phone<input id="textbox" type="text" name="empphone" size="8"><br><br>
Employee D.O.B <input id="textbox" type="text" name="empDOB" size="8"><br><br>

<input id="butsubmit" type="submit" size="10" value="Submit" >
<input input id="butedit" type="button" size="10" value="Edit">
<input input id="butreset" type="button" size="10" value="Reset" onclick="document.addempform.reset()">
<input input id="butexit" type="button" size="10" value="Exit" >
</form>
</body>
<html>

 
Old February 2nd, 2004, 04:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default


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/
 
Old April 30th, 2004, 05:30 AM
Registered User
 
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think a more elegant way is to include the file "oohforms.inc" and use its classes for validation.






Similar Threads
Thread Thread Starter Forum Replies Last Post
how can validate dynamic textbox field & get data alpi Classic ASP Basics 0 November 4th, 2007 03:24 PM
Help me to validate 2 radiobuttonlists subhasps8 ASP.NET 2.0 Professional 1 August 2nd, 2007 05:31 AM
do not validate record Vince_421 Access VBA 7 February 13th, 2007 05:03 PM
Unable to validate data in ASP.net manojpatel ASP.NET 1.0 and 1.1 Professional 2 June 13th, 2006 02:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.