1

I want to use JSON to store the value into Mysql cuz I am making a quiz and there are many answers. But it doesn't go well. Please tell me what's wrong.THX!

<form  method="post" action="resultme.php">
     <h4>   Q1 </h4>
      <input name="a[0][]" type="radio"  value="1" /><span>Yes</span> 
      <input name="a[0][]" type="radio"  value="2" /><span>maybe</span> 
      <input name="a[0][]" type="radio"  value="3" /><span>No</span>   
     <h4>   Q2 </h4>
        <input name="a[1][]" type="radio"  value="1" /><span>Yes</span> 
         <input name="a[1][]" type="radio" value="2" /><span>Maybe</span> 
         <input name="a[1][]" type="radio" value="3" /><span>No</span> 
    <h4> Q3 </h4>
        <li class="ji">Language</li>
        <li><input name="a[2][]" type="checkbox"  value="11" /><span>English</span></li>
        <li><input name="a[2][]" type="checkbox"  value="12" /><span>Chiness</span></li>
          <li class="ji">Sport</li>
          <li><input name="a[2][]" type="checkbox" value="21" /><span>swim</span></li>
         </ul>
<input type="submit" value="click" name="submit" />

How to get the POST thing? Here is my thought below to get each answer then I can json with {} But it' no use.

$ans   =array(  "q1"=> $_POST['a[0]'],
        "q2"=> $_POST['a[1]'],
        "q3"=> $_POST['a[2]'],
            )
print_r json_encode( $ans );

1 Answer 1

0
  1. to debug, just print_r($_POST); after you submit so you will see the structure
  2. try

    $ans = array();

    foreach($_POST['a'] as $idx => $answer)

    {

    $ans['q'.($idx+1)] = $anwser;

    }

    $ans = json_encode( $ans );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.