Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I know I m not that good at php, but I thought I had this code for this quiz, right. But it still comes up with errors. it tells me:
Notice: Undefined index: q1 in C:\wamp\www\results1.php on line 42
Notice: Undefined index: q2 in C:\wamp\www\results1.php on line 43
Notice: Undefined index: q3 in C:\wamp\www\results1.php on line 44
Notice: Undefined index: q4 in C:\wamp\www\results1.php on line 45
Notice: Undefined index: q5 in C:\wamp\www\results1.php on line 46
Notice: Undefined index: q7 in C:\wamp\www\results1.php on line 47
Notice: Undefined variable: q7 in C:\wamp\www\results1.php on line 55
Your answers strongly indicate that you have Asthma and/or COPDHeres my php code:
<?php
$score=0;
$q1=($_POST['q1']);
$q2=($_POST['q2']);
$q3=($_POST['q3']);
$q4=($_POST['q4']);
$q5=($_POST['q5']);
$q6=($_POST['q7']);if ($q1== "40+");
$score=$score+1;
if ($q2== "Once or Twice a week");
$score=$score+1;
if ($q4== "yes");
$score=$score+1;
if ($q7== "yes");
$score=$score+1;
if ($score==4) {
echo "Your answers strongly indicate that you have Asthma and/or COPD";
}
else {
echo "Your answers strongly indicate that you have Asthma";
}?>
can any one tell me where I went wrong?
plz.

What does your form look like?
$q1 = (isset($_POST['q1']) ? $_POST['q1'] : null);
if(!is_null($q1) && $q1 === "40+") { $score++; }

A bloody mess...
<form action="results.php" method="post">
1.)<span class="style11"> How old were you when you begin to notice breathing difficulties?</span><input type="radio" name="q1" value="Younger than 12">
Younger than 12<input type="radio" name="q1" value="12-19">
12-19<input type="radio" name="q1" value="20-40"> 20-40
<input type="radio" name="q1" value="40+"> 40+</p>
2.) <strong class="style11">During the past 4 weeks, how often have you had shortness of breath?</strong><input type="radio" name="q2" value="Once or Twice a week"> Once or Twice a week
<input type="radio" name="q2" value="3 to 5 times a week"> 3 to 5 times a week
<input type="radio" name="q2" value="Once a day"> Once a day"
<input type="radio" name="q2" value="More than once a day"> More than once a day</p>
3.)<strong> Asthma</strong><input type="radio" name="q3" value="Yes"> Yes
<input type="radio" name="q3" value="No"> No</p>
4.)<strong> COPD</strong><input type="radio" name="q4" value="yes"> Yes
<input type="radio" name="q4" value="no"> No</p>
5. )<strong>A</strong><strong>llergies</strong><input type="radio" name="q5" value="yes"> Yes
<input type="radio" name="q5" value="no"> No</p>
6.) <span class="style12"><strong>Do you smoke, or have you smaoked in the past?</strong></span><input type="radio" name="q6" value="yes"> Yes
<input type="radio" name="q6" value="no"> No</p>
<input type="submit" name="submit" value="Submit Quiz"></p>
<input type="hidden" name="qp" value="quiz00.php">
</form>I don’t know if you can tell from the wreck of a code that Imp typing. but I'm trying to get it to where, depending on the answers the user chooses that it will display a different message. do you happen to know way to help me out?

Your form doesn't seem to have any default values for options, so if a user doesn't choose anything, then that may explain the invalid index error message.
What I'd do is test the post data to see if it's set or not.
<?php $score = 0; if(isset($_POST["submit"]) && $_POST["submit"] === "Submit Quiz") { $q1 = (isset($_POST["q1"]) ? $_POST["q1"] : null); $q2 = (isset($_POST["q2"]) ? $_POST["q2"] : null); // through q6 // compare available answers if(!is_null($q1)) { if($q1 === "Younger than 12") { $score += 1; // or $score++; } else if($q1 === "12-19") { $score += 1; // or $score += some value } else if(q1 === some option) { $score += some value } } else { // q1 wasn't selected } if(!is_null($q2)) { if($q2 === "Once or Twice a week") { $score += 1; // or $score++; } else if($q2 === "3 to 5 times a week") { $score += 1; // or $score += some value } else if(q2 === some option) { $score += some value } } else { // q2 wasn't selected } // continue pattern for other questions } // find out the value of $score and output the desired answer here ?>Since you've got so many options in the first few questions, you might want to use integers to represent the "value" property instead of the strings. An integer would make the testing a bit more compact.
Hope that helps.

You get this MESSAGE because the index does not exist in the array (it has not been initialized)
Note this is not an ERROR it is only a NOTICEThis will get rid of the message:
error_reporting(E_ERROR);And all that does is DISABLE notifcation messages - so only messages of ERROR severity are displayed.
If you want to really fix the problem, you need to see if the index exists before trying to access it, i.e.:
$q1 = (isset($_POST[q1]) ? $_POST[q1] : null;

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |