-3

Possible Duplicate:
how to put javascript variable in php echo

I have JavaScript to get time and the code is below.

<script type="text/javascript">

var d = new Date();

var H = d.getHours();
var m = d.getMinutes();
if(H>11)
{
    var h = H-12;
    var a = 'PM';
}
else
{
    var h = H;
    var a = 'AM';
}

</script>

Now, I want to print the variables in PHP that used in JavaScript like,

<?php echo $h.$m; ?>

Is it possible?

if yes then help me.

5
  • 4
    Is the search broken for you? Commented Oct 31, 2012 at 12:20
  • PHP executes on the server-side. JavaScript executes on the Client-side. Research writing JavaScript values to your HTML. Commented Oct 31, 2012 at 12:20
  • You can't push client side js to PHP on the same page Commented Oct 31, 2012 at 12:20
  • dude, javascript runs in the browser and php runs in the server. Am i missing something? Commented Oct 31, 2012 at 12:21
  • 1
    Wow the same question was asked 20 minutes ago, lazy, lazy, lazy Commented Oct 31, 2012 at 12:21

1 Answer 1

1

Javascipt runs on the client, php runs on the server... If you just want to print the var in the browser

var varName = "varValue";
document.write(varName);

if you need to send the javascript var to php, you need to send it to the server with jquery or javscript and then return the data...

$.post("yourFile.php", { varName: "varValue" },
function(data) {
   //print the data coming from yourFile.php
   document.write(data);

});

if you only need to send the var to the server

 $.post("yourFile.php", { hour: "hour", min: "min", sec:"sec" } );
Sign up to request clarification or add additional context in comments.

3 Comments

I want to insert the time in database. that's why I want it to store in a variable in php.
ok, just use $.post("yourFile.php", { hour: "hour", min: "min", sec:"sec" } ); then on php use $_POST['hour'] to retrieve the value
vote up if you solve the problem ;-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.