By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

Problem with Script

P: n/a
Here it is:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

$Number=$_POST['Number'];
$Email=$_POST['Email'];
$Number2=0;
$error=0;

$Variable1=$_POST['Variable1'];
$Variable2=$_POST['Variable2'];
$Variable3=$_POST['Variable3'];
$Variable4=$_POST['SVariable4'];
$Variable5=$_POST['Variable5'];
$Variable6=$_POST['Variable6'];
$Variable7=$_POST['IVariable7'];
$Variable8=$_POST['Variable8'];
$Check=$_POST['Check']; //We will use this to
determine if we
should do data checks

if (empty($Variable1)){
$msg1=" * You have not answered this
question.";
$error++;
}
if (empty($Variable2)){
$msg2=" * You have not answered this
question.";
$error++;
}
if (empty($Variable3)){
$msg3=" * You have not answered this
question.";
$error++;
}
if (empty($Variable4)){
$msg4=" * You have not answered this
question.";
$error++;
}
if (empty($Variable5)){
$msg5=" * You have not answered this
question.";
$error++;
}
if (empty($Variable6)){
$msg6=" * You have not answered this
question.";
$error++;
}
if (empty($Variable7)){
$msg7=" * You have not answered this
question.";
$error++;
}

$Variable9=$_POST['Variable9'];
$Variable10=$_POST['Variable10'];
$Variable11=$_POST['Variable11'];
$Variable12=$_POST['Variable12'];
$Variable13=$_POST['Variable13'];
$Variable14=$_POST['Variable14'];
$Variable15=$_POST['Variable15'];
$Variable16=$_POST['Variable16'];
$Variable17=$_POST['Variable17'];

for ($i=0; $i<$Number; $i++){
if (empty($Variable9[$i])){
$msg[i]=" * You have not answered this
question.";
$error++;
}
if (empty($Variable10[$i])){
$msg[i]=" * You have not answered this
question.";
$error++;
}
if (empty($Variable11[$i])){
$msg[i]=" * You have not answered this
question.";
$error++;
}
if (empty($Variable12[$i])){
$msg[i]=" * You have not answered this
question.";
$error++;
}
if (empty($Variable13[$i])){
$msg[i]=" * You have not answered this
question.";
$error++;
}
if (empty($Variable14[$i])){
$msg[i]=" * You have not answered this
question.";
$error++;
}
if (empty($Variable15[$i])){
$msg[i]=" * You have not answered this
question.";
$error++;
}
if (empty($Variable16[$i])){
$msg[i]=" * You have not answered this
question.";
$error++;
}
if (empty($Variable17[$i])){
$msg[i]=" * You have not answered this
question.";
$error++;
}
} //closes FOR loop

