Note that there are some explanatory texts on larger screens.

plurals
  1. POSend and trunc variable without key press (onChange between Ajax and PHP)
    primarykey
    data
    text
    <p>before starting with the code I try to explain what I would like to achieve:</p> <p>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.</p> <ol> <li>I would like to take all these numBers</li> <li>Truncate them because I only need first 4 chars: 1234.</li> <li>Pass this value to the db that will check which item corresponds to this number</li> <li>Add to 'stock' number of the item.</li> <li>Clean the form</li> <li>Repeat as fast as possible.</li> </ol> <p>Ok, now that I tried to explain let's start with the funny part, my code:</p> <p>file 1: change.php</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;style&gt; 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; } &lt;/style&gt; &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form&gt; &lt;fieldset&gt; &lt;label for="troncami"&gt;Type Something:&lt;/label&gt; &lt;input id="troncami" type="text" /&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;div id="risultatotroncato"&gt;&lt;/div&gt; &lt;script&gt; $('#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(); });&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>File 2: troncare.php</p> <pre><code>&lt;?php $risultatotroncato = 0; $risultatotroncato = substr ($_GET['troncami'],0,4); echo $risultatotroncato; ?&gt; </code></pre> <p>Obviusly it doesn't work the only thing I can see is a NOTICE error:</p> <blockquote> <p>Notice: Undefined index: troncami in D:\Locali\xampp\htdocs\combobox\troncare.php on line 6</p> </blockquote> <p>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!</p> <p>(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?)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload