Jump to content


Form Generator

  • Please log in to reply
No replies to this topic

#1 mistergoomba

  • Members
  • PipPip
  • Member
  • 16 posts

Posted 08 April 2006 - 08:00 PM

I have a form generator script that I'm using that I think works great. The idea is to call a function (IE: formText) with the parameters for name="", etc. Now, when the form is submitted and there are errors, the script will return the values in $_SESSION['retval'] as an array.

For instance, I can call

formText('username','Choose a Username:');

This will send $_POST['username'] to the script. If there are errors, it will send $_SESSION['retval']['username'] back to the form. The function will look something like this:

function formText($key,$text){
     echo $text.' <input type="text" name="$key" value="'.$_SESSION['retval']['username'].'">';

The only issue I have with this, is when trying to pass an array. Take, for example this form which the user can add many usernames.

formText('teacher[username]','Choose a name for the teacher:');
formText('student[username]','Choose a name for the student:');

This code will pass the correct information to the script in the form of $_POST['teacher']['username'], however when being returned to the form, the variable will look like this: $_SESSION['teacher[username]']. I've created a few extra lines of code that will detect the error and fix it, however I would like to know if anyone in the forum has a better suggestion. Maybe there is a better way to go about this whole thing.

Thanks in advance for your help!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users