We help IT Professionals succeed at work.

passing javascript variable from form to php/mySQL database

amchugh89 asked
i am totally banging my head against the wall on this one.

everything works except it doesnt pass the date from javascript to php (or at least to mySQL).
 the mySQL game_date just says '0000-00-00, 1969-12-31 or 2069-12-31'   even though js the calendar shows the correct date on the screen.

 <link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />

	<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.css" />

<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>

<script type="text/javascript">
	window.onload = function(){
		g_globalObject = new JsDatePick({


			var obj = g_globalObject.getSelectedDay();
			document.getElementById("div3_example_result").innerHTML = obj.month + "/" + obj.day + "/" + obj.year;


<style type="text/css" media="all">
	background: #D96F2D;
	height: 100%;
	font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
	font-size: 12px;
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">

Game Name: <br> <br>   <input type="text" name="Game_Name" size="25">  <br> <br>
Location: <br>  <br> <input type="text" name="Game_Location" size="35">
Details:<BR> <br>
<TEXTAREA NAME="game_description" COLS=40 ROWS=6></TEXTAREA>


       Time:  <br>   <br>  <input type="varchar" name="game_time" size="8">  <BR>  <br>
                 [b] <div id="div3_example"  style="margin:10px 0 0px 0; border:dashed 1px black; width:205px; height:205px;">
    <div id="div3_example_result" name ="div3_example_result" style="height:20px; line-height:20px; width:80px; margin:10px 0 20px 0; border:dashed 1px black;"></div>[/b]

 Category: <br>    <br>
<select name="cats">
       <option value="1">football</option>
       <option value="2">basketball</option>
<option value="3">soccer</option>
       <option value="4">baseball</option>
       <option value="5">hockey</option>
</select>  <br><br>


$con = mysql_connect("localhost","amchugh89","keikolucky1");
if (!$con)
  die('Could not connect: ' . mysql_error());
mysql_select_db("gamesDC", $con);

         $cats = isset($_POST['cats']) ? $_POST['cats'] : 'football';

//here i am trying to convert the js variable to a php one - the reason for the whole $newer_ $older_date thing is because i have to reformat the js date from m-d-y to the mySQL standard of y-m-d 

 [b] $older_date = $_POST['div3_example_result'];

                      $newer_date = date('y/m/d',  $older_date);
    echo  "$newer_Date";[/b]

  mysql_query("INSERT INTO `gamesdc`.`games` (game_name ,
game_description ,
VALUES ('$_POST[game_Name]','$_POST[game_description]','$_POST[game_Location]','$_POST[game_time]',[b]'$newer_date',[/b]'".$cats."')");


Open in new window

All the bold stuff is what i am assuming doesnt work (it is the code related to what is f***ed  up
Watch Question


well it didnt come out bold , but the code related to my problem is on lines:

52-54, 81-84, 90 and 93
ioboxWeb Architect

$newer_date = date('y/m/d',  $older_date);
$newer_date = date('Y/m/d',  $older_date);

this should do (untested..)

is it neccessary to use same datepicker


this one is better

please check line no 21 and 83

try this code and let us know the o/p

echo  [b] $older_date = $_POST['div3_example_result'];

    echo                   $newer_date = date('y/m/d',  $older_date);
    echo  "$newer_Date";[/b]
In your input statement on line 51 varchar is not an acceptable input tag type. Should be text
The date to store in a mysql database should be in "YYYY-MM-DD" format and saved as
$newer_date=date("Y-m-d", strtotime($_POST['div3_example_result']));
The reason that you are getting the 1969 date is that is the start of the Unix epoch, or second number 1, was January 1, 1970, so a 0 creates a December 31, 1969 date. Unix time is the number of seconds since that date. strtotime turns most common date formats into Unix time that the date() function can read.
It's not JavaScript fault. When you are making a call from JavaScript to your PHP page I assume that you are passing the date value as a string. If so, when you are executing the MySQL script be sure that you are converting the string date to a correct date format which is accepted by your MySQL instance (you have to check what date format you are using on your MySQL server).