You are here: Re: dealing with incorrect input and moving back a page... « All PHP « IT news, forums, messages
Re: dealing with incorrect input and moving back a page...

Posted by scott on 10/13/77 11:29

I posted this for a similar question:

What I do for form validation is I call the same page for display,
validation and database entries.

In the action part of the html form I use:
<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF'];?>" method="post"
enctype="multipart/form-data" name="add_fleet" id="add_fleet">

This way when the submit button is clicked, the page calls itself.
To make this work you will need to set a 'hidden field':
<input name="mode" type="hidden" id="mode" value="add">

Then the whole process is as simple as checking to see if:
$_POST['mode'] == 'add'
if it does branch off to validate the fields and if the fields fail set
an $error variable. Then before you do your database insert, check to see:
isset($error)
If it is then do not do an insert but continue to display the form with
the fields filled out with what they already entered. If $error is not
set, then continue with the INSERT and if all goes well just redirect
the page to whatever you want such as a confirmation page.

I hope this is not too confusing. Here is some code that works.
################################################################
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<table width="500" border="1" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td align="center">
<?php
##### Check if form was Submitted #####
if($_POST['mode'] == 'add'){
### Retrieve the variables ###
if($_POST['field1'] == ""){
$error = "* Please fill in Field #1<br>";
}else{
$field1 = $_POST['field1'];
}
if($_POST['field2'] == ""){
$error .= "* Please fill in Field #2<br>";
}else{
$field2 = $_POST['field2'];
}
if($_FILES['file']['name'] == ""){
$error .= "* Please select a File to upload<br>";
}else{
$file_name = $_FILES['file']['name'];
/* Process file*/
}
### If fields are valid then attempt an insert ###
### If not set $error and bypass the insert ###
if(!isset($error)){
$insert = "INSERT INTO $table(
Field1,
Field2,
FileName)VALUES(
'$field1',
'$field2',
'$file_name')";
if(!$result = mysql_query($insert)){
$error .= "Error in inserting<br>";
}else{ ### If insert goes well, redirect to another page ###
?>
<script language="JavaScript">
<!--
self.location="<?php echo "confirmation.php";?>"
//-->
</script>
<?
}
}//if(!isset($error)){
### If there was an error display it ###
if(isset($error)){
echo "<table width=\"300\" border=\"0\" cellspacing=\"0\"
cellpadding=\"0\">";
echo "<tr>";
echo "<td bgcolor=\"#FF0000\">".$error."</td>";
echo "</tr>";
echo "</table>";
}
}//if($_POST['mode'] == 'add'){
?>
<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF'];?>"
method="post" enctype="multipart/form-data" name="add_fields"
id="add_fields">
<table width="500" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="right">Field #1: </td>
<td><input name="field1" type="text" id="field1" value="<?php
echo $field1;?>"></td>
</tr>
<tr>
<td align="right">Field #2: </td>
<td><input name="field2" type="text" id="field2" value="<?php
echo $field2;?>"></td>
</tr>
<tr>
<td align="right">File Upload: </td>
<td><input type="file" name="file">
<br><?php echo $file_name;?></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="Submit"
value="Submit">
<input type="reset" name="Submit2" value="Cancel"
onClick="self.location='redirect.php'">
<input name="mode" type="hidden" id="mode" value="add"> </td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>
###################################################################
If none of this makes sense, email me and we can go in deeper.

scottyj1@cox.net





juglesh wrote:
> Domestos wrote:
>
>>Say a user inputs an invalid string into a field - say for example e-mail in
>>a form on Page A...
>>
>>Validation is done on Page B. It notices that the e-mail is incorrect and
>>informs the user and jumps them back to the form on Page A.
>>
>>To get the values the user had input in the e-mail and other fields on the
>>form to re-display rather than a blank form I do the following...
>>
>>On Page B I set $_SESSION('back')=1 and set other variables like
>>$_SESSION('name')=$name and $_SESSION('email')=$email
>>
>>on returning to PageA I check if $_SESSION('back') exists and if so set the
>>$name and $email to the $_SESSION superglobals, if not I read the
>>information in from a database. I then un-register $_SESSION('back') and the
>>others...
>
>
> well, the easiest way, is on page b, just go 'hey that's an invalid
> email addy, use yer back button and fix it' The values that they typed
> should be there.
>
> Or, you could have the page A submit to itself.
> if ($_POST['submit'] == 'submit'){
> //do your validation
> }
> if (($validation == 'error')OR($_POST['submit'] != 'submit'){
> //display the form
> // <input type='text' name='email' value='<?php echo $_POST['email']
> ?>'>
> }
> else{
> // do whatever, validation is passed
> }
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация