1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Array quiz questions?

Discussion in 'Web Design & Development' started by namenotfound, Feb 1, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. namenotfound

    namenotfound Thread Starter

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    I have a list of questions in a quiz. With names like q[1] q[2] q[3] etc.

    Instead of having, for example:
    if (empty($_POST['q[1]']) OR empty($_POST['q[2]']) OR empty($_POST['q[3]']))
    {
    echo "go back and fill out all the questions";
    }

    I'd like to put them all in some kind of array and just have:
    if (empty($questions))
    {
    echo "go back and fill out all the questions";
    }

    (where "$questions" holds the array)
     
  2. namenotfound

    namenotfound Thread Starter

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    Also, how can I send the results to my email address?

    I tried the mail() function, but I'm getting blank emails sent.

    There's a total of 10 questions. Plus I have a field for entering your name. So I want the persons name, and answers to all 10 questions emailed to me.
     
  3. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    How many questions are they supposed to fill out, and what are the answers supposed to be (text fields, multiple choice?)
     
  4. namenotfound

    namenotfound Thread Starter

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    Their name is a text field.
    The rest are radio button multiple choice. (10 questions)
    They have to fill out all 10 questions, as well as the name field.

    For example:

    Code:
    Do you like Harry Potter?<br />
    <input name="q[1]" type="radio" value="yes" />Yes<br />
    <input name="q[1]" type="radio" value="no" />No<br />
    <br />
    
    Do you like Star Wars?<br />
    <input name="q[2]" type="radio" value="yes" />Yes<br />
    <input name="q[2]" type="radio" value="no" />No<br />
    
     
  5. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    Okay, this will tell you if fields q[1] through q[10] were answered yes/no. You can check the name field the usual way.
    PHP:
    function checkQuiz()
    {
      for(
    $i=1$i <= 10$i++)
      {
        if(
    $_POST['q'][$i] !== 'yes' && $_POST['q'][$i] !== 'no')
        {
          return 
    false;
        }
      }
      return 
    true;
    }
     
  6. namenotfound

    namenotfound Thread Starter

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    Ok, now what about the email part? How do I send their answers to my email?
     
  7. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
  8. namenotfound

    namenotfound Thread Starter

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    oh print_r I didn't think to use that :p Thanks :)

    One more thing. I have the name field show up as the "sender". When you use two names (first and last) then it shows fine. But if you just use one name it comes up in the sender column with [email protected]

    Any way to fix this?

    This is what I'm using

    $from = htmlspecialchars($_POST['name']);
    $headers = "From: {$from}\n";


    mail('[email protected]','subject', print_r($_POST['q'], true), $headers);
     
  9. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    The server decides what to do with the From header, although you shouldn't take it from user input like that, htmlspecialchars() isn't going to stop injection.
     
  10. namenotfound

    namenotfound Thread Starter

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    Ok. I'll just put the name into the body of the email them :)
     
  11. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    [tsg=yourewelcome]yourewelcome[/tsg]
     
  12. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/540284

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice