Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function()
- {
- $("#display").change(function()
- {
- var type = document.getElementById('display').value;
- alert(type);
- $.ajax(
- {
- //create an ajax request to load_page.php
- type: "GET",
- url: "DBOperations.php",
- data : "type=" +type,
- dataType: "json", //expect text to be returned
- success: function(response)
- {
- //$("#response").html(response);
- //alert(response.);
- $.each(response, function(index, element)
- {
- $('#response').html($(
- {
- text: element.name
- }));
- });
- },
- error: function(jqXHR, textStatus, errorThrown)
- {
- alert('error: ' + textStatus + ': ' + errorThrown);
- }
- });
- });
- });
- PHP:
- try
- {
- $dsn = 'mysql:host=localhost;dbname=practice_db'; //your host and database name here.
- $username = 'root';
- $password = '';
- //Connect to database
- $conn = new PDO($dsn, $username, $password);
- $query = "SELECT * FROM client WHERE client_type = :client_type";
- //Prepare and Execute Query
- $stmt = $conn->prepare($query);
- $stmt->bindParam(':client_type', $type);
- $stmt->execute();
- //echo 'Here: ' .$stmt;
- //$rows = $stmt->fetch();
- $rows = $stmt->fetchAll();
- foreach ($rows as $row)
- {
- echo "ClientID: ".$row['client_id'] . " ";
- echo "Name: ".$row['client_name'] . " ";
- echo "Title: ".$row['client_title'] . " ";
- echo "Client Type: ".$row['client_type'] . "<br>";
- }
- //Display associative array
- echo '<pre>';
- print_r($rows) .'<br>';
- header('Content-type: application/json');
- json_encode($rows);
- print_r(json_encode($rows));
- }
- catch (PDOException $ex)
- {
- echo "There was a problem executing the Query: " . $ex->getMessage();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement