Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   PHP - quiz - why is my code not working? (http://www.programmingforums.org/showthread.php?t=13852)

Kelvoron Aug 26th, 2007 2:42 AM

PHP - quiz - why is my code not working?
 
Hello,

I am writing a quiz for a Harry Potter role playing site i belong to. I figured that if i made a verriable called score and add 1 to it for each correct answer it would work, but for some reason it is not.

Here is my code:

:


<HTML>
  <Head>
      <Title>
          Interface
      </Title>
  </Head>

<Body>


<Div align="center">
  Welcome To the Transfigurations Midterm
      <BR>
  Proffesor: Damion Black
      <BR>
</Div>



<Div Align="center">
  <Form method="post">
      Name:
        <input name="Name" type="text">
            <br>



        <li>
        What is blah
            <BR>
        <input name="question1" value="a" type="radio">
              Answer 1
            <BR>
        <input name="question1" value="b" type="radio">
              Answer 2
        </li>



        <li>
        what is mah
            <BR>
        <input name="question2" value="a" type="radio">
              Answer 1
            <BR>
        <input name="question2" value="b" type="radio">
              Answer 2
        </li>

            <BR>         
        <input type="submit" value="submit">
            <br />
  </form>


<?
  $score=0;
     
    if(question1 == "a")
      {
          $score=$score+1;
      }
    if(question2 == "b")
      {
          $score=$score+1;
      }

?>


</Div>

</Body>

</HTML>


- I have also tryed taking out the PHP and puting it in its own file and using the form action as that file.
- I have also tryd making the button action the php file.
-I have tryed making it $score=score++ , $score+1, and $score++

none of these have worked, I know this should be simple to pull off but i can not figure it out.

xavier Aug 26th, 2007 3:59 AM

how about this ? -
:

  1. <?
  2.   $score=0;
  3.  
  4.     if($_POST["question1"] == "a")
  5.       {
  6.           $score=$score+1;
  7.       }
  8.     if($_POST["question2"] == "b")
  9.       {
  10.           $score=$score+1;
  11.       }
  12.  
  13. ?>


read some PHP tutorials FIRST ... ask questions later.

and i think : <input name="question1" value="a" type="radio"> should be
<input name="question1[]" value="a" type="radio"> but i;m not sure

dr.p Aug 26th, 2007 4:01 AM

You should probably have the score checking in a PHP script which your quiz form submits to. And in that score checking script, you should be checking $_GET['question1'] or $_POST['question1'], depending on your form submission method.

example form page:
:

<form action="grade.php" method="POST">
 ...
</form>


example score script:
:

<?php
  $score = 0;
  if ($_POST['question1'] == 'a')
    ++$score;
  if ($_POST['question2'] == 'a')
    ++$score;
?>

YOUR SCORE IS: <? echo $score ?>


Kelvoron Aug 26th, 2007 5:00 AM

Quote:

Originally Posted by xavier (Post 132850)
and i think : <input name="question1" value="a" type="radio"> should be
<input name="question1[]" value="a" type="radio"> but i;m not sure

no you can not use an array in HTML (the form itself is HTML the driver is PHP)

thanks for the help both of you i apreciate it greatly

PhilBon Aug 26th, 2007 3:58 PM

Are you ever going back to the page and the score getting reset? is that what you are trying to handle? If you are then you need to create a hidden post value of their score and when you reload the page, you have to put it in.

mattireland Aug 27th, 2007 5:25 AM

I did a quiz like this on Latin nouns once. I'll go downstairs and try and find the DVD with it on and then you can have a look at that if you want.

Kelvoron Aug 27th, 2007 5:29 AM

thanks a lot matt. I got it to work though DR.p's sugestion worked. i should have posted that but thanks for offering ^_^

mattireland Aug 27th, 2007 7:14 AM

OK. If you need any more help just say...


All times are GMT -5. The time now is 11:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC