6 Replies - 2050 Views - Last Post: 24 April 2011 - 04:26 PM Rate Topic: -----

#1 billj4u   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 15-April 11

No Idea how/where to start Form Validation for Form input file..

Posted 24 April 2011 - 08:30 AM

I was told I should set up a form validation for my input form..have no idea how/where to start..

So here is my working form..

</head>
<body>
<?php include("menu.php"); ?>
<form action="insert.php" method="post">
<center><table><center><caption>Tenant Work Authorization Log</caption></center></center>
<center>
<table class="space" border="1">
 <tr align="right"><td>ID:</td><td>                                <input type="text" name="id"/></td>
 <td>SR Number:</td><td>                         <input type="text" name="srnumber" /></td>
 <td>Tenant:</td><td>                                <input type="text" name="tenant" /></td>
 <td>Floor:</td><td>                                 <input type="text" name="floor" /></td>
 <tr><td>Tower:</td><td>                                 <input type="text" name="tower" /></td>
 <td>Job Description:</td><td>                   <input type="text" name="job_description" /><br></td>
 <td>Labour Cost:</td><td>                       <input type="text" name="labour_cost" /></td> 
 <td>Material Cost:</td><td>                     <input type="text" name="material_cost" /></td></tr>
 </table></center>
 <br>
 <center>
 <table class"space" border="1"> 
 <tr><td>Date TWA Sent to Tenant:</td><td>           <input type="text" name="date_twa_sent_to_tenant" /></td>
 <td>Date TWA Approved:</td><td>                 <input type="text" name="date_twa_approved" /></td>
 <td align="right">Date Parts Ordered:</td><td>                <input type="text" name="date_parts_ordered" /></td></tr>
 <!--</table>--> 
 <!--</table>-->
 <tr align="right"><td>Supplier:</td><td>                          <input type="text" name="supplier" /></td>
 <td>Date Job Completed:</td><td>                <input type="text" name="date_job_completed" /></td>
 <td>Date Billing Given to Accounting:</td><td>  <input type="text" name="date_billing_given_to_accounting" /></td></tr>
 </table> 
 </table>
 <input type="submit" />
 </center>
 </form>
 
Would this form validation work as a outside file..let's say form_validation.php file????

Thanks..








Is This A Good Question/Topic? 0
  • +

Replies To: No Idea how/where to start Form Validation for Form input file..

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3810
  • View blog
  • Posts: 13,838
  • Joined: 08-August 08

Re: No Idea how/where to start Form Validation for Form input file..

Posted 24 April 2011 - 09:13 AM

Form validation:

(1.) HTML form --> (2.) User submits data --> (3.) PHP checks data --> (4.) if data is valid, something is done with it. If it is not valid, the form is resent to the browser with error message(s).

Notice that part 3 requires no HTML. This should be a pure PHP file that decides what HTML to include.

Oh, and using tables to format non tabular data is very bad design.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5342
  • View blog
  • Posts: 14,235
  • Joined: 18-April 07

Re: No Idea how/where to start Form Validation for Form input file..

Posted 24 April 2011 - 09:14 AM

Here your form is setup to submit its data to "insert.php". This means that on insert.php you can read the values that were submitted in the form. Since your form submits this data using the "POST" method, PHP can access this data using the $_POST array on insert.php. On insert.php you would then read the data submitted by the form, check the values and if anything isn't right you can redirect the user back to the form, print an error message or both.

For instance, we want to check your material_cost value submitted in the form. We know the cost can't be below zero and must be a number. So lets validate that...

// First check if the form was submitted by checking if $_POST was created.
if (isset($_POST)) {
   // Now check if material_cost was given a value which is a number and above zero...
   
   if (is_numeric($_POST["material_cost"]) && ($_POST["material_cost"] >= 0)) {
       // Handle the value here
   }
   else { echo "Error, you must have a material cost which is a number and equal to zero or above"; }
}



So I suggest you read through this and see what I have done here. Then go and read up on the $_POST array and how form values can be read using this array. Validate the info accordingly.

