...

View Full Version : php form handling question:



WRStrong
03-04-2009, 06:36 PM
I have a simple php script that creates a form consisting of radio buttons that are dynamically created based upon the configuration settings, etc.. My problem is to figure out how to call $_POST for this array of buttons when I might not know how many will be used due to the config settings.

For example:


<? include 'config.php'; ?>
<?
// Open the data file
$file = fopen($file_name, "r") or die("Unable to read file" . $file_name . "!");
// Write contents of file into variables
$question = file($file_name);
?>
<html>
<head>
<title></title>
</head>
<body>
<center>
<form method="post" action="send_form.php">
<table>
<tr>
<td align=center colspan=3><b><font size=+2><? echo $feedback_program_title; ?></font></b></td>
</tr>
<tr>
<td colspan=2>
<hr>
</td>
</tr>
<tr>
<td colspan=2>
<center>
<b><u>Please fill in our feedback survey form!</u></b>
</center>
</td>
</tr>
<tr>
<td colspan=3>
<hr>
</td>
</tr>
<?
$n = 0;
while ($question[$n] !="")
{
print ("<tr>");
print ("<td>");
print $n+1;
print ("</td>");
print ("<td>");
print $question[$n];
print ("</td>");
print ("</tr>");
print ("<tr>");
print ("<td colspan=2>");
print $lowest_rating_title;

for ( $k = 1; $k <= $scale_steps; $k ++)
{
?><input name=answer<? echo $n+1; ?> type=radio unchecked value=<? echo $k;?>><?
}

print $highest_rating_title;
print ("</td>");
print ("</tr>");
print ("<tr colspan=2>");
print ("<td>&nbsp;");
print ("</td>");
print ("</tr>");
$n = $n + 1;
}
?>
<tr>
<td colspan=3>
<hr>
</td>
</tr>
<tr>
<td colspan=3>
Email Address:&nbsp;&nbsp;<input name=email type=text size=40>
</td>
</tr>
<tr>
<td colspan=3>
<hr>
</td>
</tr>
<tr>
<td colspan=3>
<center>
<input name=submit type=submit value=send><input name=reset type=reset value=reset>
</center>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>


In the config.php:



<?

$admin_email = "admin@email.com"; // Email address who's going to be sending the newsletter!
$feedback_program_title = "My Feedback Manager Title"; // Title for your actual newsletter script!
$site = "http://www.yoursite.com"; // URL of your homepage!

$lowest_rating_title = "Lowest Rating Title"; // Rating scale's lowest end title.
$highest_rating_title = "Highest Rating Title"; // Rating scale's highest end title.

$scale_steps = 5; // Rating scale steps.

// Do not edit below this line unless you know what you are doing!
$file_name = "entries.txt";
$file_name2 = "responses.txt";

$today = date('m/d/Y - h:i:s A e');

?>


Will create the form itself. Notice that the quantity of questions are directly related to the number in the config. Also notice that the number of feedback steps (radio buttons) are directly related to the config. The form works flawlessly (even when I mess with the config heavily.) I need to handle the data that comes from the form.

Do you know or could you point me in the right direction?

I know how to pass the form info (email, etc...,) it's just the stinking array of unknown number of items that's got me stumped.

Any help you can give me would be greatly appreciated.

Thanks,

Bill Strong

Fumigator
03-04-2009, 09:14 PM
If you change the names of the radio buttons to an array instead of tacking the number onto the end of the variable name:



<input name=answer[<? echo $n+1; ?>] type=radio unchecked value=<? echo $k;?>>


Now you can foreach() through all the radio buttons in one loop.



foreach ($_POST['answer'] as $aVal) {
//do what you need to do with $aVal which is each radio button
}

WRStrong
03-04-2009, 11:10 PM
I am familiar how to use foreach and display the actual buttons, etc.. How do I do it in the $_POST section, though? And how do I make the proper references to the info so that it can be utilized in the page that the form submits the info to? Can you please be a little more specific?

Fumigator
03-05-2009, 03:43 PM
It's an array in the $_POST array. Again look at the code I posted, it's very specific how you can process the radio button values in $_POST. Do this at the top of the script you submit the form to:



echo "<pre>".print_r($_POST,true)."</pre>";


You will be able to see the structure of your $_POST array, which should give you some idea of how to handle $_POST.

WRStrong
03-06-2009, 04:14 PM
I have changed send_form.php to the following:


<?

include"config.php";

$email = $_GET['email'];
echo $email;

$num = count(file($file_name));
$k=0;
while($num !="")
{

$answer[$k] = $_GET['answer['$k']'];

echo $answer[$k];
$k = $k + 1;
}

echo "<hr>";

