Simple jquery without refreshing form submission example

Source: Internet
Author: User
Tags html form php file

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>

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: info-contact@alibabacloud.com 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.