Jump to content

[SOLVED] post issue


gish

Recommended Posts

hi phpfreaks

 

 

I am creating a questionnaire for my website but I am having trouble the script. Each question has three options that will be posted 1,2,3 1 meaning yes, 2 meaning no, and 3 unsure. When it gets submitted to the next page I have to sort the amount of 1, 2 and 3's. Is  there and easier way other than using the if statement for every post variable  or can I use a loop statement?

 

below is what I have so far. what I want to know is there a way to get the

if ($_POST[business] == 1){
  $Yes = $Yes + 1;
}
elseif ($_POST[business] == 2){
$No = $No + 1;
}else{
   $Unsure = $Unsure + 1;
}

echo $_POST[business];
echo $_POST[performance];
echo $_POST[budget];
echo $_POST[structure];
echo $_POST[goals];
echo $_POST[customer];

Link to comment
Share on other sites

<?php
$Yes = 0;
$No = 0;
$Unsure = 0;
$fields = array('business','performance','budget','structure','goals','customer');

foreach($fields as $k)
{
	switch($_POST[$k])
	{
		case 1:
			$Yes++;
				break;
		case 2:
			$No++;
				break;
		case 3:
			$Unsure++;
				break;
	}
}
?>

 

Something like this make it a little shorter for ya??

 

Nate

Link to comment
Share on other sites

yeah, if your checkboxes/radio boxes all have the same name, add [] to the end of the name such as

<input type="radio" name="answer" value="yes">
<input type="radio" name="answer" value="no">
<input type="radio" name="answer" value="unsure">

 

the $_POST['answer'] or $_GET['answer'] variable would be passed to your script...and you can check to see what that value equals...

 

 

edit: [mis-read...]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.