Passing javascript variable to php - working with a calendar script

Hello,

I implemented a javascript calendar into my script but i cant seem to extract the end user selection to save the selection into the database.

basically need to find a way to pass the calendar selection to a varialbe that php can understand.


<?php
include('connectionv2.php');
if(isset($_POST['btn_test']))
{ echo $_POST['hidden1'];
   echo '<br />';
   $db = Connection::getConnection();
	try
	{	$STH = $db->prepare("INSERT INTO tbl_servicerequest (projectexpire ) VALUES (:stamp )");
		$STH->bindParam(':stamp', $_POST['hidden1']);
		$STH->execute();
	}
	catch(PDOException $e)   {    echo 'ERROR: ' . $e->getMessage(); }
}
?>

<html>
	<head>
		<meta charset="utf-8" />
		<title>jQuery UI Datepicker </title>
		<script type="text/javascript" src="jquery.js"></script>
		<script type="text/javascript" src="jquery-ui-1.10.3/ui/jquery-ui.js"></script>
		<link rel="stylesheet" type="text/css" href="jquery-ui-1.10.3/themes/base/jquery-ui.css" />

		<script type="text/javascript">
		   $(document).ready(function(){				
				 $("#date").datepicker({
				    showButtonPanel: true,
				    minDate: '0M',
					maxDate: '+90D',	
				    dateFormat: "d-MM-yy",	
				   });
				   var dateString = $('#date').val();
					var timestamp = Date.parse(dateString).getTime()/1000;					
				    document.getElementById('#hidden1').value = timestamp;
		   });
		</script>
	</head>
	<body>
	<div>
		<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
			<p> Enter expire date: <br />
			<input type="text" name="date" id="date"/></p>	
			<input type="hidden" name="hidden1" id="hidden1"  />			
			<button type="submit" name="btn_test" value="">Test Button</button>
		</form>
	</div>
	</body>
</html>


Add a hidden field to your form and have the JavaScript update the value of that field.

Hi felgall,

there is a hidden field in the script. i thinks there might be something wrong with the way im passing the variable to the hidden field. any ideas on what im doing wrong?

When you are looking at JavaScript interacting with HTML it is easier if you actually look at the HTML that is in the page and not the PHP that generates that HTML.

Often the HTML that is actually being generated is not what you expect and that is why the JavaScript doesn’t interact properly. Even where the HTML is what you expect it makes it easier to see what is happening without the PHP.

As mentioned, the datepicker is just updating the form field value.
Where you grab the value to use in php is when you post the form.
Printing your POST will show what is being passed. And this to the top of your script to test.


echo "<pre>";
print_r($_POST); 
echo "</pre>";

You should see something like this when you post your form.

Array
(
[date] => 2013-06-05
[hidden1] =>
[btn_test] =>
)

IF you don’t see the form field value being updated with your calendar then your datepicker script is not setup right. That is a different issue then getting the results from the form.

at glance it looks right to me.

are you sure timestamp value is not null?