I have a javascript code showing current time, which i want to store in php variable. The store value in php will then be used in sending through form to an email id. I just want to store the value in php.
document.getElementById('time')
Any Help/ideas appreciated.

<script type="text/javascript">
    <!--
    function updateTime() {
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();
        if (minutes < 10){
            minutes = "0" + minutes;
        }
        if (seconds < 10){
            seconds = "0" + seconds;
        }
        var v = hours + ":" + minutes + ":" + seconds + " ";
        if(hours > 11){
            v+="PM";
        } else {
            v+="AM"
        }
        setTimeout("updateTime()",1000);
        document.getElementById('time').innerHTML=v;
    }
    updateTime();
    //-->
</script>

Recommended Answers

All 6 Replies

You can store the value in a hidden input field in the form which will then be picked up when form is submitted.

<input type="hidden" id="hidden-date" />

And in the javascript I guess you change line 21 to:

document.getElementById('hidden-date').value = document.getElementById('time').innerHTML;

You can't directly asign php varible values from javascript.
Php is server side script.
If you want this value for your form , use hidden input type and asign that value to this hiden input type.

Just as @broj1 has stated, U can try that out. If it does not work, then let us know ...

If you are just trying to get the current date and time for your use in PHP use the date() function. Date - php.net.

If you are going to store in a database I suggest using the UNIX Timestamp, which you can call in your SQL call.

hi
how to set javascript value in php variable...?
please help me ...!

The answer is provided by broj1 in the second post of this thread. You can save the value in a input type hidden element within the form. Once the form is posted you can pick up the value in your php script like you would for any other input element.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.