1

I have a problem inserting the values from checkboxes in an html document into a database as a comma separated string (I know this is usually considered being bad style but in this case that is what is required).

I have an code snippet of the html file here

<div id="page16" class="page">
            <p style="font-weight: bold; width: 55%;"><a id="q16_qtext">*</a></p>
            <p style="font-style: italic;">Vælg venligst de (højst) 3 primære årsager (i alt).</p>
            <p style="font-style: italic;">Af økonomiske årsager:</p>
            <input type="checkbox" name="q16[]" value="1" id="num161">For dyrt i forhold til udbyttet for eleverne<br>
            <input type="checkbox" name="q16[]" value="2" id="num162">For dyrt i forhold til andre omkostninger (bøger, efteruddannelse af lærere osv.)<br>
            <input type="checkbox" name="q16[]" value="4" id="num164">Andre økonomiske årsager<br>
            <p style="font-style: italic;">Andre årsager:</p>
            <input type="checkbox" name="q16[]" value="5" id="num165">Pga. skolesammenlægning eller en anden større ændring<br>
            <input type="checkbox" name="q16[]" value="6" id="num166">Skolen havde/har ikke bemanding til det<br>
            <input type="checkbox" name="q16[]" value="7" id="num167">Der var for tidskrævende i forhold til udbyttet (for eleverne i forhold til pensum)<br>
            <input type="checkbox" name="q16[]" value="8" id="num168">Der var ikke længere opbakning til det i skoleledelsen (ikke økonomisk)<br>
            <input type="checkbox" name="q16[]" value="9" id="num169">Der var ikke længere opbakning til det blandt lærerne<br>
            <input type="checkbox" name="q16[]" value="10" id="num1610">Der var ikke længere opbakning til det blandt elever/forældre<br>
            <input type="checkbox" name="q16[]" value="11" id="num1611">Andet.&nbsp;&nbsp;&nbsp;&nbsp; Angiv i så fald primært hvad: &nbsp;&nbsp;<a style="text-align: center;"></a><input type="text" name="q16extra" value="" id="num89"><br>
            <input type="checkbox" name="q16[]" value="12" id="num1612">Ved ikke<br>
            <p style="bottom:5px;"><input style="width: 150px; height: 30px;" type="button" class="all" id="B19" value="tilbage" onClick="showPreviousLayer()"><input style="width: 150px; height: 30px;" type="button" class="all" id="C20" value="fortsæt" onClick="checkAnswersSixteen()"></p>    
        </div>

Then I connect to a database and use this piece of php code to create an array of the values

$q16 = implode(",",$_POST['q16[]']);

And then insert into "table" with the columns and values. There are of course many other columns and values to be inserted and these work! so the problem seems not to be with the insert statement but with the implode method perhaps.

i tried doing an echo($q16); but that seems just to be empty.

Anyone who knows what the problem might be? please let me know if you need any other information :)

2 Answers 2

2

I suggest you to try to use just $_POST['q16'] as it's being converted into array

$q16 = implode(",",$_POST['q16']);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! that solved the problem indeed. So the [] extra in the html names is not at all part of the name it is just a indicator for the array?
@PNS You are right. [] is not part of the name. But it shows that the q16 variable should be treated and recognized as an array in PHP.
0

Keep in mind that only checked inputs are passed on. So if I check the 3 first boxes and the last one, the output of my example file will be:

Array
(
    [q16] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 4
            [3] => 12
        )

    [q16extra] => e
)
----1,2,4,12

And here is the example file:

   <div id="page16" class="page">
   <p style="font-weight: bold; width: 55%;"><a id="q16_qtext">*</a></p>
   <p style="font-style: italic;">Vælg venligst de (højst) 3 primære årsager (i alt).</p>
   <p style="font-style: italic;">Af økonomiske årsager:</p>
   <form action="form.php" method="post">
   <input type="checkbox" name="q16[]" value="1" id="num161">For dyrt i forhold til udbyttet for eleverne<br>
   <input type="checkbox" name="q16[]" value="2" id="num162">For dyrt i forhold til andre omkostninger (bøger, efteruddannelse af læ   rere osv.)<br>
   <input type="checkbox" name="q16[]" value="4" id="num164">Andre økonomiske årsager<br>
   <p style="font-style: italic;">Andre årsager:</p>
   <input type="checkbox" name="q16[]" value="5" id="num165">Pga. skolesammenlægning eller en anden større ændring<br>
   <input type="checkbox" name="q16[]" value="6" id="num166">Skolen havde/har ikke bemanding til det<br>
   <input type="checkbox" name="q16[]" value="7" id="num167">Der var for tidskrævende i forhold til udbyttet (for eleverne i forhold    til pensum)<br>
   <input type="checkbox" name="q16[]" value="8" id="num168">Der var ikke længere opbakning til det i skoleledelsen (ikke økonomisk)   <br>
   <input type="checkbox" name="q16[]" value="9" id="num169">Der var ikke længere opbakning til det blandt lærerne<br>
   <input type="checkbox" name="q16[]" value="10" id="num1610">Der var ikke længere opbakning til det blandt elever/forældre<br>
   <input type="checkbox" name="q16[]" value="11" id="num1611">Andet.&nbsp;&nbsp;&nbsp;&nbsp; Angiv i så fald primært hvad: &nbsp;&n   bsp;<a style="text-align: center;"></a>
   <input type="text" name="q16extra" value="e" id="num89"><br>
   <input type="checkbox" name="q16[]" value="12" id="num1612">Ved ikke<br>
   <p style="bottom:5px;">
       <input style="width: 150px; height: 30px;" type="button" class="all" id="B19" value="tilbage" onClick="showPreviousLayer()">

   <input style="width: 150px; height: 30px;" type="submit" class="all" id="C20" value="fortsæt" onClick="checkAnswersSixteen()"></p   >
   </form>
   </div>


   <?php
   print '<pre>';
   print_r($_POST);
   print '----';
   print_r( implode(",", $_POST['q16']) );
   print '</pre>';
   ?>

The difference lies in the element and the php - print statements at the end.

Comments

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.