dimanche 10 décembre 2017

Building a php form array with dropdown and checkboxes to post to table

Thanks all! I'm new to PHP and MySQL as well as this being my first post here. Please bear with me. I am working on building a searchable database of vendors and categories. Both will be many-to-many relationships.

I have already build forms to add my vendors as v_id, vendor(name), website and category as c_id and category(name). Entries post correctly to tables vendor or category correctly. v_id and c_id auto-increment automatically as primary key. With that and my research so far I have been able to build a form that will dynamically create a dropdown of vendors and checkboxes of all the categories. from this, my goal is to select 1 vendor, then check all categories related to the vendor and submit the results building an array (I think) and then using that array post to a third table that I will use for searches.

My question is: how do I build an array using a single vendor id and all category ids selected, and then post to my table? Each row posted would have the same v_id but a different c_id. Here is the working code I have so far. It build the form elements correctly but is as far as I have gotten.

<form class="form-horizonal">
<?php

include 'dbconnect.php';

$sql1 = "SELECT * FROM vendor";
$sql2 = "SELECT * FROM category";

$result1 = mysqli_query($conn,$sql1);
$result2 = mysqli_query($conn,$sql2);

$count1 = mysqli_num_rows($result1);
$count2 = mysqli_num_rows($result2);
echo "<div class=\"col-sm-3\">";
if (mysqli_num_rows($result1)) {
    # code...
    echo "<h4><span class=\"badge\">1</span> Select Vendor</h4>";
    $select = '<select class="form-control" name="select">';
    while ($rs=mysqli_fetch_array($result1)) {
        # code...
        $select.='<option value="'.$rs['v_id'].'">'.$rs['vendor'].'</option>';
    }
}
$select.='</select>';
echo $select;
echo "</div><div class=\"col-sm-6\"><h4><span class=\"badge\">2</span> Select <strong>ALL</strong> Categories</h4>";
$cb = "SELECT category,c_id FROM category";
$result_db = $conn->query($cb);
if (!$result_db) {
    echo "$db->error . ' error perfom query!'";
} else {
    while ($row = $result_db->fetch_object()) {
        # code to build sequence of checkboxes
        echo "<div class=\"form-group\"><div class=\"checkbox-inline\"><label for=\"{$row->category}\"><input type=\"checkbox\"
            name=\"category[]\"
            value=\"{$row->c_id}\" id=\"{$row->category}\"/>";

        echo "{$row->category}</label></div></div>";
    }
}
$conn->close();
?>
</div>
<div class="col-sm-12">
<div class="col-sm-6"><input class="btn btn-primary" type="submit" value="Submit"></div><div class="col-sm-6"><input class="btn btn-warning" type="reset" name="Reset"></div>
</div>
</form>

And this is what source of the page renders as;

<div class="col-sm-12">
    <h3>Build Vendor Category Relationships</h3>
    <p>Select Vendor from Dropdown, then place a check in each product box that pertains to the vendor.</p>
<form class="form-horizonal">
<div class="col-sm-3"><h4><span class="badge">1</span> Select Vendor</h4><select class="form-control" name="select"><option value="1">Baldwin</option><option value="2">Baldwin</option><option value="3">Baldwin</option><option value="4">Trucklite</option><option value="5">Grote</option><option value="6">Fram Filters</option></select></div><div class="col-sm-6"><h4><span class="badge">2</span> Select <strong>ALL</strong> Categories</h4><div class="form-group"><div class="checkbox-inline"><label for="Air Filters"><input type="checkbox"
            name="category[]"
            value="1" id="Air Filters"/>Air Filters</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Fuel Filters"><input type="checkbox"
            name="category[]"
            value="2" id="Fuel Filters"/>Fuel Filters</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Coolant Filters"><input type="checkbox"
            name="category[]"
            value="3" id="Coolant Filters"/>Coolant Filters</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Cabin Air Filter"><input type="checkbox"
            name="category[]"
            value="4" id="Cabin Air Filter"/>Cabin Air Filter</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Strobe Lights"><input type="checkbox"
            name="category[]"
            value="5" id="Strobe Lights"/>Strobe Lights</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Truck Lighting"><input type="checkbox"
            name="category[]"
            value="6" id="Truck Lighting"/>Truck Lighting</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Hydraulic Brakes"><input type="checkbox"
            name="category[]"
            value="7" id="Hydraulic Brakes"/>Hydraulic Brakes</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Wiper Blades"><input type="checkbox"
            name="category[]"
            value="8" id="Wiper Blades"/>Wiper Blades</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Add Category"><input type="checkbox"
            name="category[]"
            value="9" id="Add Category"/>Add Category</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Add Category"><input type="checkbox"
            name="category[]"
            value="10" id="Add Category"/>Add Category</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Add Category"><input type="checkbox"
            name="category[]"
            value="11" id="Add Category"/>Add Category</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Add Category"><input type="checkbox"
            name="category[]"
            value="12" id="Add Category"/>Add Category</label></div></div><div class="form-group"><div class="checkbox-inline"><label for="Add Category"><input type="checkbox"
            name="category[]"
            value="13" id="Add Category"/>Add Category</label></div></div></div>
<div class="col-sm-12">
<div class="col-sm-6"><input class="btn btn-primary" type="submit" value="Submit"></div><div class="col-sm-6"><input class="btn btn-warning" type="reset" name="Reset"></div>
</div>
</form>

Thanks in advance for all suggestions and help!




Aucun commentaire:

Enregistrer un commentaire