Jump to content

[SOLVED] server side PHP form valudations ... help please


verdure

Recommended Posts

Hi ,

 

i am a newbie to PHP coding. i am using the following method for form validations where i am having a problem.

 

I am using PHP server side validations for my form.

 

The script is printing a success messgae when required fields are eneterd. (checking the validations for fields)

 

But if the validation fails the error messages are not displayed and form field values are not displaying post data ...(I  have set the error messages and field values to post data)

 

i tried looking for a similar problem from previous posts .... hoping u could point to revelant thread answer

Link to comment
Share on other sites

the form validation is in another script ... formValidation.php

 

<?php

//Register an error array

 

if(!$_SESSION["errors"])

$_SESSION["errors"];

 

//Clear any errors that might have been found previously

 

$errors= array();

 

//Set up $formVars array with POST variable and register with session

 

if(!$_SESSION["formVars"])

$_SESSION["formVars"];

 

foreach($HTTP_POST_VARS as $varname => $value)

$formVars[$varname] = trim($value, 50);

 

 

if(empty($formVars["companyName"]))

$errors["companyName"] = "Please enter a company name.";

 

 

//Check if there are any errors

if(count($errors))

{

//If there are any errors. Back to registration page.

header("Location: Registration2.php");

exit;

}

 

// echo "form submitted successfully"

?>

 

 

 

i am able to come print the success message. But the problem is when the validation fails. the error message are not displayed in registration2.php

Link to comment
Share on other sites

If u wanna make it easy,

keep ur validation client side using javascript.

 

Then send the correct data to ur server using php.

 

If u need more info, send me PM ill answer tomorrow, now im off home :) work day over :P

 

That's a horrible idea. The first thing that he should do is server side validation, and then follow it with JS, IF necessary.

Link to comment
Share on other sites

If u wanna make it easy,

keep ur validation client side using javascript.

 

Then send the correct data to ur server using php.

 

If u need more info, send me PM ill answer tomorrow, now im off home :) work day over :P

 

That's a horrible idea. The first thing that he should do is server side validation, and then follow it with JS, IF necessary.

 

I agree to a point. Use JS first, then use a server-side verification. JS will allow more instantaneous feedback, but cannot be relied on.

Link to comment
Share on other sites

I agree to a point. Use JS first, then use a server-side verification. JS will allow more instantaneous feedback, but cannot be relied on.

 

Of course js is instant feedback, and yeah it can't be relied on. That's why I said he should write for functionality and then for features. Javascript could be disabled, people can bypass it, etc. But they can't change your backend validation.

 

Javascript is nice to double this to make it easier, but server side should always be done first imo.

Link to comment
Share on other sites

due to time constraints i am not doing both client side and server side validations.

 

from reading about the best way to validate data ... i decided to go ahead with server side validations as js can be disabled at the client side.

 

so i opted to validate my form data using server side validation.  

Link to comment
Share on other sites

thank you :)

 

any pointers on what might be missing ... the error messages arent displayed.

 

i am thinking i am missing something small. I have tried some other ways of validating data... checking data in the same php file. had some issue there ....

 

so i want to finally stick to one approach and fix my problem.of error messages not being displayed.

Link to comment
Share on other sites

formValidation.php:

<?php
session_start();
//Register an error array

   if(!$_SESSION["errors"])
      $_SESSION["errors"];

//Clear any errors that might have been found previously

   $errors= array();
   
//Set up $formVars array with POST variable and register with session

   if(!$_SESSION["formVars"])
      $_SESSION["formVars"];
      
   foreach($HTTP_POST_VARS as $varname => $value)
   $formVars[$varname] = trim($value, 50);
      
      
      if(empty($formVars["companyName"]))
      $errors["companyName"] = "Please enter a company name.";

      
//Check if there are any errors
   if(count($errors))
   {
      //If there are any errors. Back to registration page.
      header("Location: Registration2.php");
      exit;
   }
   
//   echo "form submitted successfully"
?>

at the top of your form page:

<?php
session_start();
?>

revraz pointed this out. I just expounded upon it.

Link to comment
Share on other sites

revraz : when i include session_start();

 

i get the following error :

 

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

 

on clicking [function.session-start]

 

Not Found

 

The requested URL /function.session-start was not found on this server.

 

is there any problem on the server settings  ???

Link to comment
Share on other sites

in the formValidation.php

 

i have included the session_start() at the top of the page.

 

in the registration2.php : session_start() is included. i still get the warning message

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

 

  <?php

  session_start();

 

  function fieldError($fieldName, $errors)

  {

    if (isset($errors[$fieldName] ))

echo "<font color=RED>$errors[$fieldName]</font> <br> ";

  }

 

 

  ?>

 

Thank you for your help so far .

Link to comment
Share on other sites

yipee :)

 

i have successfully removed the session warning error.

 

it was coming because there were white spaces.

 

i am on to solving the problem of displaying error messages :D

 

the forum has been very useful so far .... previous threads help me remove the session warning error.

 

cheers !!

Link to comment
Share on other sites

I was able to solve my server side validation .. thanks to a simialr problem addresed in a previous post.

 

Thanks people for your help .

 

n here is the code that help me solve my issues ... used the same logic customised it for my form.

 

form.php

<?php

  session_start();

?>

<html>

<head>

<body>

  <form action = 'validate.php' method = 'post'>

<?php  // if there are errors...

 

if ($_SESSION['error']) {

      // for each error...

      foreach($_SESSION['error'] as $error) {

        // echo out the error

        echo "$error <br />";

      } // end for each error 

  // unset the error array so that if user messes

  // up more than once, it won't keep stacking errors

  unset($_SESSION['error']);

  } // end if there are errors

 

  // if there is info

  if ($_SESSION['info']) {

      // for easier var handling...

      extract($_SESSION['info']);

      // no need for this session var anymore

      unset($_SESSION['info']);

  } // end if there is info

// echo out the form, using the user's info as values

// if there is none, it will simply be blank 

?>

      Name <input type = 'text' name = 'username' value=<? echo "$username"?> > <br/>

 

      Something <input type = 'text' name = 'something' value=<? echo "$something"?> > <br/>

      <input type = 'submit' value = 'submit'>

  </form>

</body>

</head>

</html>

 

 

validate.php

<?php

  session_start();

 

  // if vars are posted...

  if ($_POST) {

      // for easier variable handling...

      extract($_POST);

  } // end if posted vars

 

  /* if nothing was posted, or page was directly accessed

      all error messages will be generated, user will be kicked

      to form and all messages will be displayed                  */

 

  // check if username filled out

  if (!isset($username) || trim($username) == '') {

      $error[] = "name not filled out";

  } // end if name not filled out

 

  // check if something filled out

  if (!isset($something) || trim($something) == '') {

      $error[] = "something not filled out";

  } // end if something not filled out

 

  // if an error was generated...

  if($error) {

      // create a session var to carry the posted vars back to form

      $_SESSION['info'] = $_POST;

       

      // create a session var for error messages to display on form

      $_SESSION['error'] = $error;

 

      // redirect back to form

      header('Location: formtest.php'); exit();

  // end if errors 

  } else {

      // form filled out successfully. Do something here like echo something.

      echo "thank you for filling everything out, or whatever.";

  } // end if form filled out successfully

?>

 

 

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.