英文:
php - ajax Update record onchange wihout reload page
问题
以下是您要翻译的内容:
I need create a table, in the column the size and in the row the quantity for every product, I create a query where put in array the different result to have column and row and after I create a table.. I need to update every time there anybody do a change
while($xx < count($testa))
{
$testaecho .= "<th>".$testa[$xx]."</th>";
$corpoecho .= "<td style=' max-width: 20px;'>";
$corpoecho .= "<form name='changeqta' method='post' action=''>";
$corpoecho .= "<input type='number' maxlength='3' id='mod' value='".$corpo[$xx]."'>";
$corpoecho .= "<input type='hidden' id='ctgr' value='".$idriga[$xx]."'>";
$corpoecho .= "</form>";
$corpoecho .= "</td>";
$xx++;
}
echo "<tr>".$testaecho."</tr>";
echo "<tr>".$corpoecho."</tr>";
and in the jquery
<script>
$(document).ready(function(){
$('mod').on('change', function(){
var name=$("mod").val();
var ctgr=$("ctgr").val();
$.ajax({
url:'update.php',
method:'POST',
data:{
name:name,
ctgr:ctgr
},
success:function(response){
alert(response);
}
});
});
});
</script>
the problem is only the first input do the change, if i change all other input, the firs product or the other product not update... is the same that the second "mod" is not do the submit
请注意,这些代码包含了一些HTML和PHP,以及使用jQuery进行的JavaScript操作。如果您需要有关代码的解释或帮助,可以提出具体问题。
英文:
I need create a table, in the column the size and in the row the quantity for every product, I create a query where put in array the different result to have column and row and after I create a table.. I need to update every time there anybody do a change
while($xx < count($testa))
{
$testaecho .= "<th>".$testa[$xx]."</th>";
$corpoecho .= "<td style=' max-width: 20px;'>";
$corpoecho .= "<form name='changeqta' method='post' action=''>";
$corpoecho .= "<input type='number' maxlength='3' id='mod' value='".$corpo[$xx]."'>";
$corpoecho .= "<input type='hidden' id='ctgr' value='".$idriga[$xx]."'>";
$corpoecho .= "</form>";
$corpoecho .= "</td>";
$xx++;
}
echo "<tr>".$testaecho."</tr>";
echo "<tr>".$corpoecho."</tr>";
and in the jquery
<script>
$(document).ready(function(){
$('#mod').on('change', function(){
var name=$("#mod").val();
var ctgr=$("#ctgr").val();
$.ajax({
url:'update.php',
method:'POST',
data:{
name:name,
ctgr:ctgr
},
success:function(response){
alert(response);
}
});
});
});
</script>
the problem is only the first input do the change, if i change all other input, the firs product or the other product not update... is the same that the second "mod" is not do the submit
why?
答案1
得分: 1
由于不允许重复的ID,尝试使用类。而且,尝试使用属性而不是另一个隐藏输入。例如,
$corpoecho .= "<input type='number' maxlength='3' class='mod' data-mod='" . $idriga[$xx] . "' value='" . $corpo[$xx] . "'>";
检查 data-mod='" . $idriga[$xx] . "'
现在在jQuery中这样访问,
$('.mod').on('change', function(){
var name = $(this).val(); // 获取更改项的值
var ctgr = $(this).attr('data-mod'); // 获取属性值
// ....
});
英文:
Since duplicate id is not possible try using a class. And try using attribute instead another hidden input. For example,
$corpoecho .= "<input type='number' maxlength='3' class='mod' data-mod='".$idriga[$xx]."' value='".$corpo[$xx]."'>";
check on data-mod='".$idriga[$xx]."'
Now access in jquery like this,
$('.mod').on('change', function(){
var name = $(this).val(); // Get the value of changed item
var ctgr = $(this).attr('data-mod'); // get arrtibute value
....
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论