[英]Cant pass ajax variable to php
I have this code of html, the part that says Cliente is filled with a simple mysql query; 我有这个html代码,说Cliente的部分充满了简单的mysql查询; 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. 我想做的是,当您在Cliente中选择一个选项时,它会自动在第二个列表中显示链接到数据库中cliente的选项。
<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: 为了获得第一个列表中的选定选项,我可以使用以下js代码进行操作:
$(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); 到那时为止,一切都很好,即使我使用alert(cliented); it shows me the id that corresponds to the selected client. 它显示了与所选客户端相对应的ID。 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. $ cliente变量始终为空。 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. 您没有使用ajax设置变量名称。
Change: 更改:
data: cliented,
to:
data:'cliented='+cliented
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.