Simple HTML Form With PHP -Help Please

Discussion in 'PHP' started by timallard, Aug 3, 2007.

  1. #1
    Hello,

    Im creating a quiz creator in html and php. I have a simple form where the user inputs data. when it gets to the answer part thats where im getting messed up.

    i have 3 input boxes, 1 for answer 1, one for answer 2, and one for answer three.

    how can i have it talk to the php to know which one is the correct answer?
    would i need to put a check box next to each field to check off the correct one?

    if so how would i create my if statement.

    i need it like this (im formatting it in xml)
    so if the user placed text in the correct answer box, it would display like below

    <answer correct="y">Blue</answer>

    otherwise it would look like

    <answer>blue</answer>

    thanks for the help.
     
    timallard, Aug 3, 2007 IP
  2. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #2
    Do you want the checking to be done in real time as the data is put in, or do you want it checked when the user submits the form?
     
    BRUm, Aug 3, 2007 IP
  3. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #3
    I don't know anything about xml, but as you need help in php, you can do that with if statements and $_POST:

    <?php
    if ($_POST['answer1']=='correctvalue'){
       //correct answer
    }else{
       //uncorrect answer
    }
    ?>
    PHP:
    As I said, I know nothing about XML, but if you are only formatting it (inside a .php page), you can do it this way:

    <?php
    if ($_POST['answer1']=='correctvalue'){
       $add=' correct="y"';
    }else{
       $add="";
    }
    
    echo '<answer'.$add.'>'.$_POST['answer1'].'</answer'>;
    ?>
    PHP:
    What it does: if it is correct it adds correct=y to the answer tag, otherwise it doesn't.

    hope that helps :)
     
    SedNaX, Aug 4, 2007 IP
    timallard likes this.
  4. nagasharmi

    nagasharmi Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    three check box names are equal
    but values are different
    you can click only one check at a time
    you get value from that name
     
    nagasharmi, Aug 4, 2007 IP
  5. SedNaX

    SedNaX Active Member

    Messages:
    1,326
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #5
    those are radio buttons, not checkboxes ;)

    <input type=radio name=quiz value=Answer1>Answer1<br>
    <input type=radio name=quiz value=Answer2>Answer2<br>
    <input type=radio name=quiz value=Answer3>Answer3
     
    SedNaX, Aug 4, 2007 IP
  6. jemy

    jemy Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    go here and download rar named htmlphpmailform.rar
     
    jemy, Aug 4, 2007 IP
  7. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #7
    thanks for everythign guys, ill try some things out then get back if i need anything :)
     
    timallard, Aug 4, 2007 IP
  8. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #8
    Im getting there!

    ok heres whats happening:

    i have 3 radio buttons in the same group:
    <input name="correct" type="radio" id="radio" value="correctAnswer1">
    <input name="correct" type="radio" id="radio" value="correctAnswer2">
    <input name="correct" type="radio" id="radio" value="correctAnswer3">

    with php code:

    $correct = $_POST['correct'];

    if ($_POST['correct'] == 'correctAnswer1') {
    //Correct Answer
    $add =' correct="y"';
    }else{
    //Wrong Answer
    $add="";
    }

    my echo is:
    echo ("&lt;" . "answer" . $add . "&gt;" . "&lt;" . "![CDATA[" . $txtAnswer1 . "]]&gt;" . "&lt;" . "/" . "answer" . "&gt;");

    and my output is:
    <answer correct="y"><![CDATA[Blue]]></answer>
    <answer correct="y"><![CDATA[Red]]></answer>
    <answer correct="y"><![CDATA[Green]]></answer>

    so when i fill in 1 radio button on the form,..they are all getting a value of "correct=y" and not just the specific one clicked...

    any help would be greatly appreciated!!

    Thanks, -Tim
     
    timallard, Aug 8, 2007 IP
  9. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #9
    ok i figured it out, i duplicated the if statement 3 times, 1 for each answer, and now it works appropriately, thanks!
     
    timallard, Aug 8, 2007 IP