form with variable names, not static

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

form with variable names, not static

Post by rhecker »

I am creating a feature for a custom CMS that allows my clients to create forms that they can put on their sites. The data from these forms does not populate a database; it just gets emailed back to the client. The problem I am encountering is that if the form doesn't pass validation, I want the form fields to repopulate with what is stored in the $_POST array, but since the field names are variables, I'm having trouble getting the re-population to work.

I tried using an iterator to name the fields, like so:
echo "<input type ='text' name='value[$i]' size=66 value = '$value[$i]'/>

This method worked sometimes, but not others. Sometimes fields would be populated, sometimes not, and sometimes with the wrong values.

To complicate it, fields can be checkboxes and radio buttons as well as text and textareas.

Except for this one issue, the whole thing works pretty well.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: form with variable names, not static

Post by social_experiment »

Have you thought about storing the names of the fields in a session variable?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: form with variable names, not static

Post by rhecker »

Storing the names of the fields in a session variable? I don't see how that would help. Maybe you can explain what you are thinking.

The issue is that the names are variables, so every field has the name $thisfield. I use variable variables based on the $_POST to create the text key/value pairs that populate the email that is created, like NAME: John Smith, ADDRESS: 1 Main Street but I can't (or have not figured out how to) use the same technique to populate the values of the form fields in the case where I need to return the visitor back to the form, and creating an indexed array also did not work.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: form with variable names, not static

Post by Weiry »

I think this would come down to more array structure than anything else but you would have to be careful how it gets re-written to the page after validation. The other problem you will face is that you would need a way to determine what sort of field you are actually reading and as POST doesn't contain what sort of field you have just passed, you will need to (i would imagine) have some sort of text reference inside the name="" field in order to determine this.

For example:

Code: Select all

<input type='text' name='txtMyName' value='MyName' />
<input type='radio' name='radMyRadio' value='MyRadio' />
If you note the first 3 characters of the "name" field contains a definition of the type of input your dealing with.

When actually reading that data into an array, i would be trying to format the array in the form of:
[syntax]array(
array(
'type' => "txt",
'name' => "MyName",
'value' => "MyName"
),
array(
'type' => "rad",
'name' => "MyRadio",
'value' => "MyRadio"
)
)[/syntax]
So you would want to do something like this to get them into your array.:

Code: Select all

foreach($_POST as $key => $value){
    $fields[] = array("type" => substr($key,0,3), "name" => substr($key,3), "value" => $value)
}
At that point, you have a way to validate each individual item and (if you want) you can add in an extra array field if an error was detected to the corresponding entry. So when you loop through the array to recreate the form, you can print the errors as well.

Hope that helps shed some light on one way to handle it all :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: form with variable names, not static

Post by requinix »

Arrays, like you (OP) are trying to do, have always worked best for me. If your problem is where to get the information from then it could be as simple as

Code: Select all

if (form is submitted) {
    values = $_POST[values];
    ensure that the values are appropriate for the form; // correct ids and whatever
} else {
    values = get them from wherever they normally come from;
}
(ie, figure out what your "data source" is and display the values from that)
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: form with variable names, not static

Post by rhecker »

Thank you everyone for your responses; however I clearly explained my issue very badly because none of the suggestions were relevant. I did solve the problem, though my solution seems messy to me.

Normally in a form item the value is a variable but the name is static, like <input type = 'text' name='myfield' value='$myfield'/>. This makes it easy to repopulate the form with the POST array, if necessary.

Part of the solution was to create a variable variable to get the keys and values of the POST to match up correctly. The other part was to base the variable names on the auto_increment id from the database table that defines each form item.
Post Reply