1.) To retrieve values from the selects
with PHP they need to be inside a form
that the user will submit to a page that will process the data. The selects
also need a name
attribute for this.
2.) <option val=
is incorrect. It should be <option value=
. Note that if you leave out the value attribute, the option itself will be the value.
3.) $_request
is case sensitive and needs to be capitalized: $_REQUEST
Assuming you will process the data on a page called processor.php
, you can try something like this:
HTML:
<form action="processor.php" method="POST">
<select id="cat" name="cat">
<option value="car">car</option>
<option value="phone">phone</option>
</select>
<select id="item" name="item">
</select>
<input type="submit" name="submit" />
</form>
JAVASCRIPT:
cars=new Array("Mercedes","Volvo","BMW","porche");
phones=new Array('Samsung','Nokia','Iphone');
populateSelect();
$(function() {
$('#cat').change(function(){
populateSelect();
});
});
function populateSelect(){
cat=$('#cat').val();
$('#item').html('');
if(cat=='car'){
cars.forEach(function(t) {
$('#item').append("<option value='"+t+"'>"+t+"</option>");
});
}
if(cat=='phone'){
phones.forEach(function(t) {
$('#item').append("<option value='"+t+"'>"+t+"</option>");
});
}
}
PHP: (In this example, the form is submitted to an example page processor.php
)
if (isset($_POST['submit'])) {
$cat = $_POST['cat'];
$item = $_POST['item'];
// You now have the user selections saved in the above variables and
// can proceed with inserting them into the database...
// ... use mysqli or pdo to connect to and query the db
}