Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <input type="text" id="IDAlim">
- <div class="col-md-6">
- <section>
- <li id="demo"></li>
- </section>
- </div>
- <script src="jsjquery.js"></script>
- <script type="text/javascript">
- var list = document.getElementById('demo');
- $(document).ready(function()
- {
- putIn();
- function putIn() {
- var focused = $(':focus');
- $("#IDAlim").focus();
- focused.focus();
- var IDAlim = document.getElementById('IDAlim').value;
- var quantite = "1";
- var entry = document.createElement('li');
- if(IDAlim != "")
- {
- $.post('validation.php',{IDAlim: $('#IDAlim').val()}, function(data){
- if(data.exists){
- alert('Is in DB');
- $("#demo").append('<li class="list-group-item">'+IDAlim+' '+quantite+'x'+'</li>');
- }else{
- alert('Re-Scan please');
- }
- }, 'JSON');
- setTimeout(function(){ putIn() }, 800);
- $("#IDAlim").val('');
- }
- else
- {
- entry.appendChild(document.createTextNode(IDAlim));
- setTimeout(function(){ putIn() }, 800);
- $("#IDAlim").val('');
- }
- }
- })
- </script>
- <?php
- //set the headers to be a json string
- header('content-type: text/json');
- //no need to continue if there is no value in the POST IDAlim
- if(!isset($_POST['IDAlim']))
- echo json_encode(array('non' => 'POSTError'));
- //Variable for db connection
- $host="localhost";
- $user="root";
- $pass="";
- $dbname="aliments";
- try
- {
- $dbcon = new PDO("mysql:host={$host};dbname={$dbname}",$user,$pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
- $query = $dbcon->prepare('SELECT nom FROM tblaliments WHERE nom = :nomAlim');
- $query->bindParam(':nomAlim', $_POST['IDAlim']);
- $query->execute();
- //return the json object containing the result of if the IDAlim exists or not. The $.post in my jquery will access it.
- echo json_encode(array('exists' => $query->rowCount()));
- }
- catch (Exception $e)
- {
- echo json_encode(array('non' => 'PDOError'));
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement