I have two selects: one for type and one for models. I would like to populate the models select depending on the type selection.
What is the easiest way to do it? And I am kind newbie concerning ajax.
I was thinking of perhaps having only one select with groups but can I afterwards do a search on the category?
Also one of my problems is that I am doing this on the multistep form so I don’t want to reload the page. My data currently is on text files and converted it to php arrays which I used to populate the selects (check div step2a with the pc_model example).
Here is a sample of my code.
sources for types and models
$comp_model = $dir . '\Computer_model.txt'; //Liste complète
$hard_type = $dir . '\Hardware_type.txt';
$hard_model = $dir . '\Hardware_model.txt';
index.php
<form id="add_to_stock" class="form-horizontal" name="add_to_stock" method="post" action="./result.php">
<div id="step1" class="add_mat">
<legend>Step 1 of 4</legend>
<div class="form-group col-sm-12">
<label class="control-label">Type de Matériel</label>
<div class="radio">
<label><input type="radio" name="mat_type" value="computer" checked>Ordinateur</label>
</div>
<div class="radio">
<label><input type="radio" name="mat_type" value="hardware">Hardware</label>
<div class="radio">
<label><input type="radio" name="mat_type" value="contract">Contrat</label>
</div>
<div class="form-group col-sm-12">
<div class="col-sm-5 pull-right">
<button class="btn btn-success pull-right go_to_step_2" type="button">
Suivant <span class="glyphicon glyphicon-chevron-right"></span>
</button>
</div>
</div>
</div>
</div>
<div id="step2a" class="add_mat">
<legend>Step 2 of 4</legend>
<div class="form-group col-sm-12 ">
<h4 class="text-muted text-uppercase "><strong>Ajouter un ordinateur</strong></h4>
<div class="alert alert-warning"><span class="glyphicon glyphicon-warning-sign"></span><strong> Attention :</strong> Il faut remplir tous les champs!</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="pc_model">Model :</label>
<div class="col-sm-10">
<select id="pc_model" name="pc_model" class="select2 form-control" style="width: 100%">
<?php
$pc_models = file($comp_model, FILE_IGNORE_NEW_LINES);
foreach ($pc_models as $model) {
?>
<option value="<?php echo $model; ?>"><?php echo $model; ?></option>;
<?php
}
?>
</select>
</div>
</div>
<div class="form-group no_margin">
<div class="col-sm-10">
<input id="pc_deploymentState" name="pc_deploymentState" class="form-control" type="hidden" value="Stock new">
</div>
</div>
<div class="form-group no_margin">
<div class="col-sm-10">
<input id="pc_incidentState" name="pc_incidentState" class="form-control" type="hidden" value="Operational">
</div>
</div>
<div class="form-group cont-quant">
<label class="col-sm-2 control-label" for="pc_quantity">Quantité :</label>
<div class="col-sm-10">
<input id="pc_quantity" name="pc_quantity" class="form-control quantity" type="number" min="1" max="10" value="">
<span class="glyphicon glyphicon-remove form-control-feedback AQ_close_icon hidden"></span>
</div>
</div>
<div class="col-sm-2"></div>
<div class="col-sm-10 alert alert-danger alert-dismissible AQ hidden">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
La quantité choisi doit être entre 1 et 10.
</div>
<div class="form-group col-sm-12">
<div class="col-sm-1"></div>
<div class="col-sm-5">
<button class="btn btn-warning pull-left back_to_step_1" type="button">
<span class="glyphicon glyphicon-chevron-left"></span> Précédent
</button>
</div>
<div class="col-sm-6">
<button class="btn btn-success pull-right go_to_step_3" type="button">
Suivant <span class="glyphicon glyphicon-chevron-right"></span>
</button>
</div>
</div>
</div>
</div>
<div id="step2b" class="add_mat">
<legend>Step 2 of 4</legend>
<div class="form-group col-sm-12">
<label class="control-label">Ajouter du hardware</label>
<div class="form-group">
<label class="col-sm-2 control-label" for="hard_type">Type :</label>
<div class="col-sm-10">
<select id="hard_type" name="hard_type" class="select2 form-control" style="width: 100%">
<?php
$hard_types = file($hard_type, FILE_IGNORE_NEW_LINES);
foreach ($hard_types as $hard_type) {
?>
<option value="<?php echo $hard_type; ?>"><?php echo $hard_type; ?></option>;
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="hard_model">Model :</label>
<div class="col-sm-10">
<select id="hard_model" name="hard_model" class="select2 form-control" style="width: 100%">
<?php
//if($current_type != "PCI/PCIe Card"){
// $hard_model = $dir . $current_type .'.txt';
//}else{
// $hard_mocel = $dir . 'PCI_PCIe Card.txt';
//}
$hard_models = file($hard_model, FILE_IGNORE_NEW_LINES);
foreach ($hard_models as $hard_model) {
?>
<option value="<?php echo $hard_model; ?>"><?php echo $hard_model; ?></option>;
<?php
}
?>
</select>
</div>
</div>
<div class="form-group no_margin">
<div class="col-sm-10">
<input id="hard_deploymentState" name="hard_deploymentState" class="form-control" type="hidden" value="Stock new">
</div>
</div>
<div class="form-group no_margin">
<div class="col-sm-10">
<input id="hard_incidentState" name="hard_incidentState" class="form-control" type="hidden" value="Operational">
</div>
</div>
<div class="form-group cont-quant">
<label class="col-sm-2 control-label" for="hard_quantity">Quantité :</label>
<div class="col-sm-10">
<input id="hard_quantity" name="hard_quantity" class="form-control quantity" type="number" min="1" max="10" value="" value="">
<span class="glyphicon glyphicon-remove form-control-feedback AQ_close_icon hidden"></span>
</div>
</div>
<div class="col-sm-2"></div>
<div class="col-sm-10 alert alert-danger alert-dismissible hidden AQ">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
La quantité choisi doit être entre 1 et 10.
</div>
<div class="form-group col-sm-12">
<div class="col-sm-1"></div>
<div class="col-sm-5">
<button class="btn btn-warning pull-left back_to_step_1" type="button">
<span class="glyphicon glyphicon-chevron-left"></span> Précédent
</button>
</div>
<div class="col-sm-6">
<button class="btn btn-success pull-right go_to_step_3" type="button">
Suivant <span class="glyphicon glyphicon-chevron-right"></span>
</button>
</div>
</div>
</div>
</div>
<div id="step2c" class="add_mat">
<legend>Step 2 of 4</legend>
<div class="form-group col-sm-12">
<label class="control-label">Contract</label>
<div class="form-group col-sm-12">
<div class="col-sm-1"></div>
<div class="col-sm-5">
<button class="btn btn-warning pull-left back_to_step_1" type="button">
<span class="glyphicon glyphicon-chevron-left"></span> Précédent
</button>
</div>
<div class="col-sm-6">
<button class="btn btn-success pull-right go_to_step_3" type="button">
Suivant <span class="glyphicon glyphicon-chevron-right"></span>
</button>
</div>
</div>
</div>
</div>
<div id="step3" class="add_mat">
<fieldset id="add_serialNumber">
<legend>Step 3 of 4</legend>
<div class="form-group col-sm-12">
<label class="control-label">Ajout des numéros de série</label>
<div class="SN_wrapper form-group">
<label class="col-sm-2 control-label padding-right" for="serialNb_1">Numéro de série 1 :</label>
<div class="col-sm-10">
<input id="serialNb_1" name="serialNbs[]" class="form-control" type="text" value="">
</div>
</div>
<div class="form-group col-sm-12">
<div class="col-sm-1"></div>
<div class="col-sm-5">
<button class="btn btn-warning pull-left back_to_step_2" type="button">
<span class="glyphicon glyphicon-chevron-left"></span> Précédent
</button>
</div>
<div class="col-sm-6">
<button class="btn btn-success pull-right go_to_step_4" type="button">
Suivant <span class="glyphicon glyphicon-chevron-right"></span>
</button>
</div>
</div>
</div>
</div>
<div id="step4" class="add_mat">
<div id="recap" class="container">
<h4 id="type_h4">Type : <span id="type"></span></h4>
<h4 id="model_h4">Modèle : <span id="model"></span></h4>
<h4 id="quant_h4">Quantité : <span id="quant"></span></h4>
<div class="">
<ul class="list-unstyled">
</ul>
</div>
</div>
<legend>Step 4 of 4</legend>
<div class="form-group col-sm-12">
<label class="control-label">Confirmation</label>
<div class="form-group col-sm-12">
<div class="col-sm-1"></div>
<div class="col-lg-5">
<button class="btn btn-warning pull-left back_to_step_3" type="button">
<span class="glyphicon glyphicon-chevron-left"></span> Précédent
</button>
</div>
<div class="col-lg-6">
<button id="add_new_mat" class="btn btn-primary pull-right" type="submit">
Ajouter
</button>
</div>
</div>
</div>
</div>
</form>
stock.js
$(document).ready(function () {
var id, current_div;
$(".select2").select2();
$(".select2").select2({
selectOnClose: true
});
//évenements click des buttons "suivant"
$(".go_to_step_2").click(function () {
var option_val = $('input[name=mat_type]:checked').val();
console.log(option_val);
switch (option_val) {
case 'computer':
$('#step1').hide();
$('#step2a').show();
break;
case 'hardware':
$('#step1').hide();
$('#step2b').show();
break;
case 'contract':
$('#step1').hide();
$('#step2c').show();
break;
default:
}
$(".quantity").blur(function () {
$this = $(this).val();
if ($this < 1 || $this > 10) {
$(".cont-quant").addClass("has-error has-feedback");
$(".AQ").removeClass("hidden");
$(".AQ_close_icon").removeClass("hidden");
} else {
$(".cont-quant").removeClass("has-error has-feedback");
$(".AQ").addClass("hidden");
$(".AQ_close_icon").addClass("hidden");
}
});
});
$("#hard_type").change(function(){
var selected = $("#hard_type").val();
$.ajax({
url: 'script.php',
type: 'POST',
data: {'current_type': selected},
success: function(){
}
});
});
$(".go_to_step_3").click(function () {
if ($('div').css('display') == 'block' && $('div').hasClass('add_mat')) {
id = $(".add_mat:visible").attr('id');
}
console.log(id);
current_div = "#" + id;
console.log(current_div);
$(current_div).hide();
$("#step3").show();
var option_val = $('input[name=mat_type]:checked').val();
var quantity;
switch (option_val) {
case 'computer':
quantity = $('input[name=pc_quantity]').val();
console.log(quantity);
break;
case 'hardware':
quantity = $('input[name=hard_quantity]').val();
console.log(quantity);
break;
case 'contract':
quantity = $('input[name=contr_quantity]').val();
console.log(quantity);
break;
default:
}
console.log(quantity);
var wrapper = $('.SN_wrapper');
var labelField = '';
var inputField = '';
var initialInputID = $("input[name*='serialNbs']").attr('id');
var x = parseInt(initialInputID.split("_")[1]);
var y = x + 1;
for (i = 0; i < quantity - 1; i++) {
labelField = '<label for="serialNb_' + y + ' " class="col-sm-2 control-label padding-right">' + 'Numéro de série ' + y + ' :</label>';
inputField = '<div class="col-sm-10"><input id="serialNb_' + y + '" name="serialNbs[]" class="form-control" type="text" value="" /></div>';
y++;
$(wrapper).append(labelField, inputField);
}
});
$(".go_to_step_4").click(function () {
$("#step3").hide();
$("#step4").show();
var option_val = $('input[name=mat_type]:checked').val();
var quantity;
var model;
var type;
switch (option_val) {
case 'computer':
$("#type_h4").hide();
model = $('#pc_model option:selected').text();
$("#model").append(model);
quantity = $('input[name=pc_quantity]').val();
$("#quant").append(quantity);
console.log(model + ";" + quantity);
break;
case 'hardware':
type = $('#hard_type option:selected').text();
$("#type").append(type);
model = $('#hard_model option:selected').text();
$("#model").append(model);
quantity = $('input[name=hard_quantity]').val();
$("#quant").append(quantity);
console.log(type + ";" + model + ";" + quantity);
break;
case 'contract':
quantity = $('input[name=contr_quantity]').val();
console.log(quantity);
break;
default:
}
console.log(type + ";" + model + ";" + quantity);
});
//évenements click des buttons "précédent"
$(".back_to_step_1").click(function () {
if ($('div').css('display') == 'block' && $('div').hasClass('add_mat')) {
id = $(".add_mat:visible").attr('id');
}
console.log(id);
current_div = "#" + id;
console.log(current_div);
$(current_div).hide();
$("#step1").show();
});
$(".back_to_step_2").click(function () {
var option_val = $('input[name=mat_type]:checked').val();
console.log(option_val);
switch (option_val) {
case 'computer':
$('#step3').hide();
$('#step2a').show();
break;
case 'hardware':
$('#step3').hide();
$('#step2b').show();
break;
case 'contract':
$('#step3').hide();
$('#step2c').show();
break;
default:
}
});
$(".back_to_step_3").click(function () {
$("#step4").hide();
$("#step3").show();
});
});