ajax方式提交 表单_提交AJAX表单:AngularJS方式

ajax方式提交 表单

A lot of developers were submitting forms before AngularJS came out. There were so many different ways to submit forms that it could drive a person crazy... and they still can.

在AngularJS出现之前,许多开发人员都在提交表单。 提交表单的方式有很多,可能会使一个人发疯……而且他们仍然可以。

Today we'll be looking at a simple form that used to be submitted using PHP and how to convert that to Angular. Using Angular for forms was one of those AHA moments for me. Even though it barely scratches the surface of Angular, it helps a user see the potential after seeing forms submitted and understanding the idea of two-way data-binding.

今天,我们将研究一个以前使用PHP提交的简单表单,以及如何将其转换为Angular。 对我而言,使用Angular进行表单是AHA的重要时刻之一。 尽管它几乎不会刮擦Angular的表面,但它可以帮助用户在查看提交的表单并了解双向数据绑定的思想之后看到潜力。

We will look at processing a form with just plain jQuery. The work needed to do this will be primarily in the javascript. We will submit the form, show errors, add error classes, and show/hide messages in javascript.

我们将研究仅使用普通jQuery处理表单。 为此所需的工作将主要在javascript中。 我们将使用javascript提交表单,显示错误,添加错误类以及显示/隐藏消息。

After that, we will be using Angular. The bulk of the work that was needed before will go away and lots of the things we did before (showing errors, adding classes, showing/hiding messages) will be so much simpler. Let's dive in.

之后,我们将使用Angular。 之前需要做的大部分工作都将消失,而我们之前做过的许多事情(显示错误,添加类,显示/隐藏消息)将变得非常简单。 让我们潜入。

Submitting Forms with Just jQuery and AJAX: If you want to view a full article on submitting a form just using jQuery and AJAX, view our other article: 仅使用jQuery和AJAX提交表单 :如果要查看有关仅使用jQuery和AJAX提交表单的完整文章,请查看我们的另一篇文章: Submitting AJAX Forms with jQuery. 使用jQuery提交AJAX表单 。

我们的样品表格 (Our Sample Form)

We'll be looking at two ways to submit this form: * The Old Way: AJAX Form with jQuery and PHP * The New Way: AJAX Form with AngularJS and PHP

我们将研究两种方式提交此表单:* 旧方法:使用jQuery和PHP的AJAX表单* 新方法:使用AngularJS和PHP的AJAX表单

Take a look at what we'll be building. Super simple.

看一下我们将要构建的东西。 超级简单。

submitting-forms-with-angular

表格要求 (Form Requirements)

  • Process the form without page refresh

    处理表单而不刷新页面
  • Enter Name and Superhero Alias

    输入名称超级英雄别名
  • Show errors if there are any

    显示错误(如果有)
  • Turn inputs red if there are errors

    出现错误时将输入变为红色
  • Show success message if all is good

    如果一切顺利,则显示成功消息

档案结构 (File Structure)

We'll only need two files to demonstrate.

我们只需要两个文件即可演示。

  • index.html

    index.html
  • process.php

    process.php

处理表格 (Processing the Form)

Let's setup the PHP to process our form. This will be very minimal and will use http POST to get the form data.

让我们设置PHP以处理表单。 这将是最小的,并将使用http POST来获取表单数据。

Processing the Form: This won't be that important to us. You can use any other language to process your form that you like. 处理表格:这对我们来说并不重要。 您可以使用任何其他语言来处理您喜欢的表单。

This is a very simple form processing script. We will just check if the data exists. If it does exist, don't do anything. If it doesn't exist, add a message to our $errors array.

这是一个非常简单的表单处理脚本。 我们将只检查数据是否存在。 如果确实存在,则不执行任何操作。 如果不存在,请向$errors数组中添加一条消息。

To return our data to an AJAX call, we have to echo and json_encode. This is all we have to do with our PHP form processing. It will be the same for processing a form using normal jQuery AJAX or Angular.

要将数据返回到AJAX调用,我们必须echojson_encode 。 这就是我们与PHP表单处理所要做的全部。 使用普通的jQuery AJAX或Angular处理表单将是相同的。

