Skip to content

Instantly share code, notes, and snippets.

@Joshua-Tan
Last active February 26, 2017 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Joshua-Tan/c6e5fa8f7ce1319bfac8a8fcd214f55f to your computer and use it in GitHub Desktop.
Save Joshua-Tan/c6e5fa8f7ce1319bfac8a8fcd214f55f to your computer and use it in GitHub Desktop.
Source code for Q7 of CZ3006 Nov/Dec 2016 Exam Paper

HTML File

<html xmlns = "http://www.w3.org/1999/xhtml"><head>
<script type="text/javascript">
	function bin_to_deci(){
		var deciNum=0;
		var binaryForm = document.getElementById("binaryForm");
		for(i=0; i < binaryForm.elements["binNum"].length; i++)
			if(binaryForm.elements["binNum"][i].checked)
				deciNum += Math.pow(2, 7-i);
		return deciNum;
	}
	function convert_display(){
		var binaryForm = document.getElementById("binaryForm");
		binaryForm.deciNum.value = bin_to_deci();
	}
</script>
</head><body>
<form id="binaryForm" action="number.php" method="POST">
<h2>Input the binary number</h2>
<p>	<input type="checkbox" name="binNum" onchange="convert_display();"/>
	<input type="checkbox" name="binNum" onchange="convert_display();"/>
	<input type="checkbox" name="binNum" onchange="convert_display();"/>
	<input type="checkbox" name="binNum" onchange="convert_display();"/>
	<input type="checkbox" name="binNum" onchange="convert_display();"/>
	<input type="checkbox" name="binNum" onchange="convert_display();"/>
	<input type="checkbox" name="binNum" onchange="convert_display();"/>
	<input type="checkbox" name="binNum" onchange="convert_display();"/></p>
<p> Decimal Number : 
<input type="text" name="deciNum" size="30" value="0" onfocus="this.blur();"/><br/>
<input type="reset"/><input type="submit"/></p>
</form></body></html>

number.php file

<html xmlns="http://www.w3.org/1999/xhtml"><body>
<?php
	$number = $_POST["deciNum"];
	$fp = fopen("record.dat", "a+");
	if(flock($fp, LOCK_EX)){
		while(!feof($fp)){
			$line = fgets($fp);
			if($number == intval($line))
				return;
		}
		fwrite($fp, $number . PHP_EOL);
		flock($fp, LOCK_UN);
	}
	fclose($fp);
?></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment