我有一个动态生成的选择,客户可以选择供应商($ sid)的产品($ pid):
<select name="<?php echo $pid; ?>" onchange="func(this.value)">
<?php foreach...{ ?>
<option name="<?php echo $pid; ?>" value="<?php echo $sid; ?>">
<?php echo $value; ?>
</option>
<?php } ?>
</select>
并且ajax调用必须将所选值分配给产品:
<script>
function func(selectedValue){
var pid = $('name').val();
$.ajax({
url: 'update.php',
type: 'POST',
data: {sid : selectedValue, pid : pid},
success: function() {
alert("Ok");
location.reload();
}
});
}
</script>
并且update.php
<?php
$pid = $_REQUEST['pid'];
$val = $_REQUEST['sid'];
$sid = json_encode($val);
$query = "UPDATE `orders` SET sid = '$sid' WHERE pid = '$pid'";
try {
$DBH = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
$STH=$DBH->prepare($query);
$STH->execute();
}
catch(PDOException $e) {
echo $e->getMessage();
}
$DBH=null;
?>
但信息不会更新。我的错误在哪里?
答案 0 :(得分:0)
var pid = $('name')。val(); with var pid = selectedValue;
答案 1 :(得分:0)
试试这个
<?php
$pid = $_REQUEST['pid'];
//$val = $_REQUEST['sid']; // you may write wrong here
//$sid = json_encode($val);
$sid= $_REQUEST['sid'];
$query = "UPDATE `orders` SET sid = '$sid' WHERE pid = '$pid'";
try {
$DBH = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
$STH=$DBH->prepare($query);
$STH->execute();
}
catch(PDOException $e) {
echo $e->getMessage();
}
$DBH=null;
?>
的javascript
<script>
function func(selectedValue){
var pid = $('#product option:selected').attr('name');
$.ajax({
url: 'update.php',
type: 'POST',
data: {sid : selectedValue, pid : pid},
success: function() {
alert("Ok");
location.reload();
}
});
}
</script>
HTML:
<select id="product" name="<?php echo $pid; ?>" onchange="func(this.value)">
<?php foreach...{ ?>
<option name="<?php echo $pid; ?>" value="<?php echo $sid; ?>">
<?php echo $value; ?>
</option>
<?php } ?>
</select>
答案 2 :(得分:0)
问题是由于这个错误的jquery选择器
,你没有得到正确的pid
$('name').val()
您需要将id
属性添加到<select>
代码,让我们说它是product
<select id="product" name="<?php echo $pid; ?>" onchange="func(this.value)">
<?php foreach...{ ?>
<option name="<?php echo $pid; ?>" value="<?php echo $sid; ?>">
<?php echo $value; ?>
</option>
<?php } ?>
</select>
由于$pid
始终相同,您可以使用此选择器
$('#product').attr('name')
并将pid
设置为以上内容
<script>
function func(selectedValue){
var pid = $('#product').attr('name');
$.ajax({
url: 'update.php',
type: 'POST',
data: {sid : selectedValue, pid : pid},
success: function() {
alert("Ok");
location.reload();
}
});
}
</script>
答案 3 :(得分:0)
您可以替换:
var pid = $('name').val();
用这个:
var pid = $(this).attr('name');
以上代码将获取&#39; name&#39;
的值以下代码将获取sid
$(this).val();
答案 4 :(得分:0)
<select name="supplier" id="getValue">
<?php foreach...{ ?>
<option value="<?php echo $sid; ?>">
<?php echo $value; ?>
</option>
<?php } ?>
</select>
&#13;
在你的javascript上放这段代码: 请注意,行var formVals = $(&#39; form&#39;)。serialize();将获得表单将通过的所有输入。
$('#getValue').on('change', function() {
var formVals = $('form').serialize();
$.ajax({
url: "update.php",
type: 'POST',
async : false,
data: formVals,
success: function(data) {
alert("Ok");
location.reload();
}
});
});
&#13;
最后将$ _REQUEST更改为$ _POST [&#39;供应商&#39;];
答案 5 :(得分:0)
像这样更改HTML :(更改为onchange =“func(this)”)
<select name="<?php echo $pid; ?>" onchange="func(this)">
<?php foreach...{ ?>
<option name="<?php echo $pid; ?>" value="<?php echo $sid; ?>">
<?php echo $value; ?>
</option>
<?php } ?>
</select>
在代码中你调用ajax:
<script>
function func(element){
var selectedValue = $(element).val();
var pid = $(element).attr('name');
$.ajax({
url: 'update.php',
type: 'POST',
data: {sid : selectedValue, pid : pid},
success: function() {
alert("Ok");
location.reload();
}
});
}
</script>
在update.php文件中:
<?php
$pid = $_POST['pid'];
$val = $_POST['sid'];
$sid = json_encode($val);//I don't know why you have this line, this line should be removed
$query = "UPDATE `orders` SET sid = '$sid' WHERE pid = '$pid'";
try {
$DBH = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
$STH=$DBH->prepare($query);
$STH->execute();
}
catch(PDOException $e) {
echo $e->getMessage();
}
$DBH=null;
?>