显示表格 (Showing the Form)

Let's create our HTML that will show our form.

让我们创建显示表单HTML。



Angular Forms



Submitting Forms with Angular

Now we have our form. We've also used Bootstrap to make it not super ugly. Using the Bootstrap syntax, there is also a spot under each input to show a line of text for our errors.

现在我们有了表格。 我们还使用了Bootstrap使其变得不难看。 使用Bootstrap语法,每个输入下方都有一个点,用于显示错误的一行文本。

submitting-forms-with-angular

用jQuery提交表单 (Submit the Form with jQuery)

Let's go through the process of submitting the form with jQuery now. We will add all the code into our empty ...

There's a lot of code here to process the form. We have code to get the variables from the form, send it to our form using AJAX, check if there are any errors, and show a success message. On top of all that, every time the form is submitted, we want it to clear the past errors. Quite a lot of code.

这里有很多代码来处理表单。 我们有代码从表单中获取变量使用AJAX将其发送到表单检查是否有任何错误 ,并显示成功消息 。 最重要的是,每次提交表单时,我们希望它清除过去的错误。 很多代码。

Now if there is an error:

现在,如果有错误:

submitting-error]

Or if there is a successful submission:

或者,如果提交成功:

submit-success

Now let's look at the same form, submitted with Angular. Remember, we don't have to change anything about how our PHP processes the form and our application will function the same (showing errors and successes in the same places).

现在让我们看一下与Angular一起提交的表单。 记住,我们无需更改有关PHP如何处理表单以及应用程序将运行相同功能的任何信息(在同一位置显示错误和成功信息)。

用Angular提交表单 (Submit the Form with Angular)

We are going to set up our Angular application right inside the ...

We now have the foundation for our Angular app. We've loaded Angular, created a module and controller, and applied it to our site.

现在,我们为Angular应用奠定了基础。 我们已经加载了Angular,创建了模块和控制器,并将其应用于我们的网站。

Next we will be showing off how 2-way binding works.

接下来,我们将展示2向绑定的工作原理。

2向数据绑定 (2-Way Data-Binding)

This is one of the core ideas of Angular and one of its most powerful. From the Angular docs: "Data-binding in Angular web apps is the automatic synchronization of data between the model and view." This means that the step where we have to grab data from the form using $('input[name=name]').val() is not required.

这是Angular的核心思想之一,也是最强大的思想之一。 来自Angular文档 :“ Angular Web应用程序中的数据绑定是模型和视图之间的数据自动同步。” 这意味着不需要执行必须使用$('input[name=name]').val()从表单中获取数据的步骤。

We bind data to a variable in Angular, and whenever it changes in either the Javascript or in the view, it changes in both.

我们将数据绑定到Angular中的变量,并且只要它在Javascript或视图中发生变化,它在两者中都会发生变化。

To demonstrate data-binding, we'll get our form inputs to populate a variable formData automagically. Let's look back at our Angular controller that we applied to our page. We passed in $scope and $http.

为了演示数据绑定,我们将获取表单输入以自动填充变量formData 。 让我们回顾一下应用于页面的Angular控制器。 我们传入了$scope$http

$scope: The glue between application controller and the view. Basically variables are passed to and from our controller and view using $scope. For a more detailed definition, check out the docs.
$http: The Angular service that will help us do our POST request. For more information, check out the docs.

$ scope:应用程序控制器和视图之间的粘合剂。 基本上,变量是通过$ scope传入和传出我们的控制器并进行查看的。 有关更详细的定义,请查看docs 。
$ http: Angular服务,它将帮助我们完成POST请求。 有关更多信息,请查看docs 。

使用数据绑定获取变量 (Getting the Variables Using Data-Binding)

Alright, enough talk. Let's apply this information to our form. It's way simpler than it sounded above. We will add a line to the Angular controller and a line to the view.

好吧,足够多的谈话。 让我们将此信息应用于表单。 它比上面听起来简单。 我们将向Angular控制器添加一条线,并向视图添加一条线。

...

                        
                    

相关文章