samedi 4 mai 2019

How to post multiple fields values when clicking over a checkbox

I am working in a small project and I have a code to do a multiple post of fields values when clicking over a check box. I have a table which lists classes and time. I need to know how to click over a checkbox and send class and time values at same time using a loop.

This is driving me crazy. I have spent hours and I can't figure out how to send multiple values at same time. When I check all the values, it works perfect but if I click one or two values, I always get a wrong time value shown.

Here is the PHP code, I am using:

if(isset($_POST['submit'])){
if((!empty($_POST['class'])) && (!empty($_POST['time']))) {

$checked_count = count($_POST['class']);
echo "You have selected following ".$checked_count." option(s): <br/>";

// Loop to store and display values of individual checked checkbox.

for ($i=0; $i < $checked_count; $i++) {
echo $_POST['class'][$i]. " ";
echo $_POST['time'][$i]. "<br>";
}
}
}
else{
echo "<b>Please Select At least One Option.</b>";
}

Here is my HTML CODE:

<form action="checkclass.php" method="post">

<label class="heading">Select Your Technical Exposure:</label><br />
<tr>
<td><input type="checkbox" name="class[]" value="C/C++"><label>C/C++</label></td>
<td><input type="text" name="time[]" value="1"  width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="Java"><label>Java</label></td>
<td><input type="text" name="time[]" value="2"  width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="PHP"><label>PHP</label></td>
<td><input type="text" name="time[]" value="3" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="HTML/CSS"><label>HTML/CSS</label></td>
<td><input type="text" name="time[]" value="2"  width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="LINUX"><label>UNIX/LINUX</label></td>
<td><input type="text" name="time[]" value="3"  width="15"><br /></td>
</tr>

<input type="submit" name="submit" Value="Submit"/>


</form>

How do I relate time and class values so that whole values in a row are posted at same time. I really want to do a multiple insert but I think solving this will help me.

I will appreciate any help with this matter.




Aucun commentaire:

Enregistrer un commentaire