if ($error>0){
echo "<form action=\"survey.php\" method=\"POST\">
<input type=\"hidden\" name=\"Email\" value=\"$Email
\" />
<input name=\"Number\" type=\"hidden\" value=\" $Number
\" />
<table width=\"700\" border=\"0\" cellpadding=\"0\"
align=\"center
\">
<tr><td colspan=\"25\"><strong><center>PART 1 -
SURVEY</center></
strong<br/></td></tr>
<tr><td align=\"right\"></tr>
<tr><td align=\"right\"></td></tr>
<tr><td colspan=\"25\">Please answser the following
questions:<br/
><br/></td </tr>
<tr><td colspan=\"25\" bgcolor=\"#CCCCCC\">1. Question
1?</
td </tr>
<tr>
<td align=\"center\"><input name=\"Variable1\" type=
\"radio\" value=
\"Excellent\" /Excellent</td>
<td align=\"center\"><input name=\"Variable1\" type=
\"radio\" value=
\"Good\" /Good</td>
<td align=\"center\"><input name=\"Variable1\" type=
\"radio\" value=
\"Average\" /Average</td>
<td align=\"center\"><input name=\"Variable1\" type=
\"radio\" value=
\"Poor\" /Poor</td>
<td colspan=\"21\">$msg1</td></tr>
<tr><td colspan=\"25\" bgcolor=\"#CCCCCC\">2.
Question 2?</td></tr>
<tr>
<td align=\"center\"><input name=\"Variable2\" type=
\"radio\" value=
\"Excellent\" /Excellent</td>
<td align=\"center\"><input name=\"Variable2\" type=
\"radio\" value=
\"Good\" /Good</td>
<td align=\"center\"><input name=\"Variable2\" type=
\"radio\" value=
\"Average\" /Average</td>
<td align=\"center\"><input name=\"Variable2\" type=
\"radio\" value=
\"Poor\" /Poor</td>
<td colspan=\"21\">$msg2</td></tr>
<tr><td colspan=\"25\" bgcolor=\"#CCCCCC\">3.
Question 3?</td></tr>
<tr>
<td align=\"center\"><input name=\"Variable3\" type=
\"radio\" value=
\"Excellent\" /Excellent</td>
<td align=\"center\"<input name=\"Variable3\"type=
\"radio\" value=
\"Good\" /Good</td>
<td align=\"center\"><input name=\"Variable3\" type=
\"radio\" value=
\"Average\" /Average</td>
<td align=\"center\"><input name=\"Variable3\" type=
\"radio\" value=
\"Poor\" /Poor</td>
<td colspan=\"21\">$Variety</td>
</tr>
<tr><td colspan=\"25\" bgcolor=\"#CCCCCC\">4.
Question 4?</td></
tr>
<tr>
<td align=\"center\"><input name=\"Variable4\" type=
\"radio\" value=
\"Excellent\" /Excellent</td>
<td align=\"center\"><input name=\"Variable4\" type=
\"radio\" value=
\"Good\" /Good</td>
<td align=\"center\"<input name=\"Variable4\" type=
\"radio\" value=
\"Average\" /Average</td>
<td align=\"center\"><input name=\"Variable4\" type=
\"radio\" value=
\"Poor\" /Poor</td>
<td colspan=\"21\">$msg4</td>
</tr>
<tr><td colspan=\"25\" bgcolor=\"#CCCCCC\">5.
Question 5?</td></tr>
<tr>
<td align=\"center\"><input name=\"Variable5\" type=
\"radio\" value=
\"Excellent\" /Excellent</td>
<td align=\"center\"><input name=\"Variable5\" type=
\"radio\" value=
\"Good\" /Good</td>
<td align=\"center\"><input name=\"Variable5\" type=
\"radio\" value=
\"Average\" /Average</td>
<td align=\"center\"<input name=\"Variable5\" type=
\"radio\" value=
\"Poor\" /Poor</td>
<td colspan=\"21\"$msg5</td>
</tr>
<tr>
<td colspan=\"25\" bgcolor=\"#CCCCCC\">6. Question 6?
</td>
</tr>
<tr>
<td align=\"center\"><input name=\"Variable6\" type=
\"radio\" value=
\"Excellent\" /Excellent</td>
<td align=\"center\"<input name=\"Variable6\" type=
\"radio\" value=
\"Good\" /Good</td>
<td align=\"center\"><input name=\"Variable6\" type=
\"radio\" Value=
\"Average\"/Average</td>
<td align=\"center\"><input name=\"Variable6\" type=
\"radio\" value=
\"Poor\" /Poor</td>
<td colspan=\"21\">$msg6</td>
</tr>
<tr>
<td colspan=\"25\" bgcolor=\"#CCCCCC\">7. Question 7?
</td></tr>
<tr>
<td align=\"center\"><input name=\"Variable7\" type=
\"radio\" value=
\"Excellent\" /Excellent</td>
<td align=\"center\"><input name=\"Variable7\" type=
\"radio\" value=
\"Good\" /Good</td>
<td align=\"center\"><input name=\"Variable7\" type=
\"radio\" value=
\"Average\" /Average</td>
<td align=\"center\"<input name=\"Variable7\" type=
\"radio\" value=
\"Poor\" /Poor</td>
<td colspan=\"21\">$msg7</td>
</tr>
</table>
<br/
ADDITIONAL
COMMENTS: <br/><div align=\"center\"><textarea name=\"Variable8\"
cols=
\"83\" rows=\"10\"></textarea></div><div align=\"center\"><br/>
<br/>
<strong>
<center>";

if ($Number>0){
echo "<table width=\"700\" border=\"0\"
cellpadding=\"5\">
<tr><td colspan=
\"10\"><strong><center>PART 2 - SURVEY
</center</strong></td></tr><tr><td colspan=\"10\"</td></tr>
<tr><td colspan=\"10\"><div align=
\"center\">Please fill out
each section below. </div></td></tr></table><br/></center></strong>";
$SeminarNumber=1;

while ($Number2<$Number){
echo "<table width=\"700\"
border=\"0\" cellpadding=\"0\" align=
\"center\">
<tr><td colspan=\"60\" align=
\"center\"><strong>Seminar
Number
$SeminarNumber</strong></td></tr>
<tr><td align=\"right
\">Variable9: </td><td colspan=\"5\"><input
type=\"text\" name=\"Variable9[]$Number2\" value=\"\" width=\"250\"/
></td></td><td colspan=\"21\">
$msg[$Number]</td></tr>
<tr><td colspan=\"60\"</td></
tr>
<tr><td colspan=\"60\" bgcolor=
\"#CCCCCC\">1. Question 10?</td></
tr>
<tr>
<td align=\"center\"><input
name=\"Variable10]$Number2\" type=
\"radio\" value=\"Excellent\" />Excellent</td>
<td align=\"center\" ><input
name=\"Variable10[]$Number2\" type=
\"radio\" value=\"Good\" />Good</td>
<td align=\"center\" ><input
name=\"Variable10[]$Number2\" type=
\"radio\" value=\"Average\" />Average</td>
<td align=\"center\" ><input
name=\"Variable10[]$Number2\" type=
\"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\">$msg[$Number]</
td>
</tr>
<tr><td colspan=\"60\"
bgcolor=\"#CCCCCC\">2. Question 11?</td></
tr>
<tr>
<td align=\"center\"><input
name=\"Variable11[]$Number2\" type=
\"radio\" value=\"Excellent\" />Excellent</td>
<td align=\"center\"><input
name=\"Variable11[]$Number2\" type=
\"radio\" value=\"Good\" />Good</td>
<td align=\"center\"><input
name=\"Variable11[]$Number2\" type=
\"radio\" value=\"Average\" />Average</td>
<td align=\"center\"><input
name=\"Variable11[]$Number2\" type=
\"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\"$msg[$Number]
</td>
</tr>
<tr><td colspan=\"60\"
bgcolor=\"#CCCCCC\">3. Question 12?</td></
tr>
<tr>
<td align=\"center\"><input
name=\"Variable12[]$Number2\" type=
\"radio\" value=\"Excellent\" />Excellent</td>
<td align=\"center\"><input
name=\"Variable12[]$Number2\" type=
\"radio\" value=\"Good\" />Good</td>
<td align=\"center\"><input
name=\"Variable12[]$Number2\" type=
\"radio\" value=\"Average\" />Average</td>
<td align=\"center\"><input
name=\"Variable12[]$Number2\" type=
\"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\">$msg[$Number]</
td></tr>
<tr><td colspan=\"60\"
bgcolor=\"#CCCCCC\">4. Question 13
</td></tr>
<tr>
<td align=\"center\"><input
name=\"Variable13[]$Number2\" type=
\"radio\" value=\"Excellent\" />Excellent</td>
<td align=\"center\"><input
name=\"Variable13[]$Number2\" type=
\"radio\" value=\"Good\" />Good</td>
<td align=\"center\"><input
name=\"Variable13[]$Number2\" type=
\"radio\" value=\"Average\" />Average</td>
<td align=\"center\"><input
name=\"Variable13[]$Number2\" type=
\"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\">$msg[$Number]</
td>
</tr>
<tr><td colspan=\"60\"
bgcolor=\"#CCCCCC\"5.Question 14? </
td></tr>
<tr>
<td align=\"center\"><input
name=\"Variable14[]$Number2\" type=
\"radio\" value=\"Excellent\" />Excellent</td>
<td align=\"center\"><input
name=\"Variable14[]$Number2\" type=
\"radio\" value=\"Good\" />Good</td>
<td align=\"center\"><input
name=\"Variable14[]$Number2\" type=
\"radio\" value=\"Average\" />Average</td>
<td align=\"center\"><input
name=\"Variable14[]$Number2\" type=
\"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\">$msg[$Number]</
td></tr>
<tr><td colspan=\"60\"
bgcolor=\"#CCCCCC\">6.Question 15?. </td>
</tr>
<tr>
<td align=\"center\"><input
name=\"Variable15[]$Number2\" type=
\"radio\" value=\"Excellent\" />Excellent</td>
<td align=\"center\"><input
name=\"Variable15[]$Number2\" type=
\"radio\" value=\"Good\" />Good</td>
<td align=\"center\"><input
name=\"Variable15[]$Number2\" type=
\"radio\" value=\"Average\"/>Average</td>
<td align=\"center\"><input
name=\"Variable15[]$Number2\" type=
\"radio\" value=\"Poor\" />Poor</td><td colspan=\"21\">$msg[$Number]</
td></tr>
<tr><td colspan=\"60\"><hr/></
td></tr>
</table><br/>";

$Number2++;
$SeminarNumber++;
} //Closes While Loop

echo "<table width=\"700\" border=\"0\" cellpadding=\"0\" align=
\"center\">
<tr><td colspan=\"60\" bgcolor=\"#CCCCCC\">Question 16: </td>
</tr>
<tr><td colspan=\"60\" align=\"center\"><div align=\"center
\"><textarea name=\"Variable16[]\" cols=\"83\" rows=\"5\"></textarea>
</div></td></tr>
<tr><td<input type=\"hidden\" name=\"Check\" value=\"1\"</
td></tr>
<tr><td bgcolor=\"#CCCCCC\">ADDITIONAL COMMENTS: </td></tr>
<tr><td><div align=\"center\"><textarea name=\"Variable17[]\"
cols=
\"83\" rows=\"10\"></textarea></div><br/></td></tr>
</table>
<div align=\"center\"><br/>If you are interested in receiving
information or materials, please email me at <a href=
\"mailto:e...@email.com\">e...@email.com</a>.</div>
<div align=\"center\"><br/><input name=\"Submit\" type=\"Submit
\"
value=\"Submit\">
<input name=\"Reset\" type=
\"Reset\" value=\"Reset\"></div>
</form>";
} //closes the IF ($Number2>0) loop
for if the user entered a
number for seminars

} //closes the IF ($error>0) loop
else{
include('submit.php');

}
} //closes the check POST validation IF