Hope this helps! :)
Was This Post Helpful? 0
  • +
  • -

#4 billj4u   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 15-April 11

Re: No Idea how/where to start Form Validation for Form input file..

Posted 24 April 2011 - 09:36 AM

View PostMartyr2, on 24 April 2011 - 09:14 AM, said:

Here your form is setup to submit its data to "insert.php". This means that on insert.php you can read the values that were submitted in the form. Since your form submits this data using the "POST" method, PHP can access this data using the $_POST array on insert.php. On insert.php you would then read the data submitted by the form, check the values and if anything isn't right you can redirect the user back to the form, print an error message or both.

For instance, we want to check your material_cost value submitted in the form. We know the cost can't be below zero and must be a number. So lets validate that...

// First check if the form was submitted by checking if $_POST was created.
if (isset($_POST)) {
   // Now check if material_cost was given a value which is a number and above zero...
   
   if (is_numeric($_POST["material_cost"]) && ($_POST["material_cost"] >= 0)) {
       // Handle the value here
   }
   else { echo "Error, you must have a material cost which is a number and equal to zero or above"; }
}



So I suggest you read through this and see what I have done here. Then go and read up on the $_POST array and how form values can be read using this array. Validate the info accordingly.

Hope this helps! :)



Okay like I said I am quite new to php and anyother programming but I think I am getting the hang of this...so please forgive the err of my way..

First error here was that I posted my code to my form..when I should have posted the code for the insert.php file..

I see what you posted..sort of get the idea..but what is I just want to make sure all input fields are used..at this time..later when I get more familar with it I can make the changes like you said checking value is either =>"0"...

here is my insert code..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>insert.php</title>
<link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" />
</head>
<body>
<?php
 $con = mysql_connect("localhost","root","");
 if (!$con)
   {
   die('Could not connect: ' . mysql_error());
   }
 
mysql_select_db("tecicc", $con);
 
$sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting)
 values
 ('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";
if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
 echo "1 record added";
 
mysql_close($con);
 ?> 
</body>
</html>



Where would I insert your code to start off???

if (isset($_POST)) {



This post has been edited by billj4u: 24 April 2011 - 09:39 AM

Was This Post Helpful? 0
  • +
  • -

#5 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: No Idea how/where to start Form Validation for Form input file..

Posted 24 April 2011 - 02:23 PM

First thing never input data into a database directly from $_POST without applying mysql_real_escape_string on each $_POST element.

$safe_to_enter = mysql_real_escape_string($_POST['field']);

Was This Post Helpful? 0
  • +
  • -

#6 billj4u   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 15-April 11

Re: No Idea how/where to start Form Validation for Form input file..

Posted 24 April 2011 - 03:23 PM

View Postcodeprada, on 24 April 2011 - 02:23 PM, said:

First thing never input data into a database directly from $_POST without applying mysql_real_escape_string on each $_POST element.

$safe_to_enter = mysql_real_escape_string($_POST['field']);



Could you explain further your meaning of never input data into database from $_POST without applying mysql_real_excape_string..on each element...????

And please give an example of how this would look like..?????

Thanks...

PS..I got my Form validation working ...also..but this is a new wrinkle to it..hmmmm

Can I do it this way example????

$safe_to_enter = mysql_real_escape_string('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]'";)/>



Or it has to be like this way???
$safe_to_enter = mysql_real_escape_string('$_POST[id]);
$safe_to_enter = mysql_real_escape_string('$_POST[srnumber]);
$safe_to_enter = mysql_real_escape_string('$_POST[tenant]);
$safe_to_enter = mysql_real_escape_string('$_POST[floor]);



Elaborate please..so I can fully understand...

thanks..

This post has been edited by billj4u: 24 April 2011 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4223
  • View blog
  • Posts: 13,378
  • Joined: 08-June 10

Re: No Idea how/where to start Form Validation for Form input file..

Posted 24 April 2011 - 04:26 PM

have a look at the official examples.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1