Skip to content

Lesson 02: Processing form results

Anthony Ogundipe edited this page Mar 25, 2014 · 2 revisions

The conventional way of processing your form results in regular php applications is to submit the script to a url specified by the form action attribute right?

Well, phpformapi can do that if you set the action of the form. The form results will be sent 'directly' to the external script. An example is shown below:

index.php

<?php
include "lib/phpformapi.php";
echo phpformapi::get("login.tpl.php"); 
?>

login.tpl.php

<form id="loginForm" name="loginForm" method="post" action="post.php">
  <p>User Name
    <input type="text" name="name" id="name" />
  </p>
  <p>
    <label>E-mail
      <input type="text" name="email" id="email" />
    </label>
  </p>
  <p>
    <input type="submit" value="Login" />
  </p>
</form>

post.php

<?php
  print "<h2>My form was submitted!</h2><p>These are the values we got:</p>";
  echo "<pre>";
  print_r($_POST);
  echo "</pre>";
  echo "<a href='index.php'>Click here to fill a fresh form.</a>";
  exit();
?>

Processing Your Results With A function

Well, if you continue reading to this point, it is likely you are interested in processing your form with a predefined function.

login.tpl.php

<p>We are now going to submit this result. Notice that the name of this form is loginForm.</p>

<form id="loginForm" name="loginForm" method="post">
  <p>User Name
    <input type="text" name="name" id="name" />
  </p>
  <p>
    <label>E-mail
      <input type="text" name="email" id="email" />
    </label>
  </p>
  <p>
    <input type="submit" value="Login" />
  </p>
</form>

index.php

<?php
function phpformapi_submit_loginForm($values)  {
  print "<h2>My form was submitted!</h2><p>These are the values we got:</p>";
  echo "<pre>";
  print_r($values);
  echo "</pre>";
  echo "<a href='index.php'>Click here to fill a fresh form.</a>";
  exit();
}

include "lib/phpformapi.php";

echo phpformapi::get("login.tpl.php"); 
?>

Explanation

The name of the predefined function of a form submission function is a combination of 'phpformapi_submit_' plus the name of the form or the form id (in the html template).

The submission script is supplied with the form submission variables rather similar to $_POST or $_GET albeit with slight modifications.

Warning

The submission function, and the validation function (which will be explain shortly) should be placed in the application, before including the library. Because they are processed immediately the library is included.