Thursday, October 18, 2007

Save Time on Submitting Forms

I always involved in making applications that submit forms to databases. In order to save time, I just copy this function to all my applications - because they're a great time saving function that suits all my applications. Here I'll show you step by step on how I made the function:

(This is the form example)

<form method="post" action="/process">
Input 1: <input type="text" name="input1">
Input 2: <input type="text" name="input2">
Input 3: <input type="text" name="input3">
<input type="hidden" name="action" value="submitForm1">
<input type="submit" value="Submit">
</form>

Note two important things in the form above:
  1. action value; and

  2. form action

The action value will ensure the form to be submitted the right way whereas the form action will lead itself to index.php.

In the index.php, I set a condition - switch ( portion(1) ) case "process";.

The "process" condition goes like this:

<?php
if ( isset($_POST['action']) && $_POST['action'] == "submitForm1" ) {
  if ( submitForm($_POST) ) echo "Successful!";
  else echo "Failed!";
}
?>

Do you realize - I sent the data using array - $_POST array! This is what I'm talking about. Sorry for the long intro... :P

Ok now let's see what is inside the submitForm() function. In brief, the function must contain:

  1. key assigning process for each input value;

  2. character control using str_replace function;

  3. additional controls and conditions; and

  4. SQL statement structures;


I set the head like this:
function submitForm($post,$additional_input='',$unwanted_input='')

Now we've covered the main concept and some code go through. I think I've to stop here for now. See you in Part II!

2 comments:

zplits said...

Hi Sir, good day! Can you please help me on my problem? I'm just a beginner in php. Please. Will you?

ImamKhalid said...

Yes zplits, sure I can. Just email me at imamkhalid@gmail.com! :)