Jquery Validate:如何调用 PHP 文件

发布于 2024-11-29 12:35:44 字数 518 浏览 1 评论 0原文

我对这个 javascipt、jquery 世界非常陌生,并且很难掌握它。

我的 HTML 中有以下内容

$(document).ready(function(){
   $("#frmEnquiry").validate();
});  
</script>

尽管我不明白该语句背后发生了什么,但它有效,特别是当它在每个字段条目上进行验证时(这很棒)。

我的表单定义中有 action="" 和提交按钮,我创建了一个 process.php 文件,该文件进一步验证并发送电子邮件,但我不知道如何调用该 php 文件或在哪里调用或什么语法是为了做到这一点。我假设它基于 validate() 的返回。

另外,从 PHP 文件返回成功或失败消息的正确过程是什么。

如果有人可以在简单的语法级别上提供帮助,我将非常感激,因为我已经看过很多可能的解决方案,我的头完全困惑了。

我很抱歉可能会问一个非常常见的问题,但是当您刚刚开始时,很难找到您完全理解的示例。

I am very new to this javascipt, jquery world and I am having great difficulty getting to grips with it.

I have got the following in my HTML

$(document).ready(function(){
   $("#frmEnquiry").validate();
});  
</script>

It works although I do not understand, what is going on behind that statement, especially as it validates on each field entry (which is wonderful).

I have action="" in my form definition and a submit button and I have created a process.php file which further validates and sends an email, but I have no idea how I call that php file or where I put the call or what the syntax is for doing it. I am assuming it is based on a return from validate().

Also what is the correct procedure for returning from the PHP file with either success or failure messages.

If anyone can help at a simple syntax level, I would be most grateful as I have looked at so many possible solutions, my head is just totally confused.

I apologise for probably asking a very common question, but when you are just starting, it is difficult finding an example which you fully understand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

口干舌燥 2024-12-06 12:35:44

嗯,有两种通用方法:

  1. 老式表单处理:您设置
    以便将您的请求提交到该页面。浏览器重新加载并显示您在 process.php 中呈现的内容
  2. Ajax-submit:这样您就可以将 action 留空,使用类似 的内容阻止提交

    然后使用 jQuery 的 AJAX 方法之一发送内容:获取发布ajax。它看起来像这样:

    $(文档).ready(function(){
        if($('form').validate()){
            $('表单').提交(函数{
                //我个人更喜欢ajax,因为get和post只是这个方法的简写
                $.ajax({    
                    url:'process.php',
                    数据类型:'json',
                    数据:{名称:nameVal,值:valueVal}
                    成功:函数(){
                        /*服务器返回了一些数据,按照你的意愿处理它*/
                    }
                });
            });
        }
    });
    

Well, there are two general approaches:

  1. Oldschool form processing: you set <form action='process.php'> so your request is submitted to that page. Browser reloads and displays the content that you have rendered in process.php
  2. Ajax-submit: this way you leave action blank, block submitting with something like <form onSubmit = 'return false'> and then send content using one of jQuery's AJAX-methods: get, post or ajax. It looks something like that:

    $(document).ready(function(){
        if($('form').validate()){
            $('form').submit(function{
                //Personally I prefer ajax as get and post are just shorthands for this method
                $.ajax({    
                    url:'process.php',
                    dataType:'json',
                    data:{name:nameVal, value:valueVal}
                    success: function(){
                        /*server returned some data, process it as you wish*/
                    }
                });
            });
        }
    });
    
欲拥i 2024-12-06 12:35:44

不要忘记原则:

  • PHP 在服务器端执行
  • javascript 在客户端执行

因此,只有在调用服务器时,PHP 才会执行,也就是说,如果您调用 URL (http://mydomain.com/MyPHPpage.php) 或者如果您通过 AJAX 请求调用您的服务器(这看起来是相同的,除了客户端没有重新加载)。

相反,javascript仅在客户端被调用。因此,用于 validate() 表单的代码仅在客户端调用(除非您在其背后有一个可以调用服务器的特定函数)。因此,JavaScript 将使用您提供给他的参数来验证表单,而无需调用服务器。这是一个很好的方法,可以避免服务器因不需要的请求(空字段、错误的电子邮件地址...)而超载。

无论如何,您仍然可以在服务器端进行一些其他检查(检查数据库是否存在用户......)并向用户返回另一条消息。

为了回答你的问题,我会问其他一些问题:你想验证什么?你想检查客户端还是服务器端?哪些数据以及用于什么目的?

Don't forget the principles:

  • PHP is executed server-side
  • javascript is executed client-side

Thus, PHP will only be executed if you are calling the server, meaningly, if you call an URL (http://mydomain.com/MyPHPpage.php) or if you are calling your server through an AJAX request (which is seemingly the same, except there is no reload client-side).

On the contrary, javascript is called only client-side. So the code used to validate() your form is called only on the client-side (except if you have a specific function behind this that would call the server). Hence, the javascript will use the parameters you gave him to validate the form without calling the server. This is a very good way not to overload your server with un-wanted request (empty fields, wrong e-mail addresses...).

Anyway, you can still have some other checks on your server side (checking in your database if the user exists...) and return another message to the user.

To answer your question, i'll ask some other ones: what are you trying to validate? do you want to check client-side or server-side? which data and for which purpose?

淡紫姑娘! 2024-12-06 12:35:44

您需要对 PHP 脚本进行 AJAX 调用。如果您的表单数据有效,您将进行这样的调用。您可以为 ajax 请求指定一个成功回调,您可以在其中处理 PHP 返回的数据并更新数据、显示错误消息或其他内容。从 PHP 中,您可以返回一个对象或数组,其中详细说明是否发生错误以及错误类型。有关 AJAX 的更多信息 -> http://api.jquery.com/jQuery.ajax/

You need an AJAX call to your PHP script. If your form data is valid you would do such a call. You can asign a success callback to the ajax request where you can process the return data from PHP and either update data, display a error message or something else. From PHP you can return an object or an array where you detail if errors occured and of what type. More about AJAX -> http://api.jquery.com/jQuery.ajax/

放赐 2024-12-06 12:35:44

在您的情况下,通过检查每个字段的值通过 javascript 执行表单验证是合理的。
无需向服务器发送数据并接收有关其正确性的信息。此方法使验证变得复杂,并且不能改善对不良数据的保护。

In your case it is reasonable to perform form validation via javascript by checking the value of each field.
There is no need to send data to the server and receive information about their correctness. This method complicates the validation and is not improve protection against bad data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文