Dil Marc
Dil Marc

Reputation: 103

Random Order Number Post to Email

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?

Upvotes: 0

Views: 95

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521997

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.

Upvotes: 2

Related Questions