before starting with the code I try to explain what I would like to achieve:
I have an input where the user will write a code (bar code) with a barcode reader with keyboard emulation so he will write something like: 123465789.
- I would like to take all these numBers
- Truncate them because I only need first 4 chars: 1234.
- Pass this value to the db that will check which item corresponds to this number
- Add to 'stock' number of the item.
- Clean the form
- Repeat as fast as possible.
Ok, now that I tried to explain let's start with the funny part, my code:
file 1: change.php
<!DOCTYPE html>
<html>
<head>
<style>
fieldset { margin-bottom: 1em; }
input { display: block; margin-bottom: .25em; }
#print-output {
width: 100%;
}
.print-output-line {
white-space: pre;
padding: 5px;
font-family: monaco, monospace;
font-size: .7em;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<form>
<fieldset>
<label for="troncami">Type Something:</label>
<input id="troncami" type="text" />
</fieldset>
</form>
<div id="risultatotroncato"></div>
<script>
$('#troncami').keyup(function(event) {
var str = $(this).serialize();
$.ajax({
type: "GET", // the kind of data we are sending
url: "troncare.php", // this is the file that processes the form data
data: str, // this is our serialized data from the form
success: function(msg){ // anything in this function runs when the data has been successfully processed
// this sets up our notification area for error / success messages
$("#risultatotroncato").ajaxComplete(function(event, request, settings)
{
result = msg; // msg is defined in sendmail.php
$(this).html(result); // display the messages in the #note DIV
});
$('input[name=troncami]').val(msg);
}
});
}).keydown(function(event) {
if (event.which == 13) {
event.preventDefault();
}
});
$('#other').click(function() {
$('#target').keyup();
});</script>
</body>
</html>
File 2: troncare.php
<?php
$risultatotroncato = 0;
$risultatotroncato = substr ($_GET['troncami'],0,4);
echo $risultatotroncato;
?>
Obviusly it doesn't work the only thing I can see is a NOTICE error:
Notice: Undefined index: troncami in D:\Locali\xampp\htdocs\combobox\troncare.php on line 6
So my question is how can I pass the value written in the input to $_GET / $_POST so I'll be able to 'manage' it with 'troncare.php' ? How can I do it as fast as possible so the user will be able to 'shot' with the barcode scanner without stopping and have the value saved on the DB in, almost, 'real time' ? Thank you very much!
(As you probably already know I'm still studying PHP and AJAX and jQuery so in a couple of years/months I'll be able to do it my by self but... I need it ASAP so, could you please help me?)