jepoy928 Posted May 7, 2019 Share Posted May 7, 2019 Good day to you guys. I'm working on an attendance system using a QR code / barcode scanner. Using onChage() functionI want to be redirected to a PHP file where it gets the employee number in the URL and validates the employee number and inserts to attendance table. What's wrong with my code? It does not redirect to my desired page. <html lang="en"> <head> <script> function ValidateEmployeeNumber(emp_num) { var emp_num = document.getElementById("emp_num").value; window.location.replace('validate_attendance.php?emp_num='+emp_num); } </script> </head> <body onload="document.AttendanceForm.ecode.focus();"> <form name="AttendanceForm" method="POST"> <h1>Attendance</h1> <div> <input type="text" id="emp_num" name="emp_num" onChange="ValidateEmployeeNumber(this.value)" class="form-control" placeholder="Employee Number"/> </div> </form> </body> </html> I hope you can guide me guys how to make this work. If you have different approach to the problem that works, I am more than willing to study your code. thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/ Share on other sites More sharing options...
requinix Posted May 7, 2019 Share Posted May 7, 2019 Are there any error messages in your browser's developer console? Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/#findComment-1566489 Share on other sites More sharing options...
jepoy928 Posted May 7, 2019 Author Share Posted May 7, 2019 Let me check. Thanks for the reply Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/#findComment-1566490 Share on other sites More sharing options...
jepoy928 Posted May 7, 2019 Author Share Posted May 7, 2019 There are no error messages. It reads the QR code and barcode but it does not redirect. Should I use other EventListener? Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/#findComment-1566491 Share on other sites More sharing options...
requinix Posted May 7, 2019 Share Posted May 7, 2019 I don't see anything in what you posted that deals with QR codes. How about showing us your actual code? Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/#findComment-1566492 Share on other sites More sharing options...
jepoy928 Posted May 7, 2019 Author Share Posted May 7, 2019 The cursor is in focus within a textbox onLoad(). The QR code or barcode reader automatically pastes the employee number in the textbox. I want the javascript to redirect to another page with the value pasted in the textbox. If you have a better idea how it should be done, I will study it. Thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/#findComment-1566494 Share on other sites More sharing options...
requinix Posted May 7, 2019 Share Posted May 7, 2019 onchange only fires after focus has left. What happens if you click somewhere on the page? Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/#findComment-1566496 Share on other sites More sharing options...
Zane Posted May 7, 2019 Share Posted May 7, 2019 String.replace() takes a second argument, the replacement. You're better off just overwriting the location property. window.location = 'validate_attendance.php?emp_num='+emp_num; Also, what is the point of declaring a function that takes an argument and then overwriting that argument immediately? function ValidateEmployeeNumber(emp_num) { //completely destroy the value for emp_num and create it again? var emp_num = document.getElementById("emp_num").value; window.location = 'validate_attendance.php?emp_num='+emp_num; } If this is the approach you're going to use, then either take the parameter out of the function (and the call) or just don't overwrite the variable. Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/#findComment-1566505 Share on other sites More sharing options...
requinix Posted May 7, 2019 Share Posted May 7, 2019 3 hours ago, Zane said: String.replace() takes a second argument, the replacement. It's Location.replace, not String.replace. Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/#findComment-1566507 Share on other sites More sharing options...
Zane Posted May 8, 2019 Share Posted May 8, 2019 I learn something every day. Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/#findComment-1566513 Share on other sites More sharing options...
requinix Posted May 8, 2019 Share Posted May 8, 2019 Looks like a string, acts like a string, isn't a string. Isn't Javascript wonderful? 1 Quote Link to comment https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/#findComment-1566514 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.