In this tutorial, I am going to show you, How we can load .php file using jquery.
To load .php file, we simply use load() function to call php file in a div.
Here is the example how i load php file with jquery load() function
Database Name: `load`
Table structure for table `categories`
CREATE TABLE IF NOT EXISTS `categories` (`cat_id` int(100) NOT NULL,
`cat_title` varchar(500) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;
Dumping data for table `categories`
INSERT INTO `categories` (`cat_id`, `cat_title`) VALUES(3, 'What is html?'),
(4, 'What is css?'),
(5, 'What is php?\r\n'),
(10, 'What is linux?'),
(13, 'toit');
db.php
- <?Php
- $dbhost = 'localhost';
- $dbname = 'load';
- $dbuser = 'root';
- $dbpass = '';
- try{
- $dbcon = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
- $dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }catch(PDOException $ex){
- die($ex->getMessage());
- }
- ?>
index.php
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>onlinestudy.bramento.com</title>
- <script type="text/javascript">
- $(document).ready(function() {
- $("#results").load("getcat.php"); //using load() functions here
- });
- </script>
- </head>
- <h1> How we can load .php file using jquery.</h1>
- <div id="results"></div>
- <body>
- </body>
- </html>
getcat.php
- <?php
- require_once 'db.php';
- $stmt = $dbcon->prepare('SELECT * FROM categories');
- $stmt->execute();
- while($row=$stmt->fetch(PDO::FETCH_ASSOC))
- {
- extract($row);
- ?>
- <p> <?php echo $row['cat_title']; ?></p>
- <?php
- }
- ?>
No comments:
Post a Comment