Jump to content

Recommended Posts

I am trying to make a simple little survey that calculates the total score and if a field is left empty, it will return to the survey. All is well except the while statement near the end. I cant get it to return to the form if a question is not answered. can someone please take a look at it and help me out. I know this may seem elementary to some but I am only starting to use php alot. Thanks!
[code]
<?php

$questions = array($_POST["Q_1"], $_POST["Q_2"], $_POST["Q_3"], $_POST["Q_4"], $_POST["Q_5"], $_POST["Q_6"], $_POST["Q_7"], $_POST["Q_8"], $_POST["Q_9"], $_POST["Q_10"],);

if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
1. True leaders are born, not made.<br />
True:<input type="radio" value="1" name="Q_1"><br />
False:<input type="radio" value="0" name="Q_1"><br />
<br />
2. If I take on a leadership role, I’ll improve my popularity.<br />
True:<input type="radio" value="1" name="Q_2"><br />
False:<input type="radio" value="0" name="Q_2"><br />
<br />
3. The very best leaders know the value of keeping a low profile.<br />
True:<input type="radio" value="1" name="Q_3"><br />
False:<input type="radio" value="0" name="Q_3"><br />
<br />
4. If you usually get along well with those in charge, you will probably be a good leader.<br />
True:<input type="radio" value="1" name="Q_4"><br />
False:<input type="radio" value="0" name="Q_4"><br />
<br />
5. The best leaders always know what to do. <br />
True:<input type="radio" value="1" name="Q_5"><br />
False:<input type="radio" value="0" name="Q_5"><br />
<br />
6. An effective leader must try to maintain a forceful personality.<br />
True:<input type="radio" value="1" name="Q_6"><br />
False:<input type="radio" value="0" name="Q_6"><br />
<br />
7. My physical appearance has little or nothing to do with my becoming a leader.<br />
True:<input type="radio" value="1" name="Q_7"><br />
False:<input type="radio" value="0" name="Q_7"><br />
<br />
8. I prefer reading fiction to nonfiction.<br />
True:<input type="radio" value="1" name="Q_8"><br />
False:<input type="radio" value="0" name="Q_8"><br />
<br />
9. I usually stick to my decision even when it is unpopular with my group.<br />
True:<input type="radio" value="1" name="Q_9"><br />
False:<input type="radio" value="0" name="Q_9"><br />
<br />
10. Being a quick decision-maker is an important trait of a good leader. <br />
True:<input type="radio" value="1" name="Q_10"><br />
False:<input type="radio" value="0" name="Q_10"><br />
<br />

<input type="submit" value="submit" name="submit">
</form>
<?
} else {
$i = 0
while ($i < 10)
{
$questions[$i] != null;
$i++;
}

$score = $Q_1 + $Q_2 + $Q_3 + $Q_4 + $Q_5 + $Q_6 + $Q_7 + $Q_8 + $Q_9 + $Q_10;
echo "Your score is ".$score.".<br />";
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/23498-help-with-simple-survey/
Share on other sites

try placeing an echo and then wrapping the html in a single quote. Like this.

[code]
<?php

$questions = array($_POST["Q_1"], $_POST["Q_2"], $_POST["Q_3"], $_POST["Q_4"], $_POST["Q_5"], $_POST["Q_6"], $_POST["Q_7"], $_POST["Q_8"], $_POST["Q_9"], $_POST["Q_10"],);

if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
echo '<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" action="' .  $PHP_SELF . '">
1. True leaders are born, not made.<br />
True:<input type="radio" value="1" name="Q_1"><br />
False:<input type="radio" value="0" name="Q_1"><br />
<br />
2. If I take on a leadership role, I’ll improve my popularity.<br />
True:<input type="radio" value="1" name="Q_2"><br />
False:<input type="radio" value="0" name="Q_2"><br />
<br />
3. The very best leaders know the value of keeping a low profile.<br />
True:<input type="radio" value="1" name="Q_3"><br />
False:<input type="radio" value="0" name="Q_3"><br />
<br />
4. If you usually get along well with those in charge, you will probably be a good leader.<br />
True:<input type="radio" value="1" name="Q_4"><br />
False:<input type="radio" value="0" name="Q_4"><br />
<br />
5. The best leaders always know what to do. <br />
True:<input type="radio" value="1" name="Q_5"><br />
False:<input type="radio" value="0" name="Q_5"><br />
<br />
6. An effective leader must try to maintain a forceful personality.<br />
True:<input type="radio" value="1" name="Q_6"><br />
False:<input type="radio" value="0" name="Q_6"><br />
<br />
7. My physical appearance has little or nothing to do with my becoming a leader.<br />
True:<input type="radio" value="1" name="Q_7"><br />
False:<input type="radio" value="0" name="Q_7"><br />
<br />
8. I prefer reading fiction to nonfiction.<br />
True:<input type="radio" value="1" name="Q_8"><br />
False:<input type="radio" value="0" name="Q_8"><br />
<br />
9. I usually stick to my decision even when it is unpopular with my group.<br />
True:<input type="radio" value="1" name="Q_9"><br />
False:<input type="radio" value="0" name="Q_9"><br />
<br />
10. Being a quick decision-maker is an important trait of a good leader. <br />
True:<input type="radio" value="1" name="Q_10"><br />
False:<input type="radio" value="0" name="Q_10"><br />
<br />

<input type="submit" value="submit" name="submit">
</form>';
} else {
$i = 0
while ($i < 10)
{
$questions[$i] != null;
$i++;
}

$score = $Q_1 + $Q_2 + $Q_3 + $Q_4 + $Q_5 + $Q_6 + $Q_7 + $Q_8 + $Q_9 + $Q_10;
echo "Your score is ".$score.".<br />";
}
?>[/code]

Hope that helps,
Tom
I tried that and it can back with the same error as before:
Parse error: parse error, unexpected T_WHILE in /home/content/H/a/l/HaloWeb/html/test.php on line 59.
I think something is wrong with the while loop. Like I said before I am fairly new to php, so please be patient. Thanks!
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.