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
Index.html
process.php
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 |
<!--index.html--> <!doctype html> <html> <head> <title>angular forms</title& Gt <!--LOAD BOOTSTRAP CSS--> <link rel= "stylesheet" href= "//netdna.bootstrapcdn.com/bootstrap/3.0.2/css" /bootstrap.min.css "> <!--LOAD JQUERY--> <!--when building a angular app, you generally does not want To with jquery--> <!--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--> <!--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--> <script src= "//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" ></script> <!--PROCESS FORM with AJAX (old)--> <script> <!--WE'll PROCESS our form here--> </script> &L t;/head> <body> <div class= "container" > <div class= "col-md-6 CoL-md-offset-3 "> <!--PAGE TITLE--> <div class=" Page-header "> <h1><span class=" Glyphicon Glyphicon-tower "></span> submitting Forms with angular</h1> </div> <!--show error/ SUCCESS MESSAGES--> <div id= "MESSAGES" ></div> <!--FORM--> <form> <!--NAME--> <div id= "Name-group" class= "Form-group" > <label>Name</label> <input type= "text" name= "name" class= "Form-control" placeholder= "Bruce Wayne" > <span class= "help-block" ></span> </div> <!--superhero NAME--> <div id= "Superhero-group" class= "Form-group" > <label>superhero Alias</ label> <input type= "text" name= "Superheroalias" class= "Form-control" placeholder= "caped Crusader" > <span class= "Help-block" ></span> </div> <!--submit button--> <button type= "Submit" class= " BTN btn-success btn-lg btn-block "> <span class=" Glyphicon GLYphicon-flash "></span> submit! </button> </form> </div> </div> </body> </html> |
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