else{
echo"You can not access this page directly. To complete the
survey,
go to
<br/><a href =\"http://www.website.com/index.php\">Survey
Form</a>";
}
?>

I tried formatting it for easy viewing, don't know if I succeeded. A
brief explanation is in order. The first page of the form asks a user
for a number and their email address. The number and email address are
passed to the second page of the form, which is the script listed
above. First thing we do is declare local variables. Next, we check
for any empty variables. If there are, $error>0 and we display the
form. If $error<=0, we call the submit.php page which writes
everything to the database.

The FOR loop in the middle determines if a customer entered a number
on the previous page. If there is a number, then it executes the next
block of code. If not, we skip this section. This way, if the customer
enters 0 or nothing for the number on the first page, then we don't
clutter up the screen with those questions. The WHILE loop right below
there, repeats certain questions for the number of times the user
entered on the first page. So there is a set of questions that is
asked once of all users, and a set of questions that is asked a number
of times, depending on what number the customer entered. The form
repeats the questions the correct number of times.

I am trying to accomplish some form of data verification. For the
first set of question, data verification works correctly, by not
allowing you to move further until every question is answered.
However, it automatically displays the error messages when the page
loads. I can't use an inline if($_SERVER['REQUEST_METHOD']=='POST')
{ echo $msg[i]; } method, since the form is always posted. Inside of
the WHILE loop, I can only get it to verify $Variable9. It doesn't
even check the other fields . Since the same question may be asked 10
times, I need to specify which occurrence of the question was not
answered. That is why I use the FOR loop.

