Advertisement
Guest User

Untitled

a guest
Mar 9th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "data";
  6.  
  7. try {
  8. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  9. // set the PDO error mode to exception
  10. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11.  
  12. // prepare sql and bind parameters
  13. $stmt = $conn->prepare("INSERT INTO data (data1, data2, data3)
  14. VALUES (:data1, :data2, :data3)");
  15. $stmt->bindParam(':data1', $data1);
  16. $stmt->bindParam(':data2', $data2);
  17. $stmt->bindParam(':data3', $data3);
  18.  
  19. echo "Done";
  20. }
  21. catch(PDOException $e)
  22. {
  23. echo "Error: " . $e->getMessage();
  24. }
  25. $conn = null;
  26. ?>
  27.  
  28. Then you also need some javascript to send the values from the form to your PHP file. You can use XMLHttpServer, or you can use the jQuery library and do this:
  29.  
  30. $.ajax({
  31. url: "phpfile.php",
  32. data: $("inputfield").val(),
  33. })
  34. .done(function( data ) {
  35. if ( data == "Done" ) {
  36. console.log("Data was sent to server!");
  37. }
  38. }.fail(function( data) {
  39. console.log("Something went wrong...");
  40. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement