i8grand Posted April 19, 2010 Share Posted April 19, 2010 I want a button that onClick will save a timestamp for use later on in a calculation with another timestamp in mySQL database. Can timestamping onClick of a button be done easily? Link to comment https://forums.phpfreaks.com/topic/199045-onclick-timestamp/ Share on other sites More sharing options...
seventheyejosh Posted April 19, 2010 Share Posted April 19, 2010 This is more of an ajax question, but yes it can be easily done. Something like: // html <input type="button" onclick="saveTime();">Click Me</button> <script type="text/javascript"> // jquery $.ajax({ type:'post', url:'/save.php', data:'somethingmaybe', success: function(msg){ alert(msg); } }); </script> // php file $now = time(); // insert into db echo 'ok done, added: '.$now; Link to comment https://forums.phpfreaks.com/topic/199045-onclick-timestamp/#findComment-1044812 Share on other sites More sharing options...
ScotDiddle Posted April 19, 2010 Share Posted April 19, 2010 i8grand, You need to pass a javascript value to php... This can be done by fooling JS into executing a PHP script via new Image(); Driving code: ( getTimestamp.php ) <?php Header("Cache-control: private, no-cache"); Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); Header("Pragma: no-cache"); session_start(); if (isset($_GET['myButton'])) { $weHaveTime = $_GET['myButton']; if ($weHaveTime) { echo "User hit the button \"myButton\" at " . $_SESSION['TimeStampStart']; } else { echo "Timestamp set failed... check JS_Parm_to_PHP Script..."; } exit; } else { ?> <script type="text/javascript"> function JS_Parm_to_PHP() { // alert('Hi Mom'); passParm = new Image(); passedVariable = 'timeStamp' // alert('passedVariable : ' + passedVariable); iURL="JS_Parm_to_PHP.php?variable=" + passedVariable; passParm.src=iURL; } </script> <form id="myForm" name="myForm" action="getTimestamp.php"> <input type="submit" name="myButton" id="myButton" onClick="JS_Parm_to_PHP();" value="Click Me to write Unix time-stamp to php session var"> </form> <?php } ?> Code to process "New" JS Image: ( JS_Parm_to_PHP.php ) <?php $getVariable = $_GET['variable']; if ($getVariable == 'timeStamp') { // If not already started... session_start(); $_SESSION['TimeStampStart'] = time(); header('location: getTimestamp.php?buttonValueSet=TRUE'); exit; } ?> Hope this helps. Scot L. Diddle, Richmond VA Link to comment https://forums.phpfreaks.com/topic/199045-onclick-timestamp/#findComment-1044817 Share on other sites More sharing options...
seventheyejosh Posted April 19, 2010 Share Posted April 19, 2010 @ScotDiddle What is the matter with a good old ajax call? Why hack or force something when there is already a way to do it? Link to comment https://forums.phpfreaks.com/topic/199045-onclick-timestamp/#findComment-1044820 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.