I am trying to submit a number in a text field, pass the number through mySql queries on a separate php page and reset the textbox to empty. All without leaving the page with the user’s textbox. When i submit numbers the textbox resets but data is only added to the database when the same number is entered twice in a row. I cant figure out why this is, I hope it’s something obvious and really need some help!!! Is there anything in the code below that might cause this??
<script type=“text/javascript”>
function update(value1)
{
doAjax(“behind_scan.php” , “id=”+value1);
}
function doAjax(url , str )
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert (“Browser does not support HTTP Request”);
return;
}
url=url+“?”+str;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open(“GET”,url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
//document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
//alert(xmlhttp.responseText)
document.forms.myform.scanner.value = “”;
document.forms.myform.scanner.focus();
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject(“Microsoft.XMLHTTP”);
}
return null;
}
</script>
here is the tag for the textbox:
<input type=“text” id=“scanner” name=“scanner” onchange=“update(this.value)” />
have you tried using onBlur?
<input type=“text” id=“scanner” name=“scanner” onBlur=“update(this.value)” />
You can also set an alert in the update function so it show you what is being passed.
function update(value1)
{
alert('this is my value1:'+value1);
//doAjax("behind_scan.php" , "id="+value1);
}
thanks for your suggestions, i tried OnBlur but this didnt solve the problem, when i used instead of onChange, nothing wrote to the DB, the alert message was helpful because it showed me that the value wasnt always being passed on, sometimes after i submitted the text box, it just refreshed to blank without any alert. Very confusing, i would prefer an error message to help me find a solution!!! Do you have any idea as to why the value wouldnt be passed every time??
It never passes first time round and after that it is usually every second or third time?
Really appreciate your post, have put it up on a few forums and out of a total 182 people who have viewed it you are my first reply!!!
You should post a minimal, yet complete working example that exhibits the behavior. What you’re describing is odd, which makes me think you have other code contributing to the issue.
But for now, my guess is that you might misunderstand exactly what constitutes the change event. The form element needs to lose focus for the event to fire. The value of the form element at this time, must be a different value compared to when it gained focus.
Im really sorry for my lack of knowledge in this, you’re right, i dont fully understand what you are saying and i created this script by modifying various tutorials, i am new to this and I dont use ajax anywhere else on the website besides here. The problem is that this particular section is crucial to the workings of it.
Are you saying that i cant regain focus on the ‘scanner’ text box after passing a number thru it? The reason its like that is because its a script for an inventory check. I want to be able to load the scan page and scan a number of products without touching the keyboard.
i.e scan a product ->Value passed -> db amended -> blank textbox ready for next product.
is this possible??

Please tell me it is!!
<?php
require('config.php');
if(isset($_REQUEST['id']))
{
$query = mysql_query("select * from table where barcode_no = '".$_REQUEST['id']."'");
if(mysql_num_rows($query) > 0)
{
$result = mysql_fetch_array($query);
if($result["balance"] >= 5)
{
mysql_query("update table set integer = balance - 10 where barcode_no='".$_REQUEST['id']."'");
mysql_query("insert into scan_history set barcode_no='".$_REQUEST['id']."' , time=NOW()");
}
}
}
?>
this is the other page that communicates with the database and the 1st page(with the AJAX) Getting worried. Still trying to fix it myself :mad: :mad: :mad:
A web browser will show a cursor in an input type=text element while that element has focus if that helps you understand the change event. But, the change event probably isn’t suitable to use here for handsfree operation. I’ve never used a scanner as an input device for a web browser, but I would imagine the element never loses focus, so the change event probably won’t fire.
It would probably be more appropriate to simply poll the value every 50 milliseconds or so, using setInterval(). If the value is not equal to the last submitted value, then submit.
using a scanner as an input device is not too bad. When u scan a barcode, the number simply appears and the equivalent of ‘enter’ is pressed after the last digit is inputted. i.e if you open ms Word and scan a barcode, the numbers appear on the first line and the cursor moves to the second line when the number has been inputted. Thats why i thought this would be problem free.
How wrong was I, i need to present it on friday and the main part of the site aint workin!!! I could be in trouble

Good news. If an input type=text form element has focus, and the enter key is pressed, the web browser submits the form. This means you don’t need ajax at all. All you need is this
<form method="post" action="foo.php">
<input id="id" name="id">
</form>
<script>
document.getElementById("id").focus();
</script>
foo.php simply redirects back to this url.
THANK YOU SO MUCH!!!:D:D:D
couldnt stop smilin once it worked. the php page didnt redirect back the 1st page but that was sorted with
print "<script>";
print " self.location='scan.php';";
print "</script>";
I would never have used your method and appreciate your time in helping me solve this problem