开发者

PHP storing multiple checkbox in database

i did my 'homework' in searching for a solution to this matter开发者_如何学Python through this forum and googling, and ive tried out various ways in solving this, but i can't seem to get the value to be posted into the action file...

i understand the concept of implode / concatenating the array into a string so that it can be stored in the database, i also understand how data are parsed, but i can't seem to get this done..

PHP Form:

 <form name="questionnaire_1" action="test_index.php?action=send_questionnaire" method="post">
 <table>
 <tr class="row-a">   
 <td>
 2. Position<br />
 <i>( You may choose more than one option for this question)</i>
 </td>
<td colspan="6">
<label> <input type="checkbox" name="position[]" value="1"  /> Project Leader (External)    </label><br />
<label><input type="checkbox" name="position[]" value="2"  /> Project Leader (Internal)
</label><br />
<label><input type="checkbox" name="position[]" value="3" />Project Member (External)
</label> <br />
<label><input type="checkbox" name="position[]" value="4" /> Project Member (Internal)
</label><br />
<label> <input type="checkbox" name="position[]" value="5" />Others (Please Specify)</label>

<input name="position_specify" type="text" size="40" maxlength="40" />
</td>
</tr>
</table>

PHP Action:

if (isset($_POST['Submit'])) 
{       
$position = mysql_prep($_POST['position']);
$allPosition = implode (",", $position);
}
etc ... 

the position_specify input has no problem... only having problem with the checkbox... the database takes in varchar(255) for the position.. i really do not know how to solve this..

much help is appreciated, thank you & God bless!


Possible reference to source of mysql_prep() function: http://forums.devnetwork.net/viewtopic.php?f=1&t=81234 . Note that it ends up passing an array to mysql_real_escape_string() which doubtless converts the array to the string "Array", which is then stored in the database. As miki posted in the comments above, imploding before escaping should solve your problem.


You may have a look at bitwise operators. So in otherwords five the first checkbox the value 1 then rhe second value 2 and 3rd value 4. So count in bits. So when submitting the form add all the values together by adding all the values of the checked checkboxes. And then store ome value in the db.

To check if the checkbox was ticked do the following

If($box & 1){ echo 'checkbox 1 is checked';}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