Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/308676-onchange-redirect-doesnt-work/
Share on other sites

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. 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.