Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. $(document).ready(function()
  2. {
  3. $("#display").change(function()
  4. {
  5. var type = document.getElementById('display').value;
  6. alert(type);
  7. $.ajax(
  8. {
  9. //create an ajax request to load_page.php
  10. type: "GET",
  11. url: "DBOperations.php",
  12. data : "type=" +type,
  13. dataType: "json", //expect text to be returned
  14. success: function(response)
  15. {
  16. //$("#response").html(response);
  17. //alert(response.);
  18.  
  19. $.each(response, function(index, element)
  20. {
  21. $('#response').html($(
  22. {
  23. text: element.name
  24. }));
  25. });
  26.  
  27. },
  28. error: function(jqXHR, textStatus, errorThrown)
  29. {
  30. alert('error: ' + textStatus + ': ' + errorThrown);
  31. }
  32.  
  33. });
  34. });
  35. });
  36.  
  37. PHP:
  38.  
  39. try
  40. {
  41. $dsn = 'mysql:host=localhost;dbname=practice_db'; //your host and database name here.
  42. $username = 'root';
  43. $password = '';
  44.  
  45. //Connect to database
  46. $conn = new PDO($dsn, $username, $password);
  47. $query = "SELECT * FROM client WHERE client_type = :client_type";
  48.  
  49. //Prepare and Execute Query
  50. $stmt = $conn->prepare($query);
  51. $stmt->bindParam(':client_type', $type);
  52. $stmt->execute();
  53. //echo 'Here: ' .$stmt;
  54. //$rows = $stmt->fetch();
  55. $rows = $stmt->fetchAll();
  56. foreach ($rows as $row)
  57. {
  58. echo "ClientID: ".$row['client_id'] . " ";
  59. echo "Name: ".$row['client_name'] . " ";
  60. echo "Title: ".$row['client_title'] . " ";
  61. echo "Client Type: ".$row['client_type'] . "<br>";
  62. }
  63.  
  64. //Display associative array
  65. echo '<pre>';
  66. print_r($rows) .'<br>';
  67. header('Content-type: application/json');
  68. json_encode($rows);
  69. print_r(json_encode($rows));
  70. }
  71. catch (PDOException $ex)
  72. {
  73. echo "There was a problem executing the Query: " . $ex->getMessage();
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement