I am working on a project that signs up students to specific classes("french", "math",...). The student id is already selected. True a while loop i display my classes out of my database with a checkbox in front of each class. I only wanna post te checked checkboxes in a form that will send it to a php file thats puts the student and his selected classes into the database. But i cant seem to solve how i only post the checked checkboxes because of the difficulty with the while loop. I hope someone can help me. Already thanks!
2条回答 默认 最新
duanhuilao0787 2019-03-25 12:41关注The best way would probably be to alter your HTML to POST the classes as an array instead of individual form elements.
<checkbox name="classes[]" value="CLASS_ID_1"> <checkbox name="classes[]" value="CLASS_ID_2"> <checkbox name="classes[]" value="CLASS_ID_3"> <checkbox name="classes[]" value="CLASS_ID_4">Then, the PHP side of this is very easy. Only the checked checkbox values will appear on the processing page.
So, let's say that someone checks
CLASS_ID_1,CLASS_ID_3andCLASS_ID_4;print_r($_POST['classes']);The above code should return;
array ( "CLASS_ID_1", "CLASS_ID_3", "CLASS_ID_4" )So, essentially, it is an array of checked values. This means you do not need the class name or any additional information or loops to process the data.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报