Solved

how to POST the form button click time to PHP?

Posted on 2012-03-13
13
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 50 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 50 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 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 110

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 50 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

636 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