0

This is my html code to display a value in a html label

<table  >
                <tr><td height="40" colspan="2" align=right>
                    <div class="headbox" >Order No.</div></td>
                    <td align=left> <div class="headbox"  ><label id=no > </label></div> </td>

                </tr>

These are the javascript functions to display the number in the label when form loads

function noDisplay(){
   var value =1


    document.getElementById('no').innerHTML = value;

}

window.onload = function(){
    noDisplay()
}; 

Though i want to increase the value of the number by 1 each time the form loads it still remains as 1! how can i rectify this(i.e enter image description hereincrease the value to 2 when the form is submitted or loaded again?)

Aaron Davis
  • 1,731
  • 10
  • 13
Kasun Sampath
  • 105
  • 3
  • 10

4 Answers4

1

The problem is the variable (value) always gets initialized as 0 when the page is reloaded. You need to save the variable somwhere and initialize it with it's old value when the page gets reloaded. You can use cookies(http://www.w3schools.com/js/js_cookies.asp) for this if you only want to make it work for one user or you can save it serverside in a document or in a database if you want to make it work for mutiple parallel users.

1

Generally it is a bad idea to expose the order number before saving it for many reasons:

  • what happens if two browsers load the new order page at the same time? They show the same number? Or they show two different numbers?
  • If they show the same number what happens when both try to save the data? The second is blocked with an exception or the second is saved with a different number?
  • If they show different numbers what happens when the first browser didn't save the data? You have a hole in your order numbers?
  • What if they save data but first the second browser and then the first browser? You have an order number that does reflect the insertion order.

For those reasons it is better to not expose nothing on the order number and return the number only once the data is really saved on the database. Instead if you are in the update form you can expose easily the data that is already saved on the database.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
1

You can also use localStorage variable to save the value.

    function noDisplay(){

    var count = localStorage.getItem("count")==null?1:localStorage.getItem("count");
    count=Number(count)+1;
    localStorage.setItem("count", count);
    document.getElementById('no').innerHTML = count;
  }

This is to update the label to show how many times form is loaded only. If you need order number, then you need to pass order number from server only.

Rajashekhar
  • 469
  • 3
  • 11
  • Thanx alot it works so now how can i pass the value generated by the funtion in to a php variable? – Kasun Sampath Sep 16 '16 at 13:41
  • Sorry. I am java developer. But this link might helps you. http://stackoverflow.com/questions/8191124/send-javascript-variable-to-php-variable – Rajashekhar Sep 16 '16 at 13:46
0

You can use cookies for that. Read the value from cookie if it exists and increment by 1, else display 1. A simple example can be found at http://www.w3schools.com/js/js_cookies.asp

A working example: https://jsfiddle.net/amf3bygk/1/ Create a counter=0 for default.

document.cookie = "counter=0";
pratik mankar
  • 126
  • 1
  • 10