Jump to content

validation on all forms


NickG21

Recommended Posts

good morning and happy monday to everyone,
the only question that i have is if i can include a validation.php page with the code to validate 6 different forms.  every form  has a different name for the "submit/next" button and all of the fields that are passed have different labels.  would i be able to just do all of the validation in that one page with just

if(isset('submit')){
blah blah...
}
if(isset('next')){
blah blah...
}
etc......

i think this should work fine but id like to be a little mroe sure before i decide to put in validation for 60 different form fields.
thanks in advance
Link to comment
Share on other sites

i believe that it will work, however after the validation completes i want to send it back to the previous form page if it did not complete the validation and if it does i want to process onto the next form to be included.
all of the forms are supposed to be redisplayed like this on one main form
[code] <?php
        IF (!isset($_POST['step'])) {
  include "ListInfo.php";
ELSEIF ($_POST['step'] == 2){
  include "OptServices.php";}
ELSEIF ($_POST['step'] == 3){
include "ContInfo.php";}
ELSEIF ($_POST['step'] == 4){
include "BillInfo.php";}
ELSEIF ($_POST['step'] == 5){
include "Survey.php";}
ELSEIF ($_POST['step'] == 6){
include "Terms.php";}
?>[/code]
any idea how i could say send back to ListInfo.php if it didn't correctly process?
error code:
[code]
if(count($error) > 0){
echo "Please Correct Your Errors Before Proceeding";
}ELSE{
include_once("HeaderImage.php");
}
}[/code]
if all of the fields are correct everything continues perfectly, i just don't know how to redisplay the original form, echoing to correct invalid entries
Link to comment
Share on other sites

the only problem with me doing that is that ListInfo.php has no .css involved with it, it is just a plain form that i Include in headerImage.php which has all of the styles in it.  what if i sent back to headerImage.php and made the 'step' = whatever it should with that particular page?

plus i have all of the
<?php echo '$variable' ?> in all of the text boxes and i set the values to "" if they don't pass validation.  Those work correctly
Link to comment
Share on other sites

[code]if(count($error) > 0){
header('Location: headerImage.php');
echo "<input type='hidden' value='' name='step'>";
echo "Please Correct Your Errors Before Proceeding";
}ELSE{
include_once('headerImage.php');
}
}[/code]
that works correctly to bring me back to the page if there are errors, however, it will not display the echo or redisplay the values of the variables that have been stored in the session.  i would imagine it is because i am redisplaying the initial page that is included in headerImage.php, in this case ListInfo.php.  any idea how i can get around this, because headerImage.php has the session stored in it and i am almost positive the variables are still saved.
Link to comment
Share on other sites

the variables are only saved if they pass through the validation, when the page is returned everything is reset.
i thought putting all of the variables into a hidden field might work to keep them but it doesn't either

foreach($_SESSION as $key => $value)
{
    echo "<input type='hidden' value='" . $value . "' name='" . $key
. "'>";
}
Link to comment
Share on other sites

i want all of the information to be stored no matter what, if the information does pass it is just kept as is, but if there is other information that doesn't pass i want that form to reLoad with the good information still in the text fields and the fields with invalid input will just be blank.  the user will then have to refill the information that was invalid.  when the information is valid, i will move on to the next form.  that part works fine, it is only if the info is invalid i can't Re-Post the good information back into the form because all of the variables are reset.
i hope that is a little more clear
Link to comment
Share on other sites

The easiest way is to run the validation on the same page as the form. Usually this is not a problem.

Example:
[code]

<?php

$complete = false;
$empty_arr = array();

foreach($_POST as $fieldname => $fieldvalue)
{
  if(empty($fieldvalue))
  {
    $empty_arr[] = $fieldname." was left empty";
  }
  ${$fieldname} = $_POST[$fieldname];
}

if(!empty($empty_arr))
{
  echo "<ul><li>";
  echo implode($empty_arr, '</li><li>');
  echo "</li><ul>";
}
else
{
  // no empty fields, prosess posted values already defined as $fielnames
  $complete = true;
}

if($complete == false)
{
echo <<<_HTML

<form method="post" action="{$_SERVER['PHP_SELF']}">
<p>Name:<br /><input type="text" name="name" value="$name" /></p>
<p>Email:<br /><input type="text" name="email" value="$email" /></p>
<p><input type="submit" name="submit" value="Send" /></p>
</form>

_HTML;
}

?>

[/code]
Link to comment
Share on other sites

i understand it might be easier to validate on the same form, but i am unaware of how to have my main site proceed and load the next form if the information is valid.  Jesi, i cannot save the information into session variables and then redisplay the initial page, i thought it would just keep all the submitted data since the session was already opened but it clears all the values for every variable until you process on to the next page.
Link to comment
Share on other sites

