jEditable只接受一个id [英] jEditable is taking only one id
问题描述
好的.我是jEditable的新手.让我解释一下jEditable遇到的问题.
Alright. I'm new to jEditable. Let me explain the issue i'm having with jEditable.
我的数据库中有这个简单的表-
I have this simple table in my database -
id car make
1 panamera porsche
2 italia ferraris
3 avantador lamborghini
4 slk mercedes
我将在while循环中回显此表,下面是代码-
And i'm gonna echo this table in a while loop and below is the code -
<script type="text/javascript">
<?php
$query2 = "SELECT * FROM inplace LIMIT 0, 6";
$result2 = mysql_query($query2) or die ('Query couldn\'t be executed');
$row2 = mysql_fetch_assoc($result2);
?>
$(function() {
$(".items").editable("handler.php", {
submitdata : {userid: "<?php echo $row2['id']; ?>"},
indicator : "<img src='img/indicator.gif'>",
tooltip : "Doubleclick to edit...",
event : "click",
onblur : "submit",
name : 'newvalue',
id : 'elementid',
});
});
</script>
</head>
<body>
<ul>
<?php
$query = "SELECT * FROM inplace LIMIT 0, 6";
$result = mysql_query($query) or die ('Query couldn\'t be executed');
while ($row = mysql_fetch_assoc($result)) {
echo '<li class="items" id="car">'.$row['car'].'</li>';
echo '<li class="items" id="make">'.$row['make'].'</li>';
}
?>
</ul>
在上面的代码中,我在可编辑脚本中传递了newvalue
(用户编辑)和elementid
(汽车或品牌).而且我还需要一个标识符来帮助标识要更新的正确数据库ID.所以我试图在submitdata : {userid: "<?php echo $row2['id']; ?>"}
中传递数据库ID.我不确定该方法是否正确.
In the above code, i'm passing newvalue
(user edited) and elementid
(car or make) in the jeditable script. And i also need one more identifier which helps identify the correct database id to update. So i'm trying to pass the database id in submitdata : {userid: "<?php echo $row2['id']; ?>"}
. I'm not sure if that method is correct.
以下是"handler.php"文件中的更新查询-
And below is the update query in "handler.php" file -
require("db.php");
function fail($msg) {
header('HTTP/1.0 404 Not Found');
die($msg);
}
$id = (int)@$_POST['userid'];
$field = @$_POST['elementid'];
$allowed_fields = array('car','make');
if (!in_array($field, $allowed_fields)) fail('Invalid or missing field.', 404);
$newvalue = $_POST['newvalue'];
$query = "UPDATE inplace SET `$field`='$newvalue' WHERE id=$id";
$result = mysql_query($query);
echo $newvalue;
所以这里的问题是,此submitdata : {userid: "<?php echo $row2['id']; ?>"}
仅传递第一个ID.这意味着其仅传递一个ID.因此,即使您编辑分别属于ID 2和3的italia
或avantador
,它也会更新第一个ID.当我回显查询时,无论您编辑哪种车,它始终显示为UPDATE inplace SETcar='volante' WHERE id=1
.当我直接在Submitdata中写入2或3或4时,它会正确更新.它仅传递一个ID.有人建议我使用id="car-'.$row['id'].'"
之类的东西,然后爆炸它,然后使用foo和东西.我尝试使用它,但没有为我工作.寻找解决方案.
So the issue here is, this submitdata : {userid: "<?php echo $row2['id']; ?>"}
is passing only 1st id. That means its passing only one id. So even if you edit italia
or avantador
which belongs to ids 2 and 3 respectively, it updates 1st id. When i echo the query, whichever car you edit, it always reads as UPDATE inplace SETcar='volante' WHERE id=1
. When i directly write 2 or 3 or 4 in the submitdata, it updates correctly. Its passing only one id. Someone suggested me to use like id="car-'.$row['id'].'"
and then explode it and then use foo and stuff. I tried using it but didnt work for me. Looking for a solution for this.
推荐答案
您可以在包含ID号的<li>
标记中创建自定义属性.然后,在您的jQuery中,您只需获取该值即可.
You can make a custom attribute in your <li>
tags containing the id number. Then in your jQuery, you just grab that value.
<script type="text/javascript">
$(function() {
$("li.items").each(function(index) {
$(this).editable("handler.php", {
submitdata : {userid: $(this).attr('carid')},
indicator : "<img src='img/indicator.gif'>",
tooltip : "Doubleclick to edit...",
event : "click",
onblur : "submit",
name : 'newvalue',
id : 'elementid',
});
});
});
</script>
</head>
<body>
<ul>
<?php
$query = "SELECT * FROM inplace LIMIT 0, 6";
$result = mysql_query($query) or die ('Query couldn\'t be executed');
while ($row = mysql_fetch_assoc($result)) {
echo '<li class="items" carid="'.$row['id'].'" id="car">'.$row['car'].'</li>';
echo '<li class="items" carid="'.$row['id'].'" id="make">'.$row['make'].'</li>';
}
?>
</ul>
免责声明:我没有对此进行测试,而且我不确定jQuery语法是否100%确定.
Disclaimer: I didn't test this and i'm not 100% sure on the jQuery syntax.
我只是使用$(this)
选择器将其更改为希望工作,以引用每个特定的<li>
.
I just changed it around to hopefully work using the $(this)
selector to reference each specific <li>
.
这篇关于jEditable只接受一个id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!