?
Solved

variable: javascript -> php

Posted on 2005-04-02
18
Medium Priority
?
823 Views
Last Modified: 2009-07-29
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
Comment
Question by:Dawaffleman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 3
  • 3
  • +1
18 Comments
 
LVL 9

Expert Comment

by:keteracel
ID: 13688854
no
0
 
LVL 9

Expert Comment

by:keteracel
ID: 13688862
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
 
LVL 9

Expert Comment

by:keteracel
ID: 13688865
The only way you can get a js variable to PHP is through a POST or GET request to the server again.
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 9

Expert Comment

by:keteracel
ID: 13688899
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
 
LVL 9

Accepted Solution

by:
keteracel earned 200 total points
ID: 13688963
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
 
LVL 3

Author Comment

by:Dawaffleman
ID: 13690191
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
 
LVL 7

Expert Comment

by:lexlythius
ID: 13691077
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
 
LVL 9

Expert Comment

by:keteracel
ID: 13691100

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
 
LVL 9

Expert Comment

by:keteracel
ID: 13691101
> 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
 
LVL 3

Author Comment

by:Dawaffleman
ID: 13691336
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
 
LVL 16

Expert Comment

by:sam85281
ID: 13692062
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
 
LVL 9

Expert Comment

by:keteracel
ID: 13692832
yeah, the above gives the servers date and time
0
 
LVL 9

Expert Comment

by:keteracel
ID: 13693135
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
 
LVL 9

Expert Comment

by:keteracel
ID: 13693140
>  I got this code from codewalkers.com:

should read:

I modified this code from...
0
 
LVL 16

Expert Comment

by:sam85281
ID: 13693190
http://www.olate.com/articles/254 tells all about how to get the local time with javascript and pass it to php.

-Sam
0
 
LVL 7

Expert Comment

by:lexlythius
ID: 13693204
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
 
LVL 7

Expert Comment

by:lexlythius
ID: 13693230
Oops. Remove this line:
        alert(fullDate);
0
 
LVL 3

Author Comment

by:Dawaffleman
ID: 13693521
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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question