Detailed analysis of how to submit a form using ANGULARJS programming

Source: Internet
Author: User
Tags button type min php form php script

This article mainly introduces the detailed analysis of the use of Angularjs submission form, Angularjs is a very popular JavaScript library, the article shows the ANGULARJS in front and back-end of the PHP interaction scene, the need for friends can refer to the

Before the advent of ANGULARJS, many developers faced the problem of submitting forms. Because of the complexity and variety of forms presented, it's easy to go crazy ... Now, however, it still makes people mad.

Today, we'll look at the forms that were submitted in the past using PHP, and now how to convert them to use angular submissions. Using angular to process forms, for me, is a "aha" Moment (translator: The joy of knowing or discovering something). Even though it doesn't even involve much of the angular surface, it helps users see the potential of the form submission and understand the two ways of data binding.

We will use the jquery platform to do this process. So the work to do is to use JavaScript first. We submit the form, display the error message, add the error class, and display and hide the information in JavaScript.

After that, we will use angular. Before we use it, we need to do most of the work we need, and a lot of the work we've done before will be very easy. Let's get started.

A simple form

We'll look at two ways to submit a form:

Old method: jquery and PHP Submission Form

New method: Angularjs and PHP submit the form

First look at our form, Super simple:

form requirements

Implementing a page without refreshing form processing

Enter name and superhero alias

If there is an error, display the error prompt

If the input is incorrect, turn the input into red

If all content OK, show success tips

Document structure

In our presentation, we only need two files



Form processing

Let's create a new PHP to process the form. The page is very small and submits data using post.

Working with forms: it's not that important to us. You can use other languages you like to process your form.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The //process.php   <?php   $errors = AR Ray (); Array to hold validation errors $data = Array (); Array to pass back data  //Validate the variables ====================================================== if (EMP Ty ($_post[' name ')) $errors [' name '] = ' name is required. ';   if (Empty ($_post[' Superheroalias ')) $errors [' superheroalias '] = ' superhero alias is required. ';  //Return a response ===========================================================  //Response if there are ER Rors if (! empty ($errors)) { //If there are items in we errors array, return those errors $data [' success '] = FAL Se $data [' errors '] = $errors; else { //If there are no errors, return a message $data [' success '] = true; $data [' message '] = ' success! ';} &nbs P Return ALL We data to a AJAX call echo Json_encode ($data);

This is a very simple form-processing script. We only check that the data exists, and if it does, no processing or manipulation; if not, you need to add a piece of information to the $errors array.

In order to return our data for Ajax calls, we need to use echo and Json_encode. That's all we need to do with the PHP form. The same is true with common jquery ajax or angular processing forms.

Show form

Let's create an HTML to show our form


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48, 49 50 51 52 53 54 55 56 57 58 59 &lt;!--index.html--&gt; &nbsp; &lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;angular forms&lt;/title& Gt &nbsp; &lt;!--LOAD BOOTSTRAP CSS--&gt; &lt;link rel= "stylesheet" href= "//" /bootstrap.min.css "&gt; &nbsp; &lt;!--LOAD JQUERY--&gt; &lt;!--when building a angular app, you generally does not want To with jquery--&gt; &lt;!--We are breaking this is here because jquery ' s $.param'll help us send data to our PHP SCR IPT So, PHP can recognize it--&gt; &lt;!--this are jQuery ' s only use. Avoid it in angular apps and if anyone has tips at how to send data to a PHP script w/o JQuery ments--&gt; &lt;script src= "//" &gt;&lt;/script&gt; &nbsp; &lt;!--PROCESS FORM with AJAX (old)--&gt; &lt;script&gt; &lt;!--WE'll PROCESS our form here--&gt; &lt;/script&gt; &L t;/head&gt; &lt;body&gt; &lt;div class= "container" &gt; &lt;div class= "col-md-6 CoL-md-offset-3 "&gt; &nbsp; &lt;!--PAGE TITLE--&gt; &lt;div class=" Page-header "&gt; &lt;h1&gt;&lt;span class=" Glyphicon Glyphicon-tower "&gt;&lt;/span&gt; submitting Forms with angular&lt;/h1&gt; &lt;/div&gt; &nbsp; &lt;!--show error/ SUCCESS MESSAGES--&gt; &lt;div id= "MESSAGES" &gt;&lt;/div&gt; &nbsp; &lt;!--FORM--&gt; &lt;form&gt; &lt;!--NAME--&gt; &lt;div id= "Name-group" class= "Form-group" &gt; &lt;label&gt;Name&lt;/label&gt; &lt;input type= "text" name= "name" class= "Form-control" placeholder= "Bruce Wayne" &gt; &lt;span class= "help-block" &gt;&lt;/span&gt; &lt;/div&gt; &nbsp; &lt;!--superhero NAME--&gt; &lt;div id= "Superhero-group" class= "Form-group" &gt; &lt;label&gt;superhero Alias&lt;/ label&gt; &lt;input type= "text" name= "Superheroalias" class= "Form-control" placeholder= "caped Crusader" &gt; &lt;span class= "Help-block" &gt;&lt;/span&gt; &lt;/div&gt; &nbsp; &lt;!--submit button--&gt; &lt;button type= "Submit" class= " BTN btn-success btn-lg btn-block "&gt; &lt;span class=" Glyphicon GLYphicon-flash "&gt;&lt;/span&gt; submit! &lt;/button&gt; &lt;/form&gt; &nbsp; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;

Now, we have the form. We have also used bootstrap to make the form look less ugly. Using the bootstrap syntax rule, each input contains a spot to display the error message for our text.

Submit a form using jquery

Now let's use jquery to process the form submission. I'll add all the code to the empty

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.