Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jun 2018
    Location
    Jonesboro, Arkansas
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Displaying from Java to HTML

    I am trying to generate a unique number when a button is pressed and I think I am getting close. I can run this without any errors, but my issue is that I'm not getting any feedback in my browser. I'm sure I'm just overlooking something small, but I have been staring at this for several hours and still haven't gotten it working. Any help would be appreciated.


    Here is my HTML:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    	<title>Accident # Gen</title>
    </head>
    <body>
    	<h1>SBC Jonesboro Police Accident Number Generator</h1>
    	<div id="numberDisplay">
    		<!-- Numbers will display here -->
    	</div>
    	<button onclick="ID()">Get Number</button>
    
    	<script src="javascript.js"></script>
    </body>
    </html>

    This is my JavaScript:

    Code:
    function uniqueNumber() {
    	var date = Date.now();
    
    	if (date <= uniqueNumber.previous) {
    		date = ++uniqueNumber.previous;	
    } 	else {
    		uniqueNumber.previous = date;
    }
    
    	return date;
    }
    
    uniqueNumber.previous = 0;
    
    function ID(){
    	return uniqueNumber();
    	document.getElementById('numberDisplay').innerHTML = date;
    }

  2. #2
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    11,198
    Thanks
    6
    Thanked 1,345 Times in 1,314 Posts
    How are you able to run this without errors? If I’m looking at the JS console in Firefox’s developer tools the first warning I’m getting is: https://developer.mozilla.org/en-US/...t_after_return, and it refers to the return statement in the ID() function.

    And if I switch these lines I’m getting the error “ReferenceError: date is not defined” because you define “date” inside the other function, so the variable just has local scope. Moving the variable assignment before (i. e. outside) the uniquerNumber function it generates a number, but I have no idea for what purpose you are returning the results of the uniqueNumber function.
    Last edited by VIPStephan; Jul 2nd, 2018 at 09:37 AM.

  3. #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    19,547
    Thanks
    221
    Thanked 2,712 Times in 2,686 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

    If your unique number is based on some relationship with the previous number, then you will need to store the previous result in a cookie or local storage.

    Try this (a unique number based on date and time):-

    Code:
    <!DOCTYPE html>
    <head>
    </head>
    <body>
    <form id="formname" method="post" action="">
    <label>Order Number
    <input type="text" name="orderNum" id="orderNum" />
    </label>
    </form>
    <script type="text/javascript">
    
    var d = new Date();
    var yr =( d.getYear() ).toString();
    yr = yr.charAt(3); // final digit
    var mo = ( d.getMonth()+1 ). toString();
    if ( mo.length <2 ) {mo = "0" + mo}
    var dy = (d.getDate() ).toString();
    if ( dy.length <2 ) {dy = "0" + dy}
    var hrs = ( d.getHours() ). toString();
    if (hrs.length <2) {hrs = "0" + hrs}
    var mns = ( d.getMinutes() ).toString();
    if (mns.length <2) {mns = "0" + mns}
    var secs = (d.getSeconds() ).toString();
    if (secs.length <2) {secs = "0" + secs}
    var oNum = yr + mo + dy + "-" + hrs + mns + secs;
    document.getElementById("orderNum").value = oNum ;
    </script>
    
    </body>
    </html>
    Quizmaster: Merino wool comes from which animal?
    Contestant: The whale.
    Last edited by Philip M; Jul 2nd, 2018 at 09:55 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  4. The Following 2 Users Say Thank You to Philip M For This Useful Post:

    starboychad (Jul 2nd, 2018), sternco (Jul 2nd, 2018)

  5. #4
    New Coder
    Join Date
    Jun 2018
    Location
    Jonesboro, Arkansas
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you. I have gotten something to display, finally, at least. I was only using the date because I thought that would be the easiest way to get something to display. Is there a way to make this displayed number be sequential? IE: 1001, 1002, 1003, etc?

  6. #5
    New Coder
    Join Date
    Jun 2018
    Location
    Jonesboro, Arkansas
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I am assuming I would need to store the numbers into an array and have it pull from it?

  7. #6
    New Coder
    Join Date
    Jun 2018
    Location
    Jonesboro, Arkansas
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by VIPStephan View Post
    How are you able to run this without errors? If I’m looking at the JS console in Firefox’s developer tools the first warning I’m getting is: https://developer.mozilla.org/en-US/...t_after_return, and it refers to the return statement in the ID() function.

    And if I switch these lines I’m getting the error “ReferenceError: date is not defined” because you define “date” inside the other function, so the variable just has local scope. Moving the variable assignment before (i. e. outside) the uniquerNumber function it generates a number, but I have no idea for what purpose you are returning the results of the uniqueNumber function.

    Honestly, I'm not sure how it ran without errors. I thought it looked ok. I have implemented the code that Philip M suggested, and that has gotten me a unique date/time based number displaying. I was extremely pleased until they informed me that they want it to be a sequential number each and every time. I really want to pull this off, since I am the new guy and the others that work here haven't been able to figure it out either, but I am just about to throw in the towel. They keep changing their minds about what they want and it makes it really difficult to build something.

  8. #7
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    11,198
    Thanks
    6
    Thanked 1,345 Times in 1,314 Posts
    Well, sequential numbers are even easier.

    Code:
    <button id="generate">Get number</button>
    <div id="result"></div>
    Code:
    var num = 0;
    document.getElementById('generate').addEventListener('click', function() {
    	document.getElementById('result').innerHTML = num++;
    });

  9. Users who have thanked VIPStephan for this post:

    starboychad (Jul 2nd, 2018)

  10. #8
    New Coder
    Join Date
    Jun 2018
    Location
    Jonesboro, Arkansas
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    This is what I have so far. It will add one number to it, but doesn't do anything else no matter how many times I click it. I'm also still trying to figure out the best way to "save" each previous number so that they do not get repeated.

    Code:
    <!DOCTYPE html>
    	<!-- Designates the HTML
    	This code is the property of Chad Hall and is being tested to be used by
    	the Jonesboro Police Department. This code should not be given to any other 
    	departments without the express consent of the author. Any issues that arise,
    	due to mishandling of this information, are not the responsibility of the author.-->
    
    <head>
    	<!-- Settings for the Header of the page -->
    	
    	<style>
    		body {background-color: dimgray;}
    
    		h1 {color: lime;}
    
    		label {color: white;}
    
    		form {text-align: center;}
    
    		footer
    		{
    			color: black;
    			text-align:center;
    			position:absolute;
    			bottom:0;
    			width:100%;
    			height:60px;
    		}
    	</style>
    
    	<title>Accident # Gen</title>
    	<!-- Title of the "tab" of the website -->
    </head>
    
    <body>
    
    	<h1 style="text-align:center;">SBC Jonesboro Police Accident Number Generator</h1>
    
    	<form id="formname" method="post" action="">
    
    
    		<label>	ACCIDENT NUMBER
    			<input type="text" name="orderNum" id="orderNum" />
    		</label>
    
    	</form>
    
    <button type="button" style="background-color:yellow;margin:auto;display:block" onclick="getNumber()">Generate Number</button>
    
    
    <footer>
    	<p>Created By: Chad Hall</p>
    	<p>All code contained is written and owned by Chad Hall</p>
    	<p>Other organizations interested in this program should contact Chad at Jonesboro City Hall</p>
    </footer>
    <script type="text/javascript">
    
    
    function getNumber()
    {
    
    var num = 1000;
    
    num++
    document.getElementById("orderNum").value = num;
    
    }
    
    
    
    
    
    
    </script>
    
    
    </body>
    </html>

  11. #9
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    11,198
    Thanks
    6
    Thanked 1,345 Times in 1,314 Posts
    Quote Originally Posted by starboychad View Post
    This is what I have so far. It will add one number to it, but doesn't do anything else no matter how many times I click it.
    That is because you are redeclaring the variable “num” everytime you click the button. You need to move the variable declaration out of the function:
    PHP Code:
    var num 1000;
    function 
    getNumber() {
      
    num++;
      
    document.getElementById("orderNum").value num;

    As for saving, I suppose you mean to continue where you left if the page is reloaded?
    Honestly, I think the best and most reliable way would be a database to store and a server-side script (e. g. PHP) to retrieve the values. But if that’s not an option and/or this is run in a controlled environment then, as Philip M said, a cookie or localStorage would be the way to go. I’d have to look it up myself and am too lazy right now, so I’m just gonna say that you can find many sources on the internet on how to save values in cookies/local storage.

  12. Users who have thanked VIPStephan for this post:

    starboychad (Jul 3rd, 2018)

  13. #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    19,547
    Thanks
    221
    Thanked 2,712 Times in 2,686 Posts
    I don't like to say it, but there is no need to use a computer to generate sequential numbers, 1001, 1002, 1003 etc. I can predict the next number in the sequence with some reliability.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  14. Users who have thanked Philip M for this post:

    starboychad (Jul 3rd, 2018)

  15. #11
    New Coder
    Join Date
    Jun 2018
    Location
    Jonesboro, Arkansas
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by VIPStephan View Post
    That is because you are redeclaring the variable “num” everytime you click the button. You need to move the variable declaration out of the function:
    PHP Code:
    var num 1000;
    function 
    getNumber() {
      
    num++;
      
    document.getElementById("orderNum").value num;

    As for saving, I suppose you mean to continue where you left if the page is reloaded?
    Honestly, I think the best and most reliable way would be a database to store and a server-side script (e. g. PHP) to retrieve the values. But if that’s not an option and/or this is run in a controlled environment then, as Philip M said, a cookie or localStorage would be the way to go. I’d have to look it up myself and am too lazy right now, so I’m just gonna say that you can find many sources on the internet on how to save values in cookies/local storage.
    Thank you very much. I really appreciate all the pointers and help. I will continue searching for the local storage solution. Thanks again for all your help.

  16. #12
    New Coder
    Join Date
    Jun 2018
    Location
    Jonesboro, Arkansas
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    I don't like to say it, but there is no need to use a computer to generate sequential numbers, 1001, 1002, 1003 etc. I can predict the next number in the sequence with some reliability.
    I pitched the idea of just creating a shared excel file that when they would dispatch a wreck, they would open and assign a number from the file. I was told that they didn't need "that much to do". They just want to click a single button on a page and radio in that number without worrying about the number being repeated. People have become lazy, I suppose. It's easier to ask for a program.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •