简体   繁体   English

PHP JQuery和Jeditable

[英]PHP JQuery and Jeditable

Just a quick question regarding this issue i am having. 关于这个问题,我只是一个简短的问题。 I am using jeditable to edit in place some fields on a page. 我正在使用jeditable编辑页面上的某些字段。 This is working perfectly. 这很正常。 Now I wish to implement some data checking. 现在,我希望实现一些数据检查。 I have my php code to check the data entered and if its correct, it updates that database, and if it isn't it will return the error. 我有我的PHP代码来检查输入的数据,如果正确,它将更新该数据库,如果不是,它将返回错误。 The issue I am having is I want it to spit out the error to tell them but when they click the field again to edit it, it shows the error in the field until a page refresh. 我遇到的问题是我希望它吐出错误来告诉他们,但是当他们再次单击该字段进行编辑时,它将在该字段中显示错误,直到页面刷新为止。 What i want it to do is have the same data in the field when they click on it after the error occurs instead of having to refresh the page then click the field again to edit it. 我要执行的操作是在发生错误后单击该字段时在字段中具有相同的数据,而不必刷新页面,然后再次单击该字段进行编辑。 Perhaps there is a way to return back the error and pass that into a tooltip of some sort above the field? 也许有一种方法可以返回错误,并将其传递到字段上方的某种工具提示中? Of course the way jeditable works is the div is surrounding the field then i have some js calling on my update.php file, this parses what jeditable passes to it and returns a $value to be error checked and by default if it is fine it simply at the bottom of the php "return $value;" 当然,jeditable的工作方式是div围绕着该字段,然后我在我的update.php文件上调用了一些js,它解析了jeditable传递给它的内容,并返回要进行错误检查的$ value,并且默认情况下是否合适只需在php的底部“返回$ value;” to be put back int he field after its been saved in the DB. 将其保存到数据库中后,将其放回字段中。

Hopefully someone can understand what I am asking here and any assistance would be appreciated. 希望有人能理解我在这里的要求,我们将不胜感激。

Easiest way is probably to do some client side validation. 最简单的方法可能是进行一些客户端验证。 Right now you are doing server side validation by checking in PHP when the form is submitted. 现在,您正在通过提交表单时签入PHP来进行服务器端验证。 What are you checking for?Without code it is hard to give you a good example of client side validation. 您要检查什么?没有代码,很难为您提供一个很好的客户端验证示例。

Basic field checking: 基本的现场检查:

var check_field = $("#field").val();
if (!check_field) { alert("Error message"); } else 
{ 
   // submit POST or whatever 
}

Edit 编辑

Since the MAC address validation algorithm is already written server side, I recommend a separate ajax POST request that calls the checker function. 由于MAC地址验证算法已在服务器端编写,因此我建议使用单独的ajax POST请求来调用检查程序功能。 Take the result of that request (true, false) and check it client side. 获取该请求的结果(对,错),并在客户端进行检查。 If true, proceed with the update call. 如果为true,则继续进行更新调用。

Example: 例:

$("#form").submit(function() {
var mac = $("#macfield").val();
if (!mac) { alert("MAC address can't be empty!"); } else 
{ 
   $.POST("checkmacaddress.php", {macval: mac}).success(function(a){
       //assuming a comes back as a bool
       if (!a) { alert("Invalid MAC!"); } else
       {
          // if the checker returned true, update the record
          $.POST("update.php" ...);
       }
   });
} });

This doesn't include the checkmacaddress.php but you should be able to handle that if you already have the function on hand. 这不包括checkmacaddress.php,但如果您已经拥有此功能,则应该能够处理该问题。

Hate when I do this, post here then figure out the answer myself...but at least if someone has the same issue they will see it. 讨厌当我这样做时,在这里发帖然后自己找出答案……但是至少如果有人遇到相同的问题,他们会看到的。 I found out about the jeditable onsubmit functions...i am using a tooltip to show on hover when editing the field so this will set the tooltip to the error and not submit the data unless its a valid mac. 我发现了关于jeditable onsubmit函数的信息...我在编辑字段时使用工具提示在悬停时显示,因此这会将工具提示设置为错误,并且除非其为有效的mac,否则不会提交数据。

    function isMAC(value) {
            teststr = value;
            regex=/^([0-9a-f]{2}([:-]|$)){6}$|([0-9a-f]{4}([.]|$)){3}$/i;
            if (regex.test(teststr)){
                    return true;
            }
            else {
                    return false;
            }
    }

    $(".edit_mac").editable("edit_mac.php", {
            onsubmit: function(settings, data) {
                    var input = $(data).find('input');
                    var value = input.val();
                if (isMAC(value)) {
                            return true;
                } else {
                            //display your message
                            $("#tooltip").html("Bad MAC Address...");
                            return false;
                    }
            },
          indicator : "Saving...",
          submitdata: { _method: "put" },
          submit : 'Save',
          cssclass : "editable",
          type : "text"
    });

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

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