1

is it possible to write a PHP page say form.php with a php function generalValidate($form) that is able to perform the following scenario :

  1. user browse to form.php
  2. user gets an html form
  3. user fills form and the form is sent back with POST method to form.php
  4. form.php activate generalValidate($form) where form is the just recived form filled by user
  5. generalValidate($form) returns true if this form is valid (for exemple properly filled), false else.

i think that another way to describe my question will be - is there a general way to iterate over an HTML form sent by a client (perhaps a form that the php page itself sent to client in the first place) while activating some code over each of the form values?

dont eat me if its a bad question, im really just trying to learn :)

a code exemple to fill for your convinience :

<?php
    function generalValidate($form) {
        ...
    }
    if (!isset($_SESSION)) 
            session_start();
    else if (generalValidate(...)) {

    }


?>
<html>
   <head>

   </head>
   <div>
      <p>Register</p>
   </div>
   <form id="regfrm" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" align="center">
      <table align="center">
         <tr>
            <td>First Name :</td>
            <td><input name="fname"  value="<?php if (isset($_POST["fname"])) {echo  v($_POST["fname"]);}?>" required></input></td>
         </tr>
         <tr>
            <td>Last Name :</td>
            <td><input name="lname" value="<?php if (isset($_POST["lname"])) {echo  v($_POST["lname"]);}?>"   required></input></td>
         </tr>
         <tr>
            <td>Email :</td>
            <td><input id="email" name="email" value="<?php if (isset($_POST["email"])) {echo  v($_POST["email"]);} else {echo "[email protected]";}?>"  required></input></td>
         </tr>
         <tr>
            <td>Password :</td>
            <td><input name="pw" type="password" value="e" required></input></td>
         </tr>
         <tr>
            <td>Retype password :</td>
            <td><input name="pw" type="password" value="e" required></input></td>
         </tr>
      </table>
      <input type="submit" value="Register" ></input>
   </form>
   </body>
</html>
2
  • This is a very common situation and various approaches exist, e.g., this component in the Zend Framework (which might be a little bit over your head as you seem to be relatively new to this matter, but still might give you the general idea): framework.zend.com/manual/1.12/en/zend.filter.input.html Commented Aug 7, 2013 at 21:56
  • @Niko yeah, i am new to PHP and i asked this question just to learn some tricks in PHP, so using an extrnal framework wont make me know PHP any better :( Commented Aug 7, 2013 at 21:59

4 Answers 4

2

Yes. Although iterating over the fields gives you a little less clarity and makes it more messy when you want to determine how to validate said field (for example, to know if whether the value should be a name or a number or whatever), but you can do it this way:

In your PHP script you could have something like:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Determines if the form was sent through the POST method

    foreach ($_POST as $fieldName => $formValue) {
        // Validate $formValue
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah but iterating over the values manually is exactly what i was trying to avoid, i want to be able to iterate over them without explicitly knowing whats their name or id, you see?
1

What I think you want to ask is if form data can be manipulated without knowing the form's variable names. I say this because you want to have a general purpose function where the code can be reused for any form. Since this may be any form that currently exists or a form you will create in the future you will not know the name of the variables in the form.

This code captures the form's input. All you have to do now is create a function that does whatever to the $name and $item values as they are looped through.

<?php
foreach($_POST as $name => $item) {
  print "name::$name item::$item<br>";
} 
?>
<html><title>test</title>
<form method="post">
field one:  <input type="text" name="one">
<br>
field two:  <input type="text" name="two">
<input type="submit" value="go!">
</form>
</html>

Comments

0

Of course, it is possible to have the page in which the original form resides as the recipient of the form dialog. Through the session variables, but mainly through the contents of the button variables you can determine which state your form is currently in (after having clicked a submit button you will get a $_REQUEST array element with the name of the button holding the value of the button).

Comments

0

Take a look at the answer here.

This is actually a canonical question for receiving form data in PHP. There are lots of ways to do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.