I have been able to successfully implement x-editable to create new user and subsequently post changes to the DB. Everything is working fine in this regard.
我已经能够成功地实现x-editable来创建新用户,并随后发布对DB的更改。在这方面,一切都很顺利。
I wanted to use the numeric entry of a field and do some arithmetic operations on it and display the result in a different div.
我希望使用字段的数字条目,并对其进行一些算术操作,并在不同的div中显示结果。
I created this fiddle: http://jsfiddle.net/Mdcfh/
我创建了这个fiddle: http://jsfiddle.net/Mdcfh/
which displays my setup. This fiddle works fine, but when I implement exactly the same in my code, nothing happens in the call back.
它显示我的设置。这个小提琴工作得很好,但是当我在代码中实现完全相同的东西时,在回调中不会发生任何事情。
I am appending the relevant portions of my code below:
我在下面附上我的代码的相关部分:
HTML
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>
JS
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();
}
}
});
POST.PHP
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);
}
The post.php code is working, all my editables are updating successfully. But I have not been able to make the success callback work at all. None of the statements written in the success callback work (they work in the browser console though).
这个职位。php代码正在工作,我所有的编辑器都在成功更新。但我还没能让成功的回叫起作用。在成功回调中编写的任何语句都不能工作(尽管它们在浏览器控制台中工作)。
Here's another fiddle which uses the real code. The fiddle: http://jsfiddle.net/8meZ8/1/
这是另一个使用真实代码的小提琴。小提琴:http://jsfiddle.net/8meZ8/1/
I am fairly new to this, I am sure there is some obvious omission. Any help will be greatly appreciated! Thanks in advance!!
我对此相当陌生,我确信有一些明显的遗漏。如有任何帮助,我们将不胜感激!提前谢谢! !
UPDATE for @brutallord
更新@brutallord
I use this in my post.php for generating response:
我在我的帖子里使用这个。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);
}
I will like to highlight that, submitting a new user works fine. In which case, I am posting to a different file newuser.php which sends a response as given below:
我想强调一下,提交一个新用户很好。在这种情况下,我将发布到另一个文件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'];
}
The success callback function for newuser works like a charm with the above response.
newuser的success回调函数与上面的响应类似。
3 个解决方案
#1
3
I figured out the solution:
我想到了解决办法:
It was a plain error of Javascript code flow. Editables seems to be particularly finicky about it. Moved the code blocks (editable declarations) in proper sequence (in the expected sequence of their run) and everything started working like a charm.
这是Javascript代码流的一个明显错误。可编辑的似乎对它特别挑剔。将代码块(可编辑的声明)按正确的顺序(按照预期的运行顺序)移动,一切就像魔法一样开始工作。
A particular case needs highlighting - If editable is initiated earlier in the code like so,
一个特定的情况需要突出显示——如果可编辑性在类似这样的代码中更早地启动,
$(selector).editable({
url: 'post.php',
})
Make sure that the validation/success/error code is just below it, to avoid code flow issues like I faced. Ideally would suggest, skipping separate initialization. Just declare globals in the beginning and then use editables below.
确保验证/成功/错误代码就在下面,以避免像我遇到的代码流问题。理想的做法是跳过单独的初始化。只需在开头声明全局变量,然后使用下面的可编辑表。
Thanks everyone for responding.
谢谢大家的响应。
#2
1
Seems to be so, that success callback uses answer from server as JSON, but may be you have not send that:
似乎是这样的,成功回调使用服务器作为JSON,但可能您没有发送:
header('Content-type:application/json')
?
吗?
#3
0
<?php
$yes = array('success' => true,'msg'=>'Done');
$no = array('success' => false,'msg'=>'no');
header('Content-type:application/json');
echo json_encode($yes);
And js code
和js代码
$('tr #name,tr #job').editable({
type:'text',
ajaxOptions: { dataType: 'json' },
url: 'editable-request/update-trainee.php',
display: function(value, response) {
return false; //disable this method
},
success: function(response, newValue) {
if(!response.success) {
return response.msg;
}else{
alert(response.msg);
} });
That is working for me
这对我很有效
#1
3
I figured out the solution:
我想到了解决办法:
It was a plain error of Javascript code flow. Editables seems to be particularly finicky about it. Moved the code blocks (editable declarations) in proper sequence (in the expected sequence of their run) and everything started working like a charm.
这是Javascript代码流的一个明显错误。可编辑的似乎对它特别挑剔。将代码块(可编辑的声明)按正确的顺序(按照预期的运行顺序)移动,一切就像魔法一样开始工作。
A particular case needs highlighting - If editable is initiated earlier in the code like so,
一个特定的情况需要突出显示——如果可编辑性在类似这样的代码中更早地启动,
$(selector).editable({
url: 'post.php',
})
Make sure that the validation/success/error code is just below it, to avoid code flow issues like I faced. Ideally would suggest, skipping separate initialization. Just declare globals in the beginning and then use editables below.
确保验证/成功/错误代码就在下面,以避免像我遇到的代码流问题。理想的做法是跳过单独的初始化。只需在开头声明全局变量,然后使用下面的可编辑表。
Thanks everyone for responding.
谢谢大家的响应。
#2
1
Seems to be so, that success callback uses answer from server as JSON, but may be you have not send that:
似乎是这样的,成功回调使用服务器作为JSON,但可能您没有发送:
header('Content-type:application/json')
?
吗?
#3
0
<?php
$yes = array('success' => true,'msg'=>'Done');
$no = array('success' => false,'msg'=>'no');
header('Content-type:application/json');
echo json_encode($yes);
And js code
和js代码
$('tr #name,tr #job').editable({
type:'text',
ajaxOptions: { dataType: 'json' },
url: 'editable-request/update-trainee.php',
display: function(value, response) {
return false; //disable this method
},
success: function(response, newValue) {
if(!response.success) {
return response.msg;
}else{
alert(response.msg);
} });
That is working for me
这对我很有效