If you know of a way to make the current code work, or if you have a
better idea of how to do this from top to bottom, please let me know.
I would like to also be able to carry over the user's selections, so
that they don't have to recheck each box, if they only left off one.

Apr 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

I've done something similar some time ago, but my structure was a bit
different (I wanted to be very flexible):
1. I used an array to have all questions saved, like:
$question[x]="Question X"; Also all other values (your radio
values) were saved as arrays!
That way you dont have to look at the whole page to change some
text! My page was in english, german and french, so I had three files
each for one language.
2. I've used functions to generate the questions, like
getQuestion($number), which returned the whole question + html string
for the buttons.

3. I've never used empty() call, only isset(); As far as I know it
wont write any warning about unset variables. dont know if empty()
does the same.
4. Dont save $Variable1=$_POST['Variable1']; ! As long as you dont
manipulate the data you dont need this!

5. Most of your html code is the same only the values change. you
could use such code:

.....html stuff...
<?php for($i=0;$i<sizeof($questions);$i++) { ?>
---- your question x, as function or embedded php ----
<?php } ?>
.....html stuff.....

6. Maybe you can also separate the html stuff completely from PHP,
with SMARTY or other template.

I hope that gives you some hints ;)

Apr 11 '07 #2

P: n/a
For the moment, the biggest question before me, is how to make it so
that the user doesn't have to recheck each line if the form fails to
verify. Here is an idea. What if upon submission, I were to exclude
any questions from the form that were already answered correctly? Such
that the first time through, it prints out all questions. Upon
submission it verifies each variable. If all the variables contain
data, then we move on to the next page. If a variable contains no
data, then we repost the question to the user. Within the "IF
($error>0)" I could wrap each occurrence of a question with an IF
statement to see if data for that variable has already been entered.
If not, then it prints the question. If so, it skips on to the next
IF.