Example with sessions, jumping to another file upon success:
[code]

<?php

session_start();

if(isset($_POST['submit']))
{
$empty_arr = array();

foreach($_POST as $fieldname => $fieldvalue)
{
  if(empty($fieldvalue))
  {
    $empty_arr[] = $fieldname." was left empty";
  }
  ${$fieldname} = $_POST[$fieldname]; // just so that submitted values can be re-displayed in the form if another field value is missing
  $_SESSION[$fieldname] = ${$fieldname}; // also store value in a session in case no field values is missing in the end
}

if(!empty($empty_arr))
{
  echo "<ul><li>";
  echo implode($empty_arr, '</li><li>');
  echo "</li><ul>";
}
else
{
  header("location: next.php"); // and in next you fetch all current data from $_SESSION['fieldname']
 
  // inside next.php you can put this as a test (uncomment it ofcource):
 
  // <? php
  // session_start();
  // echo "<pre>";
  // print_r($_SESSION);
  // echo "</pre>";
  // ? >
 
  exit();
}
}

echo <<<_HTML

<form method="post" action="{$_SERVER['PHP_SELF']}">
<p>Name:<br /><input type="text" name="name" value="$name" /></p>
<p>Email:<br /><input type="text" name="email" value="$email" /></p>
<p><input type="submit" name="submit" value="Send" /></p>
</form>

_HTML;

?>

[/code]
Link to comment
Share on other sites

  • 3 weeks later...
hello guys,
how to create a good validation..
because mine is not so good..its still accepting any data entry (letter,symbol or number) but
suppose to be [b]number[/b] or none...
here's i'd post the image of my form..hope the image will help.

p/s: im very new in php..so,i really need some guidence...
Link to comment
Share on other sites

to validate against only numbers you need to use a regular expression (regex)
here is a tutorial
http://weblogtoolscollection.com/regex/regex.php

if you give us more detail we can help you build your regex.
I think you try to enter hours and minutes into your form
is the hour 1 written as a 1 or as 01 ?

anatak
Link to comment
Share on other sites

here's my code
[code]<?php
if(!isset($_POST['submit']))
{
?>
<form action="" method="post">
<table width="100%" cellpadding="10" cellspacing="0" border="0">
<!--Step 3 : Select Time-->
<tr>
<td style="border-width:1px; border-color:#A8B2C6; border-style:solid;" class="body" colspan="2">
<div style="font-size:18px; color:#007E8C; font-weight:bold;">
<font color="#cccccc">3. </font>Select Time</div><br>
</td>
</tr>
<tr>
<td width="15%" class="body-facilities">From:</td>
<td class="body-facilities">&nbsp;<input type="text" name="timeFrom" size="1" maxlength="2">&nbsp;:&nbsp;<input type="text" name="timeFrom1" size="1" maxlength="2">&nbsp;
<sup style="color:#FF0000">eg: 08:00</sup>
</td>
</tr>
<tr>
<td class="body-facilities">To:</td>
<td class="body-facilities">&nbsp;<input type="text" name="timeTo" size="1" maxlength="2">&nbsp;:&nbsp;<input type="text" name="timeTo1" size="1" maxlength="2">&nbsp;
<sup style="color:#FF0000">eg: 23:00</sup></td>
</tr>
<tr>
<td class="body-facilities"><sub>Operating Hour:</sub></td>
<td class="body-facilities"><sub><?php echo $row_rs['fac_wkdayS'];?>&nbsp;to&nbsp;<?php echo $row_rs['fac_wkdayE'];?>&nbsp;(Monday to Friday)</sub><br>
<sub><?php echo $row_rs['fac_wkendS'];?>&nbsp;to&nbsp;<?php echo $row_rs['fac_wkendE'];?>&nbsp;(Saturday &amp; Sunday)</sub></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="submit" value="Next">   &nbsp;&nbsp;
<input type="hidden" name="fac_id" value="<?php echo $fac_id;?>">   </td></tr>
</table>
</form>
<?php
}
?>
</td>
</tr>
</table>
</td>
[/code]

again how to build and put the regex into this code and which line it was?
i appreciate any help...
Link to comment
Share on other sites

anyway thx anatak..
i do understand the code that you've given..
but which line should i put the code inside my php code?
i put a long with php tags and nothing happened..
and it still run'd like usual..
sorry if i ask silly questions..rite now im trying and still got some error.

Link to comment
Share on other sites

  • 4 weeks later...
Xster I noticed you write <form action="" method="post">
so you in the page where you receive the information from you form you have to do something like
$time = $_POST['timeFrom']
$legit = ereg("^[0-9]{2,2}$", $time);
if ($legit==TRUE){
time is correctly formatted}else{
time is not correctly formatted}

I hope this helps

anatak
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.