Sometimes we want to send data without refreshing page.
Solution is simple. Using jQuery post() method we can send different types of data (variables, arrays, forms, serialized data, etc.) and after that we can for example write these data to the database.
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jQuery post method</title> <script type="text/javascript"> $(document).ready(function() { $('#J_action_sendPersonSalary').click( function() { var personId = 34; var personSalary = 2000; $.post( "person.php", {personId: personId, personSalary: personSalary} ); }); }); </script> </head> <body> <a id="J_action_sendPersonSalary" href="javascript:void(0)">Send data with salary</a> </body> </html>
person.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $personId = $_POST['personId']; $personSalary = $_POST['personSalary']; // Here we can do something with our data. For example send it to the database. $query = 'INSERT INTO ....'; ?> </body> </html>
No comments:
Post a Comment