echo "<pre>".print_r($_POST,true)."</pre>";

?>


While, using your last line:

echo "<pre>".print_r($_POST,true)."</pre>";
I am able to see the info; however, while attempting to put each answer into a variable for handling, I get an error:

Parse error: syntax error, unexpected T_VARIABLE, expecting ']' in send_form.php on line 13

I guess I'm still missing something. The parts that confuse me ar the $aVal that you suggested (what is that for?) and the ($_POST,true) part. (The best I can understand about that is that it displays all true (filled in) fields in the form. Am I right, there?

If the $_POST function pulls the info from the submitted form, how do I put each into a variable so that I can handle it properly?

When I look more closely at the output, I am trying to get the output to be something like (email), (Each question number in order) (radio'd positive answer). Like if I answered 3 for the first question, 4 for the second and 1 for the third, the output would be something like, "my_email@my_domain.com, 1=3, 2=4, 3=1" (if that helps.)

Thanks for all the assistance thus far! It's been a real, 'head scratcher' for me for a while now...lol

Fumigator
03-06-2009, 06:09 PM
My suggestion to include echo "<pre>".print_r($_POST,true)."</pre>"; is only a way to show you what is in the $_POST array. It is not intended as a final solution for your script. As you dive into what the $_POST array has in it, you should be able to see the structure, and how it can be walked through using foreach(). If you are still scratching your head, please show us what is in your $_POST array and I'll try to explain it better.

FYI, $_POST is not a function; it is a super global array of values from your HTML form that PHP creates for your convenience.

One more FYI: The error you mention, "unexpected variable", is probably because you are embedding a complex variable (such as an array element) inside double quotes. make sure you enclose such a variable in squiggly brackets, or break it outside the double quotes.



//example use of squigglies:
$myString = "value of my array: {$_POST['btn'][$i]} and more to it";
//or break it out
$myString = "value of my array: " . $_POST['btn'][$i] . " and more to it";

WRStrong
03-09-2009, 05:46 PM
Thanks Fumigator! Your tips aimed me in the right direction! I'm pasting the send_form.php in case anyone else is wanting to do the same thing.



<?

include"config.php";

$email = $_POST['email']; // Get email address from submitted form.

$k=0;
foreach ($_POST['answer'] as $aVal) // Get all responses from submitted form.
{
$answer[$k] = $aVal;
$k = $k + 1;

}
$k = 0;

$question = file($file_name); // Get all questions from data file.

?>
<center>
<table>
<tr>
<td colspan=3><b>Submission from:</b>&nbsp;&nbsp;<i>
<?
echo $email; // Create the display table.
?>
</i>
</td>
</tr>
<tr>
<td colspan=3><hr></td>
</tr>
<tr>
<td align=center><b>Number</b></td>
<td align=center><b>Question</b></td>
<td align=center><b>Response</b></td>
</tr>
<tr>
<td colspan=3><hr></td>
</tr>
<?
$mail_subject = $feedback_program_title . " response!"; // Set the subject of the email.

$mail_body = $email . " has just submitted a response to your feedback form " . $feedback_program_title . "!\n\r";

while ($answer[$k] !="") // Drop all questions and answers into the table.
{
?>
<tr>
<td align=center><? echo $k; ?></td>
<td align=left><? echo $question[$k]; ?></td>
<td align=center><? echo $answer[$k]; ?> of <? echo $scale_steps; ?></td>
</tr>

<?
// Add questions and answers to the email body.

$mail_body .= "Question # " . $k . ": " . $question[$k] . " - " . "Answer: " . $answer[$k] . "\n\r";
$k = $k + 1;
}
?>

<?
// Send responses to the admin.

mail($admin_email, $feedback_program_title . " response!", $mail_body, "From: " . $email);

// Modify questions and answers to be sent to the user.

$mail_body2 = "You have just submitted a response to the " . $feedback_program_title . " . Below is a copy of your submission, for your records:\n\r";
$mail_body2 .= $mail_body;

// Send copy of the responses to the user.

mail($email, $feedback_program_title . " response!", $mail_body2, "From: " . $admin_email);
?>
<tr>
<td colspan=3><hr></td>
</tr>
<tr>
<td colspan=3 align=center><b>Thank you for your feedback!</b><br><br>
<font size=-1>This information will help us make our product better for you.</font><br><br>
<font size=-1>These responses have just been sent to the site administrator.</font><br><br>
You also have received a copy via the email<br>address (<i><? echo $email; ?></i>) you supplied us.
</td>
</tr>
<tr>
<td colspan=3><hr></td>
</tr>
</table>
</center>


I also added the feature to send the submission, is a pretty handy feature.

Thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum