I have a carro
table that has a relationship with marca
and modelo
, and these two also have a relationship. I first add a marca , then when adding a new modelo , I must select a marca before.
Now, to add a new carro , I must select one marca and modelo . Currently all the registered modelo are being listed, but I need to list those related to the selected marca . Below is the select
HTML for the fields.
<div class="row">
<input id="hdn_id_marca" type="hidden" name="hdn_id_marca" value="0"/>
<div class="form-group col-md-6">
<label for="id_marca">Marca do Carro</label>
<select id="id_marca" name="id_marca" class="form-control" required>
<option value="" selected disabled>Selecione</option>
<?php foreach ($marcas as $m) { ?>
<option value="<?php echo $m['id']; ?>">
<?php echo $m['nome_marca']; ?>
</option>
<?php } ?>
</select>
</div>
<div class="form-group col-md-6">
<label for="id_modelo">Modelo do Carro</label>
<select id="id_modelo" name="id_modelo" class="form-control" required>
<option value="" selected disabled>Selecione</option>
<?php foreach ($modelos as $m) { ?>
<option value="<?php echo $m['id']; ?>">
<?php echo $m['nome_modelo']; ?>
</option>
<?php } ?>
</select>
</div>
</div>
These values are retrieved from the database, it is not any static value. I have tried to create an Ajax:
$('#id_marca').change(function() {
var selectedId = $(this).find('option:selected').val();
$('#hdn_id_marca').attr('value', selectedId);
$.ajax({
url: '/admin/modelos/select_by_id_marca',
data: {'hdn_id_marca': selectedId},
type: 'POST',
success: function() {
alert(data)
}
});
});
But I then I get
POST XHR http://localhost:8000/admin/modelos/select_by_id_marca [HTTP/1.1 500 Internal Server Error 53ms]
Edit 2
Below is my select_by_id_marca()
method. I have also updated the previous code by adding the hidden
field this method is suppose to have access to. After some changes, I fixed the reason for the 500 error to be happening, but now, the field is not populated only with the modelo according to the selected marca .
public function select_by_id_marca()
{
$data = array();
$this->db->order_by($this->primary_key, 'DESC');
$this->db->get_where('marca', array('id' => $this->input->post('hdn_id_marca')));
$query = $this->db->get('marca');
foreach ($query->result_array() as $row) {
$data[] = $row;
}
$query->free_result();
return $data;
}
This is wrong
$this->db->order_by($this->primary_key, 'DESC');
$this->db->get_where('marca', array('id' => $this->input->post('hdn_id_marca')));
$query = $this->db->get('marca');
You are using both get_where()
and get()
. It should be
$this->db->order_by($this->primary_key, 'DESC');
$query = $this->db->get_where('marca', array('id' => $this->input->post('hdn_id_marca')));
Next,
change
return $data;
to
echo json_encode($data);
In ajax success,
success: function(data) {
var a = JSON.parse(data);
alert(a.id);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.