|
Posted by Rami Elomaa on 04/10/07 16:52
Jerim79 kirjoitti:
> Here it is:
>
> <?php
>
....A few hundred lines of code snipped...
>
> 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.
>
1) You really really need to learn how to use loops. Copy&pasting the
same code 10 times is insane.
For example:
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++;
} ........ the same thing over and over again, why not just use a loop?
This becomes one simple loop easily, but you might want to use an array
in that case:
for($i=1;$i<=8;$i++){
if (empty($Variable[$i])){
$msg[$i]=" * You have not answered this question.";
$error++;
}
}
2) TLDR, Too Long, Didn't Read. Post a conciderably shorter code sample
and someone might actually get intrested in it. Pasting 1000 lines of
codes makes you look like you have no idea what the problem is and how
to start fixing it. Instead, post only the piece of code that matters.
I'm not gonna dig thru all that spaghetti trying to find the problem,
and I think most people here wouldn't either...
--
Rami.Elomaa@gmail.com
"Olemme apinoiden planeetalla."
[Back to original message]
|