Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to POST the form button click time to PHP?

Posted on 2012-03-13
13
Medium Priority
?
434 Views
Last Modified: 2012-03-25
For the following button action,  I cannot able to post the "new Date();" to PHP and into the DB...  Bascially i am trying to send the button click time-stamp to Database. How do I POST the button click time to PHP?


 <?php
$baction = pg_escape_string($_POST['baction']);


$sql = "INSERT INTO table VALUES('$baction');";

 ?>

<FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >

<input type="baction" value="Action" name="baction" onclick="init(); new Date();">

</FORM>
0
Comment
Question by:Mike
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 4

Expert Comment

by:Paul-B
ID: 37718393
Is there a long time between the clicking of the button and the processing to the DB or something? I mean couldn't you just call the date("ymd") at the time of the actual INSERT?
0
 
LVL 11

Assisted Solution

by:maeltar
maeltar earned 150 total points
ID: 37718582
Are you wanting to insert the timestamp of when the button was clicked or when the record is inserted ?

if the latter you could use :

$sql = "INSERT INTO table VALUES(NOW())";

but you need to ensure the datatype of the column is datetime
0
 
LVL 7

Assisted Solution

by:Ironhoofs
Ironhoofs earned 150 total points
ID: 37719181
The button itself is not posted. You can declare another input and change the value of this input when the button is clicked. For example:

<FORM name="navForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
   <input type="hidden" name="baction" value="">
   <input type="submit" value="Action" onclick="document.navForm.baction.value=new Date().getTime();">
</FORM>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:Ironhoofs
ID: 37719207
In my example i used "new Date().getTime()" because it returns milliseconds elapsed sinds 1-1-1970, while "new Date()" only returns full seconds. Use what best suits your needs.

Also remember that anything posted is a string and you have to format/convert it to the
correct type before inserting it into the database.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 37719521
This article contains information about how to get the client date and time through a form.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html
0
 

Author Comment

by:Mike
ID: 37720204
Yes “Paul-B”, there is a time gap between (baction) button click and final form SUBMIT button click.

Yes, “maeltar” I am able to capture the final submit button time with the following php-postgresql connection code

$sql = "INSERT INTO users VALUES (clock_timestamp());

But here, I also wanted to capture the timestamp when user click “baction” button.



<input type="baction" value="Action" name="baction" onclick="init(); new Date();">

When user click “baction” button, it call the JavaScript function init() and it works great, but also I want to capture the button click time at the same time

Within the javascript file where init() reside, I tried to insert the following

Function currenttime()
{
var bclicktime = new Date()
}

And try to POST to PHP sometime like $btime = pg_escape_string($_POST[‘baction’]);

I could be a small little thing but I am not thinking correctly… Basically I want the “baction” button to execute “init();” function (it is working great) at the same time want to capture the button click timestamp POST to PHP and pass it down to Postgresql.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37720419
Go to the article posted above and read the paragraphs captioned:
Storing Date and Time in Your Database
PHP and MySQL Have Different Time Zone Settings (Test on your system to see if this is also applicable to PostGreSQL - I would expect that it is applicable)
Practical Application #8
0
 

Author Comment

by:Mike
ID: 37720975
"Ray_Paseur"
I didn't find anyting in the above link but I checked section "Practical Application #8" in your "Handling Date and Time in PHP and MySQL" article.

$client_offset_minutes = $_POST["date_O"];

<form method="post">
<input name="date_O" id="dateTime_O" type="hidden" />
<input type="submit" value="CHECK CLIENT DATETIME" />
</form>

<script type="text/javascript">
var dateObject = new Date();
document.getElementById("dateTime_O").value =dateObject.getTimezoneOffset();
</script>


The above code seems like for page load not for button click.
I didn't try yet, but if I replace type="hidden" with "button" should this POST button click timestamp to "$client_offset_minutes"?

<input type="button' name="date_O" id="dateTime_O" onlick="init();" />
0
 

Author Comment

by:Mike
ID: 37722474
It is not working for me, any help on capturing the button click time (not submit button click time).
0
 
LVL 15

Assisted Solution

by:StingRaY
StingRaY earned 150 total points
ID: 37723216
Like this?

<FORM name="navForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
   <input type="button" name="baction" value=""  onclick="this.value=new Date().getTime();">
</FORM>

Open in new window

0
 

Accepted Solution

by:
Mike earned 0 total points
ID: 37726904
After serveral tries, I am coming to a conclusion that we cannot use second BUTTON within the same FORM that has SUBMIT button to POST data to PHP...
When I change the type="button" to type="radio" the problem is gone. I still like to use the button as it is more user friendly, I guess i am stuck with "radio/checkbox" type.  I might be wrong, if I am wrong please do correct me.


 <?php
$baction = pg_escape_string($_POST['baction']);
$sql = "INSERT INTO table VALUES('$baction');";

echo .$baction.;
 ?>

<FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<input type="radio" value="Action" name="baction" onclick="init();" onmouseup="this.value=new Date();">

<input .....
<input .....
<input .....
<input .....

<input type="submit" name="submit" value="SET">
</FORM>
0
 
LVL 7

Expert Comment

by:Ironhoofs
ID: 37728304
I have completly lost you there. You can have several submit buttons in a form, but what problem does the radio button solve that the submit button didn't?
0
 

Author Closing Comment

by:Mike
ID: 37762390
For my situation, chaning type="button" to type="radio" worked. Unfortunatly i cannot use multiple submit button, just wanted to use regular button.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question