Jump to content


Photo

Help with simple survey


  • Please log in to reply
5 replies to this topic

#1 bassplaya4string

bassplaya4string
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 10 October 2006 - 04:38 AM

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!
<?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 />";
}
?>


#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 10 October 2006 - 05:02 AM

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

<?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 />";
}
?>

Hope that helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 bassplaya4string

bassplaya4string
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 10 October 2006 - 05:15 AM

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!

#4 php_coder_dvo

php_coder_dvo
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationPhilippines

Posted 10 October 2006 - 05:36 AM

why you dont use javascript for validating..

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 10 October 2006 - 09:37 AM

You have no semicolon after
$i = 0
in your while loop.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 whitehat

whitehat
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 11 October 2006 - 09:57 PM

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)

Poll script




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users