Page 1 of 1

extracting info from $_POST

Posted: Thu Jun 24, 2010 1:21 pm
by balcoder
Hi,
I have a page where the user is adding answers to a question they have written. The page has up to four text area boxes( depending on how many answers they choose to supply to the question) and a checkbox under each answer to signify if the answer is correct or not( the questions can have one or more correct answers).
When the page is submitted I dumped out the $_POST array using print_r and this is what i get
Array
(
[0] => May
[quest] => Array ( [0] => 1 [1] => 2 [2] => 3 )
[1] => June
[2] => July
[3] => December
)

The arrays that have an integer name are the answers and the quest array holds the correct answers.
So in the above the answers are May,June,July and December, and the correct answers are 1,2 and 3.
I need to put this into a MySQL database. I have been trying various things. Here is the latest.

Code: Select all

foreach ($_POST as $value)
  {
   if (is_int(array_key($value)))
    {
     $sql = "INSERT INTO answers set
     question = '$value[0]',
     question_id ='$question_id'";
     if (!mysqli_query($link, $sql))
     {
				$error = 'Error inserting answer into answers '
        .mysqli_error($link);
				include 'error.html.php';
				exit();
		 }
      
    } 
  }
				include 'error.html.php';
				exit();  
}
Can anybody give an easier way to do this. Thanks

Re: extracting info from $_POST

Posted: Thu Jun 24, 2010 1:45 pm
by AbraCadaver
I'm not totally sure I understand, and I don't know what $question_id is, but try something like this:

Code: Select all

foreach ($_POST['quest'] as $value) {
	$sql = "INSERT INTO answers set
	question = '$value[0]',
	question_id = '$question_id'";
}
Why are you only inserting element 0?

Re: extracting info from $_POST

Posted: Thu Jun 24, 2010 1:54 pm
by ell0bo
What's your form's html code? It looks like you're not really naming your form elements correctly, which would make this easier

Code: Select all

<form>
  <input type='text' name='hello' value=''>
  <select name = 'pick'>
    <option value = '1'>First</option>
    <option value = '2'>Second</option>
  </select>
  <input type='checkbox' name='check' value='1'>
  <input type='checkbox' name='check' value='2'>
  <input type='checkbox' name='check' value='3'>
</form>
You can get back post data like this... array('hello' => 'some value', 'pick' => '1', 'check' => array(1,3))

Re: extracting info from $_POST

Posted: Thu Jun 24, 2010 2:35 pm
by balcoder
Sorry for not explaining correctly this is the form

Code: Select all

<form action="?add_ans" method="post">
  <?php
   for ($i=0; $i<$ans; $i++)
    {?>
      <div>
        <label for="answer<?php echo ($i+1); ?>" >Type Answer<?php echo ($i+1); ?> here</label>   
          <textarea id="<?php echo $i; ?>" name="<?php echo $i; ?>" rows="3" cols="40">
          </textarea>
      </div>
      <div><label for="Question<?php htmlout($i+1) ?>">
        <input type="checkbox" name="quest[]" value ="<?php htmlout($i+1); ?>"/>True
        </label>
      </div>
   <?php } ?>
   <div><input type="submit" value="submit"/></div>
   </form>
Depending on how many answers there are to the question(at least two,but can be 3 or 4)
The page displayed will have that many textareas and a checkbox below each to check if the answer is true.
When the page is submitted the $_POST has this in it if 4 answers are supplied and the fourth is the correct answer.
Array ( [0] => Answer1 [1] => Answer2 [2] => Answer3 [3] => Answer4 [quest] => Array ( [0] => 4 ) )
I want to iterate through this only extracting the Answer1, Answer2, Answer3, and Answer4 ie(the arrays that have an numeric name value [0],[1],[2] and [3])
Not the [quest] as this only holds the true answers in this case only 4 is correct.
I tried this but get errors

Code: Select all

foreach ($_POST as $value)
  {
   if (is_int(array_keys($value))) [color=#00FF00]// if the name of the associative array is a integer ie( [0],[1],[2] and [3]) insert the
values they hold into the answers table under column name answers[/color]
    {
     $sql = "INSERT INTO answers set
     question = '$value[0]',
     question_id ='$question_id'";
     if (!mysqli_query($link, $sql))
     {
				$error = 'Error inserting answer into dev1answers '
        .mysqli_error($link);
				include 'error.html.php';
				exit();
		 }
      
    } 
  }
Thanks for the help

Re: extracting info from $_POST

Posted: Thu Jun 24, 2010 2:41 pm
by AbraCadaver
It would probably be easier to rename the answer textarea:

Code: Select all

<textarea id="<?php echo $i; ?>" name="answer[]" rows="3" cols="40">
Then you can loop through the answers:

Code: Select all

foreach($_POST['answer'] as $value) {
   // do stuff
}

Re: extracting info from $_POST

Posted: Thu Jun 24, 2010 2:58 pm
by balcoder
Thanks Abracadaver
That make a lot more sense
Been hitting my head against the wall for a few hours now.

Thanks alot for the help