i have a car inventory website, we all know cars can vary greatly. so i made a table with the most common attributes, it includes checkboxes, radios and text now i need to check each one, i dont know if an if statement for each one will be appropiate since there is three types of inputs i guess i can group them on the same for loop but i have no idea how to do this OR if is there a better way?
<h3>Add a vehicle</h3>
<form action="" method="post" enctype="multipart/form-data" >
<b>VIN: <input type="text" name="vin">
<button class="btn-success"><a style="color: white;" href="#">Decode Vin</a></button><br />
Year: <?php include 'admin/tool/caryears.php'; ?>
Make: <?php include 'admin/tool/carmakes.php'; ?>
Model: <input type="text" name="model"><br />
Mileage: <input type="text" name="mileage"><br />
Price: <input type="text" name="price"><br />
VIN: <input type="text" name="vin"><br />
Attribute: <input type="text" name="att1">
Attribute: <input type="text" name="att2"><br />
Attribute: <input type="text" name="att3">
Attribute: <input type="text" name="att4"><br />
<table border="1" bordercolor="FFCC00" style="background-color:FFFFCC" width="100%" cellpadding="3" cellspacing="3">
<center>
<b>Common Attributes</b>
<tr>
<th>Body style</th>
<td>SUV <input value="suv" name="bodystyle" type="radio"></td>
<td>Sedan <input value="sedan" name="bodystyle" type="radio"></td>
<td>Truck <input value="truck" name="bodystyle" type="radio"></td>
<td>Mini Van <input value="minivan" name="bodystyle" type="radio"></td>
<td>Convertible <input value="convertible" name="bodystyle" type="radio"></td>
<td>Coupe <input value="coupe" name="bodystyle" type="radio"></td>
</tr><tr><td></td>
<td>Hatchback <input value="hatchback" name="bodystyle" type="radio"></td>
<td>Hybrid <input value="hybrid" name="bodystyle" type="radio"></td>
<td>Diesel <input value="diesel" name="bodystyle" type="radio"></td>
<td>Crossover <input value="crossover" name="bodystyle" type="radio"></td>
</tr>
<tr>
<th>Engine and Performance</th>
<td>Engine Size <input name="enginesize" type="text" value=" L" class="input-small"></td>
<td>Cylinders <select name="cyl" class="input-small"><option>Select</option><?php $i=2; while($i<13){echo '<option value="cyl'.$i.'">'.$i.' Cylinders</option>'; $i++;}?></select></td>
<td>Horse Power <input name="hp" class="input-small" type="text"></td>
<td>Diesel <input value="diesel" name="fuel" type="radio"></td>
<td>Gasoline <input value="gasoline" name="fuel" type="radio"></td>
</tr>
<tr>
<tr>
<th>Transmission</th>
<td>Automatic <input value="automatic" name="transmission" type="radio"></td>
<td>Manual <input value="manual" name="transmission" type="radio"></td>
<td>4 Speed <input value="fourspeed" name="shifts" type="radio"></td>
<td>5 Speed <input value="fivespeed" name="shifts" type="radio"></td>
<td>6 Speed <input value="sixspeed" name="shifts" type="radio"></td>
<td>OverDrive <input name="od" type="checkbox"></td>
</tr>
<tr>
<th>Sound System</th>
<td>CD Player <input value="cd" name="sound" type="radio"></td>
<td>MP3 Player <input value="mp3" name="sound" type="radio"></td>
<td>DVD Player <input value="dvd" name="sound" type="radio"></td>
<td>GPS Nav <input name="gps" type="checkbox"></td>
<td>Brand: <input name="sound_system" type="text" class="input-small"></td>
<td>Satellite Radio <input name="sradio" type="checkbox"></td>
</tr>
<tr>
<th>Instrumentation</th>
<td>Tachometer <input name="tachometer" type="checkbox"></td>
<td>Clock <input name="clock" type="checkbox"></td>
<td>Trip Computer <input name="trip" type="checkbox"></td>
<td>Exterior-Weather <input name="eweather" type="checkbox"></td>
<td>All-Digital <input name="digitalboard" type="checkbox"></td>
</tr>
<tr>
<th>DriveTrain</th>
<td>Rear-wheel-drive <input value="rwd" name="drive" type="radio"></td>
<td>Front-wheel-drive <input value="fwd" name="drive" type="radio"></td>
<td>All-wheel-drive <input value="awd" name="drive" type="radio"></td>
<td>4x4 <input name="fxf" type="checkbox"></td>
<td>Cruise Control <input name="cruisecontrol" type="checkbox"></td>
<td>Tilt Steering <input name="tiltsteering" type="checkbox"></td>
</tr>
<tr>
<th>Inside and outside</th>
<td>A/C <input name="ac" type="checkbox"></td>
<td>Removable Top <input name="removabletop" type="checkbox"></td>
<td>Keyless <input name="keyless" type="checkbox"></td>
<td>AirBags <input name="airbags" type="checkbox"></td>
<td>Alloy Wheels <input name="alloy" type="checkbox"></td>
<td>Trunk anti-trap <input name="trunkantitrap" type="checkbox"></td>
</tr>
<tr>
<th>Electric Powered Options</th>
<td>Power Windows <input name="ewindows" type="checkbox"></td>
<td>Power Mirrors <input name="emirrors" type="checkbox"></td>
<td>Power Driver Seat <input name="eseat" type="checkbox"></td>
<td>Power Locks <input name="elocks" type="checkbox"></td>
<td>Vehicle Anti-theft <input name="antitheft" type="checkbox"></td>
<td>LED headlights <input name="ledheadlights" type="checkbox"></td>
</tr>
</table>
</form>
So i need to check each one, if its not empty or checked or selected then create an array.
i personally think im doing something wrong, is there a better way to go about this?
i basically need this info to enter it into a database of attributes
If you are looking to capture the values of the checkbox
inputs this is how you can do it with PHP. You want the checkbox inputs to have the same name but add the square brackets []
to the end. When the form is submitted and you check the value of the checkbox fields with the same name, you will be given an array containing the checked options.
My simplified example:
HTML
<form method="post">
<label for="power-windows">Power Windows</label>
<input type="checkbox" id="power-windows" name="e-power-opts[]" value="power windows" />
<br>
<label for="power-doors">Power Doors</label>
<input type="checkbox" id="power-doors" name="e-power-opts[]" value="power doors" />
<br>
<label for="power-locks">Power Locks</label>
<input type="checkbox" id="power-locks" name="e-power-opts[]" value="power locks" />
<input type="submit" name="submit" value="submit">
</form>
With the above example let's say that a user selects Power Windows
and Power Doors
. When you check the contents of $_POST['e-power-opts']
you will receive an array like this:
Array
(
[0] => power windows
[1] => power doors
)
If no options are selected then $_POST['e-power-opts'] = null
.
You could loop through $_POST
but if nothing is selected for a checkbox
or radio
input then there wouldn't be a corresponding "empty" value in $_POST
. For example you have make
, model
and electronic power options
in the form and the user doesn't select anything for the electronic power options
checkboxes. Your $_POST
contents would be similar to this:
Array
(
[make] => pontiac,
[model] => g6,
[submit] => submit
)
Notice there is not a key value pair for electronic power options
. I only point this out if you needed to set a default value before your database insertion.
In the end, most of the time you will need to check each field. Not just to see if they're empty or not, but also for validation/sanitization purposes.
You might also want to check out PHPs filter functions: http://www.php.net/manual/en/ref.filter.php.