Example 1
HTML file:
The code is as follows: |
Copy code |
<! DOCTYPE html> <Html> <Head> <Script src = "/jquery-latest.js"> </script> <Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"> </Head> <Body> <Form action = "" id = "searchForm"> Enter a number to test <input type = "number" name = "s" placeholder = "Enter a number..."/> + <Input type = "number" name = "B" placeholder = "Enter a number..."/> <Input type = "button" value = "POST" id = "su"/> </Form> <! -- The result of the search will be rendered inside this div --> <Div id = "result"> </div> <Script> $ (Function (){ $ ("# Su"). click (function (){ // Var form = $ ('# searchForm'). serialize (); Var s = $ ("input: [name = s]"). val (); Var B = $ ("input: [name = B]"). val (); $. Post ("test. php", {s: s, B: B}, function (data ){ $ ("# Result" pai.html (data) }); // Alert ("fsdf "); }); }); </Script> </Body> </Html> Php file: <? Php // Print_r ($ _ POST ); // $ I =$ _ POST; $ S = $ _ POST ['s ']; $ B = $ _ POST ['B']; $ I = $ s + $ B; Echo $ I; ?> |
Example 2
This example is submitted using jquery. form. js form and passed by action.
Added a project to determine whether to fill in the project
Directly replace the ID of this page
Use jquery. form. js
Main code
The code is as follows: |
Copy code |
<Script src = ".../jquery. js"> </script> <Script src = "jquery. form. js"> </script> <Script> // Prepare the form when the DOM is ready $ (Document). ready (function (){ // Bind form using ajaxForm $ ('# HtmlForm'). ajaxForm ({ BeforeSubmit: checkForm, // Run function verification before submission // Target identifies the element (s) to update with the server response Target: '# htmlExampleTarget ', // Success identifies the function to invoke when the server response // Has been encoded ed; here we apply a fade-in effect to the new content Success: function (){ $ ('# Message'). attr ({"value": ""}); // clear the form after the content is submitted $ ('# HtmlExampleTarget'). fadeIn ('low '); } }); Function checkForm (){ If(document.html Form. bigclassid. value = "") { Alert ("Please choose product item "); Document.html Form. bigclassid. focus (); $ ('# HtmlForm'). preventDefault (); // prevents form submission } } }); </Script> </Head> <Form id = "htmlForm" action = "OK. php" method = "get" name = "htmlForm"> <Input id = "bigclassid" name = "bigclassid"> Message: <input type = "text" name = "message" value = "" id = "message"/> <Input type = "submit" value = "OK"/> </Form> <Div id = "htmlExampleTarget"> </Div> |