I have this code of html, the part that says Cliente is filled with a simple mysql query; what i want to do is that when you select one option in Cliente it automatically displays the options in the second list that are linked to cliente in the database.
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Cliente</label>
<div class="col-md-4">
<select id="cliented" name="cliented" class="form-control">
<?php foreach ($dcli3 as $key) { ?>
<option value="<?php echo $key['id'] ?>"><?php echo $key['nombre'] . ' ' . $key['apellido'] ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Cuenta</label>
<div class="col-md-4">
<select id="cuentad" name="cuentad" class="form-control">
<?php foreach ($dcu as $key) { ?>
<option value="<?php echo $key['numero'] ?>"><?php echo $key['numero'] ?></option>
<?php } ?>
</select>
</div>
</div>
To get the selected option in the first list i do it with this js code:
$(document).ready(function() {
$('select[name="cliented"]').change(function(){
var cliented = $(this).val();
$.ajax({
type: 'POST',
url: 'cons.php',
data: cliented,
dataType: 'json',
});
});
});
Everything is fine until that point, even if i use alert(cliented); it shows me the id that corresponds to the selected client. The problem is when i make the query like this:
if (isset($_POST['cliented'])) {
$cliente = $_POST['cliented'];
$cu = "SELECT * FROM cuenta WHERE idc= '$cliente'";
$dcu = $conn->query($cu);
}
The $cliente variable is always empty. What am i missing?
You want to send a key/value pair. Right now all you are sending is the value
Change:
data: cliented,
To:
data: {cliented: cliented},
You need to assign the value to a key.
$(document).ready(function () {
$('select[name="cliented"]').change(function () {
var cliented = $(this).val();
var postData = {
cliented: cliented
}
$.ajax({
type: 'POST',
url: 'cons.php',
data: postData,
dataType: 'json',
});
return false;
});
});
$(document).ready(function() {
$('select[name="cliented"]').change(function(){
var cliented = $(this).val();
DATA = 'cliented=' + cliented;
$.ajax({
type: 'POST',
url: 'cons.php',
data: cliented,
dataType: 'json',
success:function()
{
}
});
});
});
You are not setting variable name with the ajax.
Change:
data: cliented,
to:
data:'cliented='+cliented
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.