Advertisement
Guest User

Untitled

a guest
Oct 11th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. <input type="text" id="IDAlim">
  2.  
  3. <div class="col-md-6">
  4. <section>
  5. <li id="demo"></li>
  6. </section>
  7. </div>
  8.  
  9. <script src="jsjquery.js"></script>
  10. <script type="text/javascript">
  11.  
  12. var list = document.getElementById('demo');
  13. $(document).ready(function()
  14. {
  15.  
  16. putIn();
  17. function putIn() {
  18.  
  19. var focused = $(':focus');
  20. $("#IDAlim").focus();
  21. focused.focus();
  22.  
  23. var IDAlim = document.getElementById('IDAlim').value;
  24. var quantite = "1";
  25. var entry = document.createElement('li');
  26.  
  27. if(IDAlim != "")
  28. {
  29. $.post('validation.php',{IDAlim: $('#IDAlim').val()}, function(data){
  30. if(data.exists){
  31. alert('Is in DB');
  32. $("#demo").append('<li class="list-group-item">'+IDAlim+' '+quantite+'x'+'</li>');
  33. }else{
  34. alert('Re-Scan please');
  35. }
  36. }, 'JSON');
  37.  
  38. setTimeout(function(){ putIn() }, 800);
  39. $("#IDAlim").val('');
  40. }
  41. else
  42. {
  43. entry.appendChild(document.createTextNode(IDAlim));
  44. setTimeout(function(){ putIn() }, 800);
  45. $("#IDAlim").val('');
  46. }
  47.  
  48. }
  49.  
  50.  
  51. })
  52. </script>
  53.  
  54. <?php
  55.  
  56. //set the headers to be a json string
  57. header('content-type: text/json');
  58.  
  59. //no need to continue if there is no value in the POST IDAlim
  60. if(!isset($_POST['IDAlim']))
  61. echo json_encode(array('non' => 'POSTError'));
  62.  
  63.  
  64.  
  65. //Variable for db connection
  66. $host="localhost";
  67. $user="root";
  68. $pass="";
  69. $dbname="aliments";
  70.  
  71. try
  72. {
  73. $dbcon = new PDO("mysql:host={$host};dbname={$dbname}",$user,$pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  74.  
  75. $query = $dbcon->prepare('SELECT nom FROM tblaliments WHERE nom = :nomAlim');
  76.  
  77. $query->bindParam(':nomAlim', $_POST['IDAlim']);
  78.  
  79. $query->execute();
  80.  
  81. //return the json object containing the result of if the IDAlim exists or not. The $.post in my jquery will access it.
  82. echo json_encode(array('exists' => $query->rowCount()));
  83. }
  84. catch (Exception $e)
  85. {
  86. echo json_encode(array('non' => 'PDOError'));
  87. }
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement