extracting info from $_POST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
balcoder
Forum Newbie
Posts: 8
Joined: Thu Jun 24, 2010 8:52 am

extracting info from $_POST

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: extracting info from $_POST

Post 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?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: extracting info from $_POST

Post 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))
balcoder
Forum Newbie
Posts: 8
Joined: Thu Jun 24, 2010 8:52 am

Re: extracting info from $_POST

Post 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
Last edited by balcoder on Thu Jun 24, 2010 2:50 pm, edited 1 time in total.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: extracting info from $_POST

Post 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
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
balcoder
Forum Newbie
Posts: 8
Joined: Thu Jun 24, 2010 8:52 am

Re: extracting info from $_POST

Post 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
Post Reply