0

Hi I have a standard HTML side.

From the final page before submission of form (page3.php), i have all the details already there in hidden fields, once I click "Submit" and email is sent with the details.

I am trying to add a random number, like an order number.

I am able to do it with:

<script>
now = new Date();
randomNum = '#S';
randomNum += Math.round(Math.random()*9);
randomNum += Math.round(Math.random()*9);
randomNum += now.getTime().toString().slice(-6);
</script>  

and in html on the form:

Order Number = <script>document.write(randomNum)</script>
<input type="hidden" name="orderid" onload="this.value=randomNum">

This works, a random number can be generated on that page itself (page3.php), however when I click submit, I cant seem to get it to appear in the email. Submit leads to an email.php that sends the form data to my email address. In that email.php I have it sent to collect "orderid" and post it into the email body as "$orderid"

Any idea what I am doing wrong?

Dil Marc
  • 103
  • 1
  • 8

1 Answers1

2

Place the JavaScript code which generates the random number after the definition of the <input> element:

<input type="hidden" name="orderId" id="orderId">

<script>
    now = new Date();
    randomNum = '#S';
    randomNum += Math.round(Math.random()*9);
    randomNum += Math.round(Math.random()*9);
    randomNum += now.getTime().toString().slice(-6);
    var order = document.getElementById("orderId");
    order.value = randomNum;
</script>

You may want to place your scripts at the bottom of the page for this and other reasons.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • doesn't work when i put it after it, but it works when i put it before it. it appears on page3.php. a random number is generated, only problem is after i click submit, that same number does not appear in the email sent to me. – Dil Marc Dec 22 '17 at 10:17
  • @DilMarc There is little I can do at this point because I don't have your code in front of me. I tried to give the best answer available to me. – Tim Biegeleisen Dec 22 '17 at 10:18