Jump to content

Archived

This topic is now archived and is closed to further replies.

bassplaya4string

Help with simple survey

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]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
You have no semicolon after [code=php:0]$i = 0[/code] in your while loop.

Regards
Huggie

Share this post


Link to post
Share on other sites
To be hornest i cant help you with your own code
However if you see this (i'm a fan of this guy's scripts)

[url=http://www.dhtmlgoodies.com/scripts/ajax-poller/ajax-poller.html]Poll script[/url]

Share this post


Link to post
Share on other sites

×

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.