简体   繁体   English

x可编辑的帖子值未定义

[英]x-editable post value is undefined

I just created an x-editable form : 我刚刚创建了一个x可编辑的表单:

   <a href='#' class='username'
   data-pk='<?php echo $key['idca']; ?>'
   data-type="text"
   data-placement="right"> <?php echo $key['categoryname']; ?> </a>

Then I created Javascript function to handle x-editable form : 然后,我创建了Javascript函数来处理x可编辑形式:

    $.fn.editable.defaults.success = function() { $(this).show() };
    $.fn.editable.defaults.mode = 'inline';
    $('.username').editable({
        url:'edit.php',
        pk:'1',
        type:'text',
        send:'always',
        ajaxOptions:{
            dataType:'json'
        },
        success: function(response, newValue) {
            window.alert('oke');
        },
        error: function(response, newValue) {
            window.alert('failed');
        }
    });

and in PHP I just create as follow : 在PHP中,我只创建如下:

<?php

 $pk = $_POST['pk'];
 $val = $_POST['value'];
 $name = $_POST['name'];

 print_r($_POST);

 ?>

But why I received message "undefined index pk, value, name" on window alert, which means I failed posting pk, value, name from x-editable.. 但是为什么我在窗口警报中收到消息“未定义的索引pk,值,名称”,这意味着我无法通过x-editable发布pk,值,名称。

Need helps, thank you very much 需要帮助,非常感谢

Given the following HTML 鉴于以下HTML

<a href="#" class="username"
    data-pk="<?php echo $key['idca']; ?>"
    data-type="text"
    data-placement="right"
>
    <?php echo $key['categoryname']; ?>
</a>

You can use this jQuery: 您可以使用以下jQuery:

$.fn.editable.defaults.success = function() { $(this).show() };
$('#username').editable({
    type: 'text',  // optionnal if you've set up the data-type attribute 
    pk: $(this).data('pk'), // optionnal if you've set up the data-pk attribute 
    url: 'edit.php', // mandatory
    title: 'Enter username'
});

This should work as per the documentation . 这应该按照文档进行操作 There is no need for you to do the $.ajax({}); 您无需执行$.ajax({}); by yourself. 由你自己。

A little late but on php side when you response you should use json_encode because x-editable is expecting you to return with json string. 有点晚了,但是在PHP端,当您响应时,您应该使用json_encode,因为x-editable希望您返回json字符串。

 $pk = $_POST['pk'];
 $val = $_POST['value'];
 $name = $_POST['name'];

 echo json_encode($_POST);
 die();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM