Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
ITGeared
  • Social Media
    • Social Media Submenu
      Instagram
      Snapchat
      Facebook
      Reddit
      TikTok
      Twitter
      LinkedIn
  • Streaming
    • Streaming Submenu
      Twitch
      Vimeo
      Youtube
  • Messaging
    • Messaging Submenu
      Discord
      FaceTime
      iMessage
      Microsoft Teams
      Messenger
      Skype
      Slack
      Telegram
      WhatsApp
      Zoom
  • Computers & Programming
    • Computers & Programming Submenu
      Web Development
      Web Building
      HTML / XHTML
      HTML5
      CSS
      CSS3
      Frontend Development
      JavaScript
      AJAX
      jQuery
      Backend Development
      SQL
      ASP
      ADO
      ASP.NET
      Computers & Networking
      VBScript
      Basic Networking
      Windows
      Windows Server
  • Social Media
    • Instagram
    • Snapchat
    • Facebook
    • Reddit
    • TikTok
    • Twitter
    • LinkedIn
  • Messaging
    • Telegram
  • Streaming
    • Twitch
    • Vimeo
    • YouTube
  • Computers & Programming
    • Backend Development
      • SQL
      • ASP
      • ASP.NET
      • ADO
    • Computers & Networking
      • VBScript
      • Basic Networking
      • Windows
      • Windows Server
    • Frontend Development
      • JavaScript
      • jQuery
      • AJAX
    • Web Development
      • Web Building
      • HTML5
      • CSS3
      • HTML/XHTML
      • CSS
ITGeared
  • Social Media
    • Instagram
    • Snapchat
    • Facebook
    • Reddit
    • TikTok
    • Twitter
    • LinkedIn
  • Messaging
    • Telegram
  • Streaming
    • Twitch
    • Vimeo
    • YouTube
  • Computers & Programming
    • Backend Development
      • SQL
      • ASP
      • ASP.NET
      • ADO
    • Computers & Networking
      • VBScript
      • Basic Networking
      • Windows
      • Windows Server
    • Frontend Development
      • JavaScript
      • jQuery
      • AJAX
    • Web Development
      • Web Building
      • HTML5
      • CSS3
      • HTML/XHTML
      • CSS
Computers & Programming​AJAX​Frontend Development

AJAX and PHP with SQL

Paul BurchBy Paul Burch February 14, 2022February 15, 2022

In this article, we are going to look at how to implement an Ajax solution that uses a PHP page to pull data from a back-end database. In this tutorial, we have a table stored in a MySQL database.

You can replace the PHP code on this page with any other server-side scripting languages that you are familiar with such as ASP or ASP.NET. In addition, the back-end data source does not have to be MySQL.

You can modify the database connection in the example for accessing other database platforms. The concept remains the same.

HTML Example

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        function showEmployee(str) {
            if (str==""){
                document.getElementById("div1").innerHTML="Select an Employee for more details!";
                return;
            }
            var xhr = false;
            if (window.XMLHttpRequest) {
                // IE7+, Firefox, Chrome, Opera, Safari
                xhr = new XMLHttpRequest();
            } 
            else {
                // IE5/IE6
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            if (xhr) {
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        document.getElementById("div1").innerHTML = xhr.responseText;
                    }
                }
                xhr.open("GET", "/demo/ajax_dbquery.php?q="+str, true);
                xhr.send(null);
            }
        }
    </script>
</head>
<body>
    <div>
        <select name="employees" onchange="showEmployee(this.value)">
            <option value="">Select an Employee:</option>
            <option value="3">Frank Ford</option>
            <option value="1">John Smith</option>
            <option value="4">Lisa Stark</option>
            <option value="2">Sally Smart</option>
        </select>
        <div id="div1">Select an Employee for more details!</div>
</div>
</body>
</html>

PHP Example

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Ajax and PHP</title>
</head>
<body>
    <table>
        <?php
        header("Cache-Control: no-cache, no-store, must-revalidate");
        header("Pragma: no-cache");
        header("Expires: Sat, 14 Jan 2012 01:00:00 GMT");
        $hostname="dbserver.com";
        $username="dbUserName";
        $password="dbPassword";
        $dbname="databaseName";
        $usertable="employees";
        $field1 = "empName";
        $field2 = "empTitle";
        $field3 = "empOffice";

        $conn = mysql_connect($hostname,$username, $password);
        if (!$conn) {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db($dbname);

        $query = "SELECT * FROM $usertable WHERE empID = '" .$_GET['q']."'";

        $result = mysql_query($query);

        if($result){
            while($row = mysql_fetch_array($result)){
                echo "<tr><td style='width:100px;'>Name:</td><td>".$row["$field1"]."</td></tr>";
                echo "<tr><td style='width:100px;'>Title:</td><td>".$row["$field2"]."</td></tr>";
                echo "<tr><td style='width:100px;'>Office:</td><td>".$row["$field3"]."</td></tr>";
            }
        }
        mysql_close($conn);
        ?> 
    </table>
</body>
</html>

We used a front-end HTML web page that loads data from a PHP (.php) page via Ajax. When a user selects an item in the dropdown list, the selection is sent to the PHP page using Ajax and the receiving page uses the information passed in the query string.

The value of the query string parameter is sent to the database and the results are sent back to the HTML page on the XmlHttpResponse.responseText property.

Related Posts

How To Host Your Own Dns And/Or Webserver

How to Host Your Own DNS and/or Webserver

February 10, 2022 February 11, 2022
Css Styling Links

CSS Styling Links

February 14, 2022 February 15, 2022
How To Extend The Windows 2008 Evaluation Period Up To 240 Days

How to Extend the Windows 2008 Evaluation Period Up to 240 Days

February 11, 2022 February 11, 2022
Cidr And Subnetting

CIDR and Subnetting

February 9, 2022 February 11, 2022
Html5 Semantic Markup And Page Layouthtml5 Semantic Markup And Page Layout

HTML5 Semantic Markup and Page Layout

February 21, 2022 June 15, 2022
Simple Javascript Image Thumbnail Viewer

Simple JavaScript Image Thumbnail Viewer

February 14, 2022 February 15, 2022

About The Author

Paul Burch

Paul Burch

Paul is a programming enthusiast who loves to write about all things technical. Whether it's networking, operating systems or programming, Paul enjoys delving into the nuts and bolts of technology and explaining it in a way that everyone can understand. When he's not writing articles for ITGeared.com, Paul likes to spend his time tinkering with computers and playing video games.

Leave a Comment Cancel Reply

Your email address will not be published. Required fields are marked *

Paul Burch
Paul BurchAuthor

Paul is a passionate programmer who enjoys writing about all things technical. He likes getting into the nitty-gritty of technology and describing it in a way that anybody can understand.

Latest Posts
What Is Synacor Youtube Tv
What Is Synacor YouTube TV?
October 16, 2023
Why Did Apbassing Quit Youtube
Why Did Apbassing Quit YouTube?
October 16, 2023
How To Upload Mp3 To Youtube
How To Upload MP3 to YouTube
October 16, 2023
Related Posts
What Is Fsmo?
What is FSMO?
May 18, 2022
Ajax And Browser Caching Issues
AJAX and Browser Caching Issues
February 11, 2022
Dns Client Settings For Domain Controllers
DNS Client Settings for Domain Controllers
February 23, 2022

Categories

  • Social Media
  • Messaging
  • Computers & Programming
  • Streaming

About

  • About Us
  • Privacy Policy

Copyright © 2021 - 2025 - ITGeared

Scroll to Top