Dyon
Dyon

Reputation: 195

Store time in a Form to send to Jquery

I used to have my contact-form in php.

Now im converting everything to use ajax. Here is the problem:

I used to have a function that checks the time the form was opened and the time the form was send. If the time-difference was too low the form would not be send.

i used <?php echo time(); ?> as the value of a hidden field.

Now with Jquery I am not able to use this value. It just reads the code.

I tried <script>$.now()</script> but it doesnt execute the code inside the value of the form.

So my question is: How do i store the time a form was opened in a way that i can get it later via jquery?

Thanks for your help!

Addition: FormerCode:

<div class="container col-xs-12 col-md-6">
            <div class="form-group">
                    <label for="comment">Ihre Nachricht an uns</label>
                    <textarea class="form-control" rows="7" id="message" name="message"></textarea>
                    <label for="adress"></label>
                <input type="text" class="adressform form-control" id="adress" name="adress" style="display: none;">
                    <input type="hidden" value="<?php echo time(); ?>" name="time" id="time"/>
                </div>    
            </div>

And i fetched it via:

$time = $_POST["time"];

Now im trying to get it via:

$(document).on('click','.formLink', function(){


    //Fetch the LinkValue 
    var linkValue = $(this).attr('value');
    var name     = $('#name').val();
    var email    = $('#email').val();
    var phone     = $('#phone').val();
    var message    = $('#message').val();
    var sendTime    = $.now();
    var endTime = $('#time').val();

// For testing only
    alert(sendTime + endTime);


});

Upvotes: 0

Views: 117

Answers (2)

Dyon
Dyon

Reputation: 195

Sorry for bothering. Right after posting an answer in Jquery popped up which was similar. So i was able to use the answer.

If any other Beginner asks himself the same question: I used:

<script>
    document.getElementById('time').value = $.now();
</script>

To set the value to the actual time!

Upvotes: 0

6bytes
6bytes

Reputation: 6116

Remove PHP code from HTML:

<input type="hidden" value="" name="time" id="time"/>

and then in JavaScript write:

var now = $.now();
$('#time').val(now);

Upvotes: 1

Related Questions