red*_*lat 6 html javascript php x-editable
我已经能够成功实现x-editable来创建新用户,并随后将更改发布到数据库.在这方面一切都很好.
我想使用字段的数字输入并对其进行一些算术运算,并将结果显示在不同的div中.
我创造了这个小提琴:http://jsfiddle.net/Mdcfh/
显示我的设置.这个小提琴工作正常,但是当我在我的代码中实现完全相同时,回调中没有任何反应.
我在下面附上我的代码的相关部分:
HTML
<div id="msg_units" style="display:none">Saved!</div>
<table class="table table-condensed table-hover">
<thead>
<tr>
<td class="text-left hide800"><strong>Name</strong></td>
<td class="text-center"><strong>Price</strong></td>
<td class="text-center"><strong>Quantity</strong></td>
<td class="text-right"><strong>Totals</strong></td>
</tr>
</thead>
<tbody>
<tr>
<td class="text-left hide800"><?php echo $_REQUEST['title']?></td>
<td class="text-center"><i class="fa fa-inr"></i><strong><span class="price" id="unit_cost"><?php echo $_REQUEST['cost']?></span></strong></td>
<td class="text-center"><a href="#" class="myeditable qty" id="num_units" data-type="text" data-pk="3" data-name="num_units" data-title="How many are going?"></a></td>
<td class="text-right"><i class="fa fa-inr"></i><strong><a href="#" id="exp_total"></a> </strong></td>
</tr></tbody></table>
Run Code Online (Sandbox Code Playgroud)
JS
//editables
$('#num_units').editable({
url: 'post.php',
ajaxOptions: { dataType: 'json' },
display: function(value, response) {
return false; //disable this method
},
success: function(response, newValue) {
var current_pk = $(this).data('pk');
$('#exp_total').html(3*7200);
$('#msg_units').text(current_pk).show();
if (response && response.units) {
$('#msg_units').text(response.units*5).show();
}
}
});
Run Code Online (Sandbox Code Playgroud)
post.php中
$result = mysql_query('update mytable set '.mysql_escape_string($name).'="'.mysql_escape_string($value).'" where id = "'.mysql_escape_string($pk).'"');
$yes[] = array(
'success' => 'true',
'msg' => 'update success for',
'value' => $value
);
if ($result != false) {
// echo json_encode($yes);
echo '{"units": "3"}';
// echo '{"success": "true"}';
} else {
$no[] = array(
'success' => 'false',
'msg' => 'update failed for' .mysql_escape_string($name),
'value' => $value
);
echo json_encode($no);
}
Run Code Online (Sandbox Code Playgroud)
post.php代码正常工作,我的所有可编辑都在成功更新.但我根本无法成功回调.成功回调中写的所有语句都不起作用(尽管它们在浏览器控制台中工作).
这是使用真实代码的另一个小提琴.小提琴:http://jsfiddle.net/8meZ8/1/
我对此很新,我确信有一些明显的遗漏.任何帮助将不胜感激!提前致谢!!
更新@brutallord
我在post.php中使用它来生成响应:
$yes[] = array(
'success' => 'true',
'msg' => 'update success for',
'value' => $value
);
if ($result != false) {
echo json_encode($yes);
//echo '{"units": 3}';
// echo '{"success": "true"}';
} else {
$no[] = array(
'success' => 'false',
'msg' => 'update failed for' .mysql_escape_string($name),
'value' => $value
);
echo json_encode($no);
}
Run Code Online (Sandbox Code Playgroud)
我想强调一点,提交新用户可以正常工作.在这种情况下,我发布到另一个文件newuser.php,它发送一个响应,如下所示:
if ($result != false) {
echo '{"id": '. mysql_insert_id() .'}';
} else {
echo "Error:" . $maxid . $_POST['buyer_email'] . $_POST['buyer_contact_no'] . $_POST['buyer_full_name'];
}
Run Code Online (Sandbox Code Playgroud)
newuser的成功回调函数就像具有上述响应的魅力一样.
我想出了解决方案:
这是 Javascript 代码流的一个简单错误。Editables 似乎对此特别挑剔。按正确的顺序(按预期的运行顺序)移动代码块(可编辑声明),一切都开始像魅力一样工作。
需要突出显示一个特殊情况 - 如果可编辑是在代码中较早启动的,如下所示,
$(selector).editable({
url: 'post.php',
})
Run Code Online (Sandbox Code Playgroud)
确保验证/成功/错误代码位于其下方,以避免像我遇到的代码流问题。理想情况下建议跳过单独的初始化。只需在开头声明全局变量,然后使用下面的可编辑变量即可。
感谢大家的回复。