简体   繁体   English

我应该分开我的Jquery和PHP验证还是以某种方式使用ajax?

[英]Should I keep my Jquery and PHP validation separate or use ajax somehow?

I need to validate two number fields, then multiply/ add together using php (keep calculators private). 我需要验证两个数字字段,然后使用php(将计算器保持私有)相乘/相加。 Finally, return this number into a third 'total field'. 最后,将此数字返回第三个“总计字段”。

Should I use my Jquery & PHP validation together with ajax some how? 我应该将我的Jquery和PHP验证与Aj​​ax一起使用吗? Or should I keep my Jquery & PHP validation separate, like the example below: ? 还是应该分开我的Jquery和PHP验证,如以下示例所示:

Jquery validation: jQuery验证:

 function mp_calcs_display() {
        $output = <<<HTML
    <form action="" method="post" name="formsubmit" id="formsubmit"   >
    Number of welds: <input type="number" name="numberofwelds" id="numberofwelds"  >
    Number of construction welds: <input type="number" name="numberofconwelds" id="numberofconwelds"  >
    Total time(secs): <input type="text" name="totaltimesecs" disabled>
    <input type="submit"  value="Calculate" id="submit" name="submit">
    </form>
    <script type="text/javascript">
        jQuery(document).ready(function($) {

            $('#formsubmit').validate({
            rules:  {

            numberofwelds: "required",
            numberofconwelds: "required"
                    },

            messages: {
            numberofwelds: "Please enter the number of welds",
            numberofconwelds: "Please enter number of con"
            },

            submitHandler: function(form) {
            form.submit();
            }       

        });

        });

        </script>
    HTML;

        return $output;
    }  

PHP validation like this: PHP验证如下:

if (isset($_POST['formsubmit'])){ 

then make sure both are numbers & not empty. 然后确保两者都是数字且不为空。 Then do the calculations in php 然后在php中进行计算

You should always validate your forms both in the client and on the server-side. 你应该总是验证您的形式无论是在客户端和服务器端。

Client side validation is simply for user experience. 客户端验证只是为了用户体验。 It's just really easy on your users to respond quickly and seemlessly with error information versus another page load. 与错误和其他页面加载相比,用户非常容易快速,毫不费力地做出响应。 When the request is sent off to your server, regardless of javascript validation, you will still need to check again to ensure the request has not been manipulated. 将请求发送到您的服务器后,无论是否进行了javascript验证,您仍将需要再次检查以确保未处理该请求。

Server side validation is where it counts. 服务器端验证才是关键所在。 Always do this. 总是这样做。

Simple answer: Keep both. 简单的答案:两者都保留。

Edit: To better answer your question, use AJAX if you can -- it's a better user experience versus reloading the page. 编辑:为了更好地回答您的问题,请尽可能使用AJAX-与重新加载页面相比,这是更好的用户体验。

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

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