Guest User

Untitled

a guest
Apr 9th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. <!doctype html>
  2.  
  3. <html>
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>OnClick Insert Radio Button value into Database using PDO in
  7. Jquery Ajax
  8. PHP | SoftAOX Tutorial</title>
  9. <script
  10.  
  11. src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js">
  12. </script>
  13. </head>
  14. <body>
  15. <h1> On Click Insert Radio Button Value into Database</h1>
  16. <input type="radio" name="gender" value="Male">Male<br/><br/>
  17. <input type="radio" name="gender" value="Female">Female<br/><br/>
  18. <input type="radio" name="gender" value="Others">Others<br/>
  19. <input type="submit"><br/>
  20. <h3 id="result"></h3>
  21. <br/>
  22. <script>
  23. $(document).ready(function(){
  24. $('input[type="submit"]').click(function(){
  25. var gender = $(this).val();
  26. $.ajax({
  27. url:"insert.php",
  28. method:"POST",
  29. data:{gender:gender},
  30. success: function(data){
  31. $('#result').html(data);
  32. }
  33. });
  34. });
  35. });
  36. </script>
  37. </body>
  38. </html>
  39.  
  40.  
  41.  
  42.  
  43. <?php
  44. //Insert Data
  45. $hostname = "localhost";
  46. $username = "root";
  47. $password = "";
  48. $databasename = "tut";
  49. try
  50. {
  51. $conn = new PDO("mysql:host=$hostname;dbname=$databasename",$username,
  52. $password);
  53. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  54. if(isset($_POST["gender"]))
  55. {
  56. $query = "INSERT INTO gender(gender) VALUES (:gender)";
  57. $statement = $conn->prepare($query);
  58. $statement->execute(
  59. array(
  60. 'gender' => $_POST["gender"]
  61. )
  62. );
  63. $count = $statement->rowCount();
  64. if($count > 0)
  65. {
  66. echo "Data Inserted Successfully..!";
  67. }
  68. else
  69. {
  70. echo "Data Insertion Failed";
  71. }
  72. }
  73. }
  74. catch(PDOException $error)
  75. {
  76. echo $error->getMessage();
  77. }
  78. ?>
Add Comment
Please, Sign In to add comment