Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4.  
  5.  
  6. <?php include('add.php') ?>
  7. <link rel = "stylesheet" href = "styles.css" type = "text/css">
  8. <link href="https://fonts.googleapis.com/css?family=Vollkorn" rel="stylesheet">
  9. <script
  10. src="https://code.jquery.com/jquery-3.1.1.min.js"
  11. integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
  12. crossorigin="anonymous"></script>
  13.  
  14. <title>Тестовое задание Трофимов</title>
  15.  
  16. </head>
  17.  
  18. <body>
  19.  
  20. <div id = "main_container">
  21.  
  22. <div id = "success_message">
  23.  
  24. <p>Товар добавлен в корзину</p>
  25.  
  26. <div id = "tooltip_arrow"></div>
  27.  
  28. </div>
  29.  
  30. <div class = "clearfix"></div>
  31. <div id = "form">
  32. <input name = "submit" type = "button" value = "купить" id = "button">
  33.  
  34. <div class = "quantity_counter" id = "minus"><a href="#" id="decrease">&#8211;</a></div>
  35.  
  36. <input type = "text" size = "3" value = "1" id = "input_quantity" name = "input_quantity"></input>
  37.  
  38. <div class = "quantity_counter" id = "plus"><a href="#" id="increase">&#43;</a></div>
  39.  
  40. </div>
  41. <div class = "clearfix"></div>
  42.  
  43. <div id = "error_message"><br/><p>Укажите количество товаров!</p></div>
  44.  
  45.  
  46. </div>
  47.  
  48.  
  49.  
  50. <script type ="text/javascript" src = "js.js"></script>
  51.  
  52. </body>
  53.  
  54. </html>
  55.  
  56. $(document).ready(function(){
  57.  
  58. var currentValue = 1;
  59.  
  60. $('#increase').click(function(e){
  61. e.preventDefault();
  62. currentValue = parseInt($('#input_quantity').val());
  63. currentValue++;
  64. $('#input_quantity').val(currentValue);
  65. });
  66.  
  67.  
  68. $('#decrease').click(function(e){
  69. e.preventDefault();
  70. currentValue = parseInt($('#input_quantity').val());
  71. currentValue--;
  72. if(currentValue < 1) {
  73. currentValue = 0;
  74. }
  75. $('#input_quantity').val(currentValue);
  76.  
  77.  
  78. });
  79.  
  80.  
  81. function Messager(){
  82. this.showMessage = function(container){
  83. $(container).fadeIn();
  84. setTimeout(function() {
  85. $(container).fadeOut();
  86. }, 3000);
  87. };
  88. this.success = function(){
  89. this.showMessage("#success_message");
  90. };
  91. this.error = function(){
  92. this.showMessage("#error_message");
  93. };
  94. }
  95.  
  96. $('#button').click(function(e){
  97. $.ajax({
  98. url: "add_to_cart.php",
  99. type: 'post',
  100. dataType: 'html',
  101. data: {
  102. id: currentValue,
  103. }
  104. }).done(function(response){
  105.  
  106.  
  107. var msg = new Messager();
  108. if (currentValue != 0) {
  109. msg.success();
  110.  
  111. } else {
  112. msg.error();
  113. }
  114. });
  115. });
  116.  
  117. });
  118.  
  119. <?php
  120.  
  121.  
  122. $servername = "myhost";
  123. $username = "myUsername";
  124. $password = "mypassword";
  125. $dbname = "mydbname";
  126.  
  127. // Create connection
  128. $conn = new mysqli($servername, $username, $password, $dbname);
  129. // Check connection
  130.  
  131. if ($conn->connect_error) {
  132. die("Connection failed: " . $conn->connect_error);
  133. }
  134.  
  135. $currentValue = $_POST['currentValue'];
  136. $date = date('Y/m/d H:i:s');
  137. $sql = sprintf("INSERT INTO test (amount, ip, date)
  138. VALUES ('%s', '%s', '%s')", $currentValue, $_SERVER['REMOTE_ADDR'], $date);
  139.  
  140. if ($conn->query($sql) === TRUE) {
  141. echo '<script type="text/javascript">',
  142. 'Messager();',
  143. '</script>'
  144. ;
  145. } else {
  146. echo "Error: " . $sql . "<br>" . $conn->error;
  147. }
  148.  
  149. $conn->close();
  150. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement