Jump to content

onClick timestamp


i8grand

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.