#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3

    Question And Answer submit


    Hi! I have a page with outputs questions from a database, and the user can select an answer..

    all the answers are collected in a $answers array which is sent to the next page like this

    Code:
    <select name="answers[]"> ... </select>
    Now how can i also send the Questions array to the next page..
    a hidden input field cannot hold an array that easy and if i use a while loop to create many hidden fields, they all have a different name.

    Anyone that can help me out please!
    thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    They will not have different names if you create the names in the loop as well.
    PHP Code:
    echo "<select name=\"questions[$i]\">...</select>"
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Hm i don't understand. The questions are echo'ed by php as plain text. But i need to send the id of the questions to the POST
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    You can include the ID in the form's post method:
    Code:
    <form method="POST" action="checkAnswer.php?question=<?php echo $questionId; ?>">
    You can also use the session.

    You can also name the questions array with the answer ID:
    Code:
    <select name="answers[<?php echo $questionId; ?>][]"> ... </select>
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by ManiacDan
    You can include the ID in the form's post method:
    Code:
    <form method="POST" action="checkAnswer.php?question=<?php echo $questionId; ?>">
    You can also use the session.

    You can also name the questions array with the answer ID:
    Code:
    <select name="answers[<?php echo $questionId; ?>][]"> ... </select>
    I tried the second idea and it's just outputting the $row['vraag_id']

    Code:
    <select name="antwoord[ <?php echo $row['vraag_id'] ?> ] [] ">
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,497
    Rep Power
    594
    If that is the case then you probably have quotes messed up somewhere. It is treating the $row variable as a literal string. Post all your code and use [ PHP ] tags. See the sticky at the top of this forum.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by gw1500se
    If that is the case then you probably have quotes messed up somewhere. It is treating the $row variable as a literal string. Post all your code and use [ PHP ] tags. See the sticky at the top of this forum.
    This is the PHP.

    PHP Code:
    <select name="antwoord[ <?php echo $row['vraag_id'?> ] [] ">
    And this is how i'm trying to test-fetch it

    PHP Code:
    $antwoorden $_POST['antwoord'];
    print_r($antwoorden); 
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    What is the resulting HTML for this PHP output?

    What's the result of print_r($_POST)?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by ManiacDan
    What is the resulting HTML for this PHP output?

    What's the result of print_r($_POST)?
    Thanks for your help! I managed to get to this

    PHP Code:
    echo '<select name="antwoord['.$vraagid.'][]">'
    Which results in this
    Code:
    Array ( [antwoord] => Array ( [135] => Array ( [0] => 6 ) [137] => Array ( [0] => 0 ) ) )
    But how can i get this result to show like this
    Code:
    [135] => 6  [137] => 0
    Thanks!
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    by leaving out the "[]" at the end of the input name. It's useless, anyway.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by Jacques1
    Hi,

    by leaving out the "[]" at the end of the input name. It's useless, anyway.
    It's fixed! Thanks a lot for the help!

    Using

    PHP Code:
    foreach($_POST['antwoord'] as $vraag => $antwoord){ ... } 

IMN logo majestic logo threadwatch logo seochat tools logo