I'm trying to post certain textboxes that have checkboxes with them. My code works when consecutive checkboxes are checked, it echoes "checkbox value 1 & text area value 1", "checkbox value 2 & text area value 2" and so on. However when I only check checkboxes 1 and 3, the code only echoes the values for checkbox 1 and textarea. How do I make it that I can post only those that their checkbox checked?
Currently, what I'm doing is having all checkboxes and text areas have the same name, literally. All checkboxes have the same name as all textareas. I'm also putting all these values in an array in my php. So that the first checkbox would be in the 0 place of the array and the text area corresponding it at 1, then the second checkbox at 2 and its corresponding textbox at 3, and so on. I'm using while loops for how many checkboxes are there, foreach for array values (not so sure what really this is for), and an if statement if the checkbox value in the array is checked or not.
When I only check the first and third checkbox the first checkbox is "checked" but as the loop goes on it says that the third checkbox is unchecked, using the array.
HTML Code
<form method="post">
<div class="form-group col-sm-4">
<label><input type="checkbox" name="rel_crossed_eyes[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes[]"/>
</div>
<div class="form-group col-sm-4">
<label><input type="checkbox" name="rel_crossed_eyes[]" value="Crossed Eyes" /> Crossed Eyes1</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes[]"/>
</div>
<div class="form-group col-sm-4">
<label><input type="checkbox" name="rel_crossed_eyes[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes[]"/>
</div>
<div class="form-group col-sm-4">
<label><input type="checkbox" name="rel_crossed_eyes[]" value="Crossed Eyes" /> Crossed Eyes1</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes[]"/>
</div>
<div>
<input type="submit" name="submit"/>
</div>
</form>
PHP Code
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "main";
$conn=mysqli_connect($servername, $username, $password);
if (!$conn){
die("Database Connection Failed" . mysqli_error($conn));
}
$select_db = mysqli_select_db($conn, $dbname);
if (!$select_db){
die("Database Selection Failed" . mysqli_error($conn));
}
$result = mysqli_query($conn,"SELECT * FROM inventory");
$numbers=("SELECT count(*) as total FROM inventory");
$result=$conn->query($numbers);
$row= $result->fetch_assoc();
echo $row['total'];
$conditions = array("rel_crossed_eyes" => "Crossed Eyes");
if (isset($_POST['submit'])){
$counted = 0;
$counteds = 0;
while ($row['total'] > $counted) {
foreach ($conditions as $key => $value){
if ($_POST[$key][$counteds] == $value){//if the checkbox is checked
echo $_POST[$key][$counteds]." ". $_POST[$key][$counteds+1];
}else{
echo "mali";
}
$counted++;
$counteds += 2;
}
}
}
?>
Aucun commentaire:
Enregistrer un commentaire