0

My brain is not functioning well right now XD and I want some help.... The thing is, I want to get the date and add to my ajax.post. I have a code here for my time&date format... What is the best way to make it?

js Time&date format

var my_date_format = function(){
    var d = new Date();
    var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var date = d.getDate() + " " + month[d.getMonth()] + ", " + d.getFullYear();
    var time = d.toLocaleTimeString().toLowerCase().replace(/([\d]+:[\d]+):[\d]+(\s\w+)/g, "$1$2");
    return (date + " " + time);  
};

console.log(my_date_format("2015-07-12 11:28:13"));
// output sample 13 Dec, 2015 1:38 pm

html

<form id="member" method="post" action="../php/connect.php">
                <fieldset style="width:400; border-width:6px;" align="center"   >
            <legend><h3>Member Log</h3><br><br></legend>
                <p id="get_member"></p>

             <input class="fname" type='text' name='firstname' id='firstname' placeholder="First Name" required/>

            </br><input class="lname" type='text' name='lastname' id='lastname' placeholder="Last Name" required /></br>

            </br><label for="male">Male  </label><input type="radio" name="gender" id="male" required value="Male" />
                 <label for="female">Female  </label><input type="radio" name="gender" id="female" value="Female" /><br />


            <br><br><input class="submit" type="submit" name="submit">
        </fieldset>
            </form>

jquery ajax post

<script type="text/javascript">

        $(document).ready(function(){
         $('#member').submit(function(){

                  $.ajax({
                           data: $(this).serialize(),
                           type: $(this).attr('method'),
                           url: $(this).attr('action'),
                           success: function(data){
                                    $('#get_member').html(data);
                            }

                  });

                  $("#member")[0].reset();
                event.preventDefault();
         });


});

    </script>
Jayrex
  • 245
  • 1
  • 4
  • 19

2 Answers2

1

You can make a hidden input field in the form and on submit click call date time function add the value return by your date_time function to it. Then call ajax. It will send your date time with your data.

Or you can append the dateTime to serialized form that will also work.

Punit Vajpeyi
  • 196
  • 11
1

You can do this the following way. add html code to your html file.

<input type="hidden" name="dateAndTime" id="dateAndTime" value="">

add js code to your js file before ajax call:

$('#dateAndTime').val(my_date_format);

and finally you can get your data from server end by reading the value named "dataAndTime" in html.

note: don't forget to include jQuery library in your markup page.

Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34