Php 为什么我在使用onlick事件时会再次显示重复的值?
这里是我的js脚本Php 为什么我在使用onlick事件时会再次显示重复的值?,php,jquery,Php,Jquery,这里是我的js脚本 <script> function displayWarriors() { $.ajax({ url: "display_warriors.php", success: function(data) { $("#tableWarrior").append(data); } }); return false;
<script>
function displayWarriors() {
$.ajax({
url: "display_warriors.php",
success: function(data) {
$("#tableWarrior").append(data);
}
});
return false;
}
function createWarrior() {
$.post( "create_warrior.php", {
"wname": $("#txtWname").val(),
"wtype": $("#txtWtype").val()
},
function(msg){
displayWarriors();
});
return false;
}
</script>
我怎样才能解决这个问题
my display_warters.php
foreach($stmt as $warrior){
echo '<tr><td>'.$warrior['warrior_id'].'</td><td><a href="selected.php?id='.$warrior['warrior_id'].'">'.$warrior['warrior_name'].'</a></td><td>'.$warrior['warrior_type'].'</td></tr>';
}
foreach($stmtas$warrior){
回显“.$warrior['warrior\u id'..”.$warrior['warrior\u type'.]”;
}
重复项即将出现,因为您总是返回所有数据,其中一些数据已经存在于“tableWarrior”元素中,您应该更改语句
success: function(data) {
$("#tableWarrior").append(data);
}
到
你应该改变
<script>
function displayWarriors(wname, wtype) {
$.ajax({
url: "display_warriors.php",
data: { name: wname, type: wtype},
success: function(data) {
$("#tableWarrior").append(data);
}
});
return false;
}
function createWarrior() {
var name = $("#txtWname").val(),
var type = $("#txtWtype").val()
$.post( "create_warrior.php", {
"wname": name,
"wtype": type
},
function(msg){
displayWarriors(name, type);
});
return false;
}
</script>
函数显示(wname,wtype){
$.ajax({
url:“display_culars.php”,
数据:{name:wname,type:wtype},
成功:功能(数据){
$(“#tableWarrior”)。追加(数据);
}
});
返回false;
}
函数createWarrior(){
var name=$(“#txtWname”).val(),
变量类型=$(“#txtWtype”).val()
$.post(“create_warrior.php”,{
“wname”:名称,
“wtype”:类型
},
功能(msg){
显示(名称、类型);
});
返回false;
}
您的display_warrior.php应该返回一个warrior tr.like
$name = $_REQUEST["name"];
$type = $_REQUEST["type"]
$warrior = GetYourWarriorWithItsNameAndType($name, $type);//Call a funciton here to get the warrior from its name and type.
echo '<tr><td>'.$warrior['warrior_id'].'</td><td><a href="selected.php?id='.$warrior['warrior_id'].'">'.$warrior['warrior_name'].'</a></td><td>'.$warrior['warrior_type'].'</td></tr>';
$name=$\u请求[“name”];
$type=$\u请求[“type”]
$warrior=GetYourWarriorWithItsNameAndType($name,$type)//在这里调用一个函数,从名称和类型中获取战士。
回显“.$warrior['warrior\u id'..”.$warrior['warrior\u type'.]”;
重复项即将出现,因为您总是返回所有数据,其中一些数据已经存在于“tableWarrior”元素中,您应该更改语句
success: function(data) {
$("#tableWarrior").append(data);
}
到
你应该改变
<script>
function displayWarriors(wname, wtype) {
$.ajax({
url: "display_warriors.php",
data: { name: wname, type: wtype},
success: function(data) {
$("#tableWarrior").append(data);
}
});
return false;
}
function createWarrior() {
var name = $("#txtWname").val(),
var type = $("#txtWtype").val()
$.post( "create_warrior.php", {
"wname": name,
"wtype": type
},
function(msg){
displayWarriors(name, type);
});
return false;
}
</script>
函数显示(wname,wtype){
$.ajax({
url:“display_culars.php”,
数据:{name:wname,type:wtype},
成功:功能(数据){
$(“#tableWarrior”)。追加(数据);
}
});
返回false;
}
函数createWarrior(){
var name=$(“#txtWname”).val(),
变量类型=$(“#txtWtype”).val()
$.post(“create_warrior.php”,{
“wname”:名称,
“wtype”:类型
},
功能(msg){
显示(名称、类型);
});
返回false;
}
您的display_warrior.php应该返回一个warrior tr.like
$name = $_REQUEST["name"];
$type = $_REQUEST["type"]
$warrior = GetYourWarriorWithItsNameAndType($name, $type);//Call a funciton here to get the warrior from its name and type.
echo '<tr><td>'.$warrior['warrior_id'].'</td><td><a href="selected.php?id='.$warrior['warrior_id'].'">'.$warrior['warrior_name'].'</a></td><td>'.$warrior['warrior_type'].'</td></tr>';
$name=$\u请求[“name”];
$type=$\u请求[“type”]
$warrior=GetYourWarriorWithItsNameAndType($name,$type)//在这里调用一个函数,从名称和类型中获取战士。
回显“.$warrior['warrior\u id'..”.$warrior['warrior\u type'.]”;
display\u warriors.php的内容是什么?display\u warriors.php的内容是什么?
?它会破坏我的表,输出仍然是same@BrainedWashed,是display_warriors.php页面,只有以下语句echo'.$warrior['warrior\u id'.'。$warrior['warrior\u type'.'
@BrainedWash,那么您如何在该文件中制作和传递$warrior
?@BrainedWash,当您循环时,它将从display\u warriors.php文件返回多个tr
,因此它也返回重复项。更新了答案,可能会解决您的问题。祝你好运:)它破坏了我的表,输出仍然是正确的same@BrainedWashed,是display_warriors.php页面,只有以下语句echo'.$warrior['warrior\u id'.'。$warrior['warrior\u type'.'
@BrainedWash,那么您如何在该文件中制作和传递$warrior
?@BrainedWash,当您循环时,它将从display\u warriors.php文件返回多个tr
,因此它也返回重复项。更新了答案,可能会解决您的问题。祝你好运:)