Jump to content

Recommended Posts

The below code works and all, but I would like to figure out how to get rid of the global variable. I tried without declaring it as a global variable and it did not work. Any other way to do that? Also, how would I go about returning two variables(right now I am returning $id), how can I return both $id and $first_name from the database.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/reset.css">
<link rel="stylesheet" type="text/css" href="css/structure.css">
<script type="text/javascript">
window.onload = function () {
    var inputs = document.getElementsByTagName('input');
    for (i = 0; i < inputs.length; i++) {
        inputs[i].onkeyup = function () {
            if (this.value.match(/^([0-9][0-9][0-9][0-9][0-9])$/)) {
                document.forms["punch"].submit();
            }
        };
    }
};
$("form").bind("keypress", function (e) {
    if (e.keyCode == 13) {
        $("#btnSearch").attr('value');
        //add more buttons here
        return false;
    }
});
</script>
</head>
<?php
$id = 0;
function getID($staffid){
	$servername = "localhost";
	$username = "root";
	$password = "x";
	$dbname = "hr";
	$conn1 = new mysqli($servername, $username, $password, $dbname);
		if ($conn1->connect_error) {
			die("Connection failed: " . $conn1->connect_error);
		} 
	
		$sql = "SELECT id, first_name, employee_id FROM Employees WHERE employee_id='".$staffid."'";
		$result = $conn1->query($sql);
		global $id;
		if ($result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {
			$id = $row["id"];
		}
		return $id;
		} else {
			return false;
		}
		$conn1->close();
}
if (isset($_POST['staffid'])){
	$staffid = $_REQUEST['staffid'];
	
	if(getID($staffid)) {
		$status = "<font color='green'>Punched</font>";
		echo $id;
	} else {
	$status = "<font color='red'>Invalid ID, try again</font>";
	}
}


?>
<body>

<form class="box login" action="" id="punch" method="post">
<fieldset class="boxBody">
	  <label>Staff ID</label>
	<p><input id="staffid" type="text" autofocus="autofocus" name="staffid"></p>
</fieldset>	
<fieldset class="boxBody2">
	  <center><img src="clock.png" /><br><?php if ($status) {echo $status;} else {echo 'Please enter your Staff ID';}?></center>
		</fieldset>	
</form>

</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/298356-basic-php-questionhelp/
Share on other sites

I changed it to the following, and it is now returning 0, not 1 like it should. Any ideas?

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/reset.css">
<link rel="stylesheet" type="text/css" href="css/structure.css">
<script type="text/javascript">
window.onload = function () {
    var inputs = document.getElementsByTagName('input');
    for (i = 0; i < inputs.length; i++) {
        inputs[i].onkeyup = function () {
            if (this.value.match(/^([0-9][0-9][0-9][0-9][0-9])$/)) {
                document.forms["punch"].submit();
            }
        };
    }
};
$("form").bind("keypress", function (e) {
    if (e.keyCode == 13) {
        $("#btnSearch").attr('value');
        //add more buttons here
        return false;
    }
});
</script>
</head>
<?php
$id = 0;
function getID($staffid, $id, $first_name){
	$servername = "localhost";
	$username = "root";
	$password = "x";
	$dbname = "hr";
	$conn1 = new mysqli($servername, $username, $password, $dbname);
		if ($conn1->connect_error) {
			die("Connection failed: " . $conn1->connect_error);
		} 
	
		$sql = "SELECT id, first_name, employee_id FROM Employees WHERE employee_id='".$staffid."'";
		$result = $conn1->query($sql);
		if ($result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {
			$id = $row["id"];
			$first_name = $row["first_name"];
		}
		return array($id, $first_name);
		} else {
			return false;
		}
		$conn1->close();
}
if (isset($_POST['staffid'])){
	$staffid = $_REQUEST['staffid'];
	
	if(getID($staffid)) {
		$status = "<font color='green'>Punched</font>";
		echo $id;
		echo $first_name;
	} else {
	$status = "<font color='red'>Invalid ID, try again</font>";
	}
}


?>
<body>

<form class="box login" action="" id="punch" method="post">
<fieldset class="boxBody">
	  <label>Staff ID</label>
	<p><input id="staffid" type="text" autofocus="autofocus" name="staffid"></p>
</fieldset>	
<fieldset class="boxBody2">
	  <center><img src="clock.png" /><br><?php if ($status) {echo $status;} else {echo 'Please enter your Staff ID';}?></center>
		</fieldset>	
</form>

</body>
</html>

This line:

 

if(getID($staffid)) {
--- depends on getID returning a boolean equivalent value. You have changed the function so that it no longer works that way. You will need to rethink the way that code is written if you want to change the way getID() works. Returning false or an array is no longer compatible with returning false OR some positive integer that will evaluate to true.

I don't understand why you need the $id variable as you don't seem to use it. The only id variable I see you use is the $staffid

$arg = getID($staffid);

if(is_array($arg)) 
{
  $status = "<font color='green'>Punched</font>";
  echo $arg[0]; //$id
  echo $arg[1]; //$first_name
} 
else 
{
  $status = "<font color='red'>Invalid ID, try again</font>";
}

Change the arguments in this function to this:

function getID($staffid){

I plan on using the ID later in the file, for a different MySQL statement. So what should I be using for this?

This is how you create spaghetti.

 

functions should be discrete and do one thing. Planning to "add a different MySQL statement later" isn't the right solution. You should write a different function for that.

 

It's also clear that getID() is a bad name for this function. It really should be named something like:

 

function getEmployee($id) {
}
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.