• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 826
  • Last Modified:

variable: javascript -> php

hello,

i have a javascript script that returns a date/string to a javascript variable. is there any way i can convert that javascript variable into a php variable within the same page of code.

thanks
0
Dawaffleman
Asked:
Dawaffleman
  • 10
  • 3
  • 3
  • +1
1 Solution
 
keteracelCommented:
no
0
 
keteracelCommented:
basically, javascript is client-side and php is server side. When the page has loaded and javascript is running in the page, PHP has nothing to do with it anymore.
0
 
keteracelCommented:
The only way you can get a js variable to PHP is through a POST or GET request to the server again.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
keteracelCommented:
example GET request:

<a href="http://www.domain.com/page.php?variable=value">request</a>

example POST request:

<form id="frm1" action="http://www.domain.com/page.php?variable=value" method="post">
  <input type="hidden" name="variable" value="value"/>
</form>

<a href="#" onclick="document.getElementById('frm1').submit()">request</a>
0
 
keteracelCommented:
ioops, a copy and paste error:

example POST request:

<form id="frm1" action="http://www.domain.com/page.php" method="post">
  <input type="hidden" name="variable" value="value"/>
</form>

<a href="#" onclick="document.getElementById('frm1').submit()">request</a>
0
 
DawafflemanAuthor Commented:
hmmm

the get/post method might work, but then i would have to load the javascript function on another page...

ill just wait a little longer and see if anyone else comes up with an idea
0
 
lexlythiusCommented:
What keteracel says is true. Consider this:

In the host computer, Apache webserver takes your entire file and starts throwing everything to client's HTTP browser as HTML, until it reaches a <?php ...  ?> tag. Then it sends the content of that tag (as big as it may be) to PHP.exe, which computes PHP code, returning to the webserver a HTML trail which in turn is thrown to client's browser.

All that means that PHP code has already been executed when the user in client's computer does something with the page.

That's why the only way to pass information back to PHP is through GET, POST or COOKIE and that requires reloading (sending and receiving) a page.

You might consider using hidden IFRAMEs if you wish to avoid reloading your main page:

<iframe id="myFrame" src="myScript" style="display:none"></iframe>

Then you would dynamically change your main page contents according to information received inside your iframe. This is a very difficult approach, though.
0
 
keteracelCommented:

Is Javascript really neccessary? According to Norton, 38% of people now turn at least some of the features of javascript off. I would suggest using only PHP if it's possible!
0
 
keteracelCommented:
> This is a very difficult approach, though.

And if we don't know why you need to do this we can't tell how difficult or even if it's possible this way!
0
 
DawafflemanAuthor Commented:
well i was making a forum kind of thing and i was just getting the current date with javascript and then using php to store it into a mySQL database along with some other info.
maybe there is someway to return the date of the users computer with php?
i think i tried this before but what i got was the sever's date and time which wasnt what i wanted.
0
 
sam85281Commented:
PHP Date method:

$date = date( "m/d/Y"); // returns 04/03/2005

$date = date( "F j, Y"); // returns April 3, 2005

$date = date("l F j, Y"); // returns Sunday April 3, 2005

Then you can use your PHP variable $date instead of the javascript var.

-Sam


0
 
keteracelCommented:
yeah, the above gives the servers date and time
0
 
keteracelCommented:
I got this code from codewalkers.com:

<head>
<script type="text/javascript">
var now = new Date();

document.write('<img src="collect.php?now='+now+'" width="1" height="1" style="display: none;"/>');
</script>
</head>

essentially this adds an invisible image which is then called by the browser with the current time of the client. You can also send extra info in the url as required. This should allow you to do what you need...
0
 
keteracelCommented:
>  I got this code from codewalkers.com:

should read:

I modified this code from...
0
 
sam85281Commented:
http://www.olate.com/articles/254 tells all about how to get the local time with javascript and pass it to php.

-Sam
0
 
lexlythiusCommented:
sam85281's code will give you the correct date from the server's host computer.

If you want to get the actual client's computer's date (even in the case its clock is wrong!) you must use javascript since it executes on the client's system (whereas php runs on the host):

<html>
<body>
<form id="myForm" method="get" onSubmit="fillCurrentDateAndTime()">
      <input id="dateTime" name="dateTime" type="hidden" value="">
      <button id="sendMe" type="submit">Send</button>
</form>
</body>

<script language="javascript">
      // be sure to put the following line AFTER your form is created
      var theForm = document.forms['myForm'];

      function fillCurrentDateAndTime()
      {
          var dt = new Date();
          var year = padNumbers(dt.getFullYear(), 4);
          //funny enough, Date object's months run from 0 to 11
          var month = padNumbers((dt.getMonth()+1), 2);
          var day = padNumbers(dt.getDate(), 2);
          var hours = padNumbers(dt.getHours(), 2);
          var minutes = padNumbers(dt.getMinutes(), 2);
          var seconds = padNumbers(dt.getSeconds(), 2);
          var fullDate = ""+year+month+day+hours+minutes+seconds;
          alert(fullDate);
          theForm.dateTime.value = fullDate;
      }

      function padNumbers( number, digits )
      {
          var filler = "0";
            number = number.toString();
            for (var i=number.length; i<digits; i++) {
              number = ""+filler+number;
            }
            return number;
      }
</script>
</html>

This will give your php script a mysql datetime representation ready to be stored into database.
You can even use Sam's snippet to get date and time default value if you wish (date('YmdHis') will do) so in case that javascript is not supported in the client's browser you will get your server's date and time at least.
0
 
lexlythiusCommented:
Oops. Remove this line:
        alert(fullDate);
0
 
DawafflemanAuthor Commented:
thank you for all of your help. i have gotten the script to work now and i will award the points to keteracel because the post idea seems to have worked the best as far as what i needed.

thanks
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 10
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now