PHP - getting unknown number of form fields

maw

Supreme [H]ardness
Joined
Sep 27, 2000
Messages
4,135
ok, so i have a javascript driven survey page. the nature of the survey is such that if the user answers certain questions, then other questions are automatically disabled (e.g. if you answer question A, then Question D is disabled, but if you answer Question B, then Question D is enabled). So obviously, disabled form fields are not going to get submitted.

Anyway, what i need to know is if there is an efficient way in PHP to assign the submitted form fields to variables in my script without testing for their existence first?

right now i have
if ($_POST['question1'])
{
$q1 = $_POST['question1'];
}
if ($_POST['question2'])
{
$q2 = $_POST['question2']);
}

etc.. only problem is, i have about 60 questions, of which anywhere between 20-30 of them will not be submitted. Writing all those if statements for each possible answer just seems really tedious to me.

I know in ASP/VBScript, a variable assigned to an empty POST value automatically gets an empty string as it's value, but in PHP that would generate an error.
so, what i'm asking is if there is a way in PHP for me to simply say:
$q1 = $_POST['question1'];
and have $q1 automatically get an empty string (or NULL value) if question1 was not in the submitted $_POST variables?
 
i suggest doing like so
PHP:
for ($i = 1; $i <= 60; $i++) {
    $tmp = "q$i";
    $$tmp = (isset($_POST["question$i"]))
            ? $_POST["question$i"]
            : '';
}
this uses the idea of variable variables
http://us2.php.net/manual/en/language.variables.variable.php

also, it might be a good idea to put them in an array instead of just having them float around
PHP:
$questions = array();
for ($i = 1; $i <= 60; $i++) {
    $questions["q$i"] = (isset($_POST["question$i"]))
                        ? $_POST["question$i"]
                        : '';
}
 
I have never tried the whole JS form thing, but I have submitted a random number of fields. When they submit do the 20-30 they don't submit now get to the $_POST?

If they don't look at using a foreach statement. Then you could check the key->value pair to get the field name (as long as you match the form field names to the database names and such)

That way your foreach loop would go like this:

// first time through
$_POST['q1']
// 2nd
$_POST['q5']
// 3rd
$_POST['q6'])

And you only deal with the ones that are submitted.
 
thanks for the suggestions :)

i was still thinking in "VBScript" mode, so it was difficult for me to envision other approaches to the problem. This should help tremendously.

one more quick question.

what is the difference between
if (isset($_POST['question1']))
and
if ($_POST['question1'])

don't they both do the same thing?
 
maw said:
thanks for the suggestions :)

i was still thinking in "VBScript" mode, so it was difficult for me to envision other approaches to the problem. This should help tremendously.

one more quick question.

what is the difference between
if (isset($_POST['question1']))
and
if ($_POST['question1'])

don't they both do the same thing?

The first checks if the value is set, the second check assumes it has a value and checks for it to equate to true.

As far as code to accomplish what you want i'd do something like:
PHP:
foreach($_POST as $question=>$answer){
   if($answer){
      ${$question}=$answer;
   }
}

Then what you'll end up with is a bunch of variables like $question1 = whatever the form submitted. And you'll only end up dealing with questions that were submitted and had an answer.
 
Sgarissta said:
The first checks if the value is set, the second check assumes it has a value and checks for it to equate to true.
just to be as specific as possible, isset() checks to see if a variable has been initialized, that is, if it has been used before (if it's null it returns false though). just using a variable inside an if statement checks to see whether it's value evaluates to true/false, as Sgarissta said, however i believe it raises a notice, you wouldn't see it unless you have error_reporting to E_ALL.

note some of the user comments
http://us2.php.net/isset
 
Sgarissta said:
As far as code to accomplish what you want i'd do something like:
PHP:
foreach($_POST as $question=>$answer){
   if($answer){
      ${$question}=$answer;
   }
}

Then what you'll end up with is a bunch of variables like $question1 = whatever the form submitted. And you'll only end up dealing with questions that were submitted and had an answer.

so if i follow this correctly, this will return a bunch of variables whose names are the same as the form elements themselves? So if i have a form element named "textbody" and one named "btnYes" then i'll end up with:
$textbody = some text
$btnYes = y
 
maw said:
so if i follow this correctly, this will return a bunch of variables whose names are the same as the form elements themselves? So if i have a form element named "textbody" and one named "btnYes" then i'll end up with:
$textbody = some text
$btnYes = y

In theory ;) I just wrote that code off the top of my head, but you're correct in that is what i inteded for the code to do.
 
maw said:
so if i follow this correctly, this will return a bunch of variables whose names are the same as the form elements themselves? So if i have a form element named "textbody" and one named "btnYes" then i'll end up with:
$textbody = some text
$btnYes = y
that is exactly the idea of variable variables
 
there are also variable functions
PHP:
$var = 'strtoupper';
echo $var('This Text Will Be Uppercase');

yeah, php kicks fuckin ass
 
tim said:
there are also variable functions
PHP:
$var = 'strtoupper';
echo $var('This Text Will Be Uppercase');

yeah, php kicks fuckin ass

i've been tinkering around with it on my personal site, and sort of ad-hoc learning bits and pieces of it as needed. my PHP coding techniques might not be the best right now (still thinking in VBScript mode), but i know enough to get everything organized and doing what i need it to do. it's fun learning some of the more in-depth features of the language.

i finally got around to installing the PHP module on the webserver at work (IIS), so now i can get a lot more hands-on time with it.
 
Back
Top