You say not to use $Variable1=$_POST['Variable1']. But after
correcting the problem questions, how would I pass along the value for
the correct questions as well? Usually I use hidden buttons on the
form to send along values. Let's say I have three radio groups, A, B
and C. User picks an answer for A and C but skipped B. When the form
validates, and reprints question B, how would I include the value of A
and C so that when the user clicks submit for the second time, the
previous values are not over written? Does isset() maintain its status
throughout all submissions? (As far as I know, PHP can only hold one
form at a time. If you submit another form, then the values of that
form overwrite the previous form.)

Apr 11 '07 #3

P: n/a
On Apr 11, 3:38 pm, "Jerim79" <m...@hotmail.comwrote:
For the moment, the biggest question before me, is how to make it so
that the user doesn't have to recheck each line if the form fails to
verify. Here is an idea. What if upon submission, I were to exclude
any questions from the form that were already answered correctly? Such
that the first time through, it prints out all questions. Upon
submission it verifies each variable. If all the variables contain
data, then we move on to the next page. If a variable contains no
data, then we repost the question to the user. Within the "IF
($error>0)" I could wrap each occurrence of a question with an IF
statement to see if data for that variable has already been entered.
If not, then it prints the question. If so, it skips on to the next
IF.
Does isset() maintain its status
throughout all submissions? (As far as I know, PHP can only hold one
form at a time. If you submit another form, then the values of that
form overwrite the previous form.)
You're correct about having to retain the state of those variables
that were answered "correctly". You can do this like you initially
suggested, passing it back and forth from server to client each time
using $_POST or $_GET using hidden values in the form or using a
cookie on the client...but another way to do it, to save network
resources and the like, is to save it on the server. If you're using
a database, I suggest using transactions. You can "store" the
variables in the transaction until the user get's them all "correct"
and then commit the transaction. That would also give you the added
benefit of using rollbacks if the transaction fails at a later part.
Another way is to use sessions and to save those variables deemed
"correct" as a session variable. That way, the only thing that you
would need to send back and forth from the server to client would be
the session ID.

Apr 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.