Guest User

Untitled

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. <form class="form-inline" method="post" >
  2. <div id="div_id_value" class="form-group">
  3.  
  4. <input class="numberinput form-control"
  5. id="value" name="value"
  6. placeholder="Value (mmol/L)"
  7. required="True" type="number" step="any" min="0"/>
  8. </div>
  9.  
  10.  
  11. <div id="div_id_category" class="form-group">
  12.  
  13. <select id="id_category" name="category">
  14. <option value="Breakfast">Breakfast</option>
  15. <option value="Lunch" >Lunch</option>
  16. <option value="Dinner">Dinner</option>
  17. <option value="Snack">Snack</option>
  18. <option value="Bedtime">Bedtime</option>
  19. <option value="No Category" selected="selected">No Category</option>
  20. </select>
  21. </div>
  22.  
  23. <input type="submit" name="submit" value="Quick Add" id="quick">
  24. </form>
  25.  
  26. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
  27.  
  28.  
  29. $("#quick").click(function() {
  30.  
  31. var sugar2 = $("#value").val();
  32. var category2 = $("#category").val();
  33.  
  34. $.ajax({
  35. type:'POST',
  36. url:'quick.php',
  37. data:{ 'sugar': sugar2, 'category':category2},
  38. dataType:'json',
  39. success: function(output) {
  40. alert(output);
  41. };
  42. });
  43. });
  44.  
  45. <?php
  46. session_start();
  47. include 'conn.php';
  48.  
  49. if (isset($_POST['sugar'])) {
  50.  
  51. $sugar = $_POST['sugar'];
  52. $category = $_POST['category'];
  53. $email= $_SESSION['email'];
  54.  
  55. $query = "INSERT INTO data (email, sugar, category)
  56. VALUES($email, $sugar, $category )";
  57.  
  58.  
  59. if(mysqli_query($link, $query)) {
  60.  
  61. header("Location: index.php");
  62.  
  63.  
  64. };
  65. };
  66. ?>
  67.  
  68. $("#quick").submit(function(evt) { ... });
  69. //or
  70. $("#quick").on('submit', function(evt) { ... });
  71.  
  72. <?php
  73. session_start();
  74. include 'conn.php';
  75.  
  76. if (isset($_POST['sugar'])) {
  77.  
  78. $sugar = $_POST['sugar'];
  79. $category = $_POST['category'];
  80. $email= $_SESSION['email'];
  81.  
  82. $query = "INSERT INTO data (email, sugar, category)
  83. VALUES($email, $sugar, $category )";
  84.  
  85. //save the output of the result
  86. $result = mysqli_query($link, $query);
  87.  
  88. //according to the docs, mysqli_query() will return false on failure
  89. //check for false
  90.  
  91. if( $result === false ) {
  92.  
  93. //the query failed
  94. //provide a response to the AJAX success function
  95. echo "Query failed. Check the logs to understand why, or try enabling error reporting.";
  96.  
  97. }else {
  98.  
  99. //the query worked!
  100. echo 'All good!'; //provide a response to the AJAX success function
  101.  
  102. }
  103.  
  104. //Kill PHP. Good idea with an AJAX request.
  105. exit;
  106. }
  107.  
  108. <?php
  109. echo "my response";
  110. ?>
Add Comment
Please, Sign In to add comment