Is there a way to post javascript variables into PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Is there a way to post javascript variables into PHP?

Post by dethron »

The question is obvious, I think.... :)
junrey
Forum Newbie
Posts: 12
Joined: Thu May 09, 2002 9:53 pm
Location: Cebu, Philippines
Contact:

Is there a way to post javascript variables into PHP?

Post by junrey »

Actually you can. However it must be included in the form tag. You can put a hidden field inside a form and update or change its value through javascript. Once the form is submitted, the value of the hidden field will be carried over.


8)
junrey
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

I have following code....

Code: Select all

<SCRIPT LANGUAGE="JavaScript">

var today_date= new Date();
var myyear=today_date.getYear();
var mymonth=today_date.getMonth()+1;
var mytoday=today_date.getDate();
var myhour=today_date.getHours();
var mymin=today_date.getMinutes();

var timex=myhour+":"+mymin;
var datex=mytoday+"."+mymonth+"."+myyear;

</SCRIPT>
and i want to post this data, so;

Code: Select all

<form name="silex" action="<?=$PHP_SELF;?>" method="post">
<input type=hidden value=timex name="time">
<input type=hidden value=datex name="date">
<input type=submit value="SEND">
The problem arises vith value part.

when i send following query

Code: Select all

$result = mysql_query ("INSERT INTO dati (DATE, TIME)
	                VALUES ('$date', '$time')              ");
However, in table,dati, the inserted values are just date, time. How can i insert the values of this variables (datex and timex).
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

Hi, you can set up a javascript function that sets the values of the hidden fields to the values of the variables timex and datex, then call this function when the form is submitted. This works in IE6, not sure if you access form elements the same way in other browsers :?:

Code: Select all

<html>
<head>
<SCRIPT LANGUAGE="JavaScript"> 

var today_date= new Date(); 
var myyear=today_date.getYear(); 
var mymonth=today_date.getMonth()+1; 
var mytoday=today_date.getDate(); 
var myhour=today_date.getHours(); 
var mymin=today_date.getMinutes(); 

var timex=myhour+":"+mymin; 
var datex=mytoday+"."+mymonth+"."+myyear; 

function setvalues()&#123;
  document.all.silex.time.value = timex;
	document.all.silex.date.value = datex;
&#125;

</SCRIPT> 
</head>
<body>

<form name="silex" action="<?=$PHP_SELF;?>" method="post"> 
<input type=hidden value=timex name="time"> 
<input type=hidden value=datex name="date"> 
<input type=submit name='submit' value="SEND" onSubmit='setvalues()' > 

</body>
</html>
Then in your script, if the form has been submitted, $HTTP_POST_VARS['time'] and $HTTP_POST_VARS['date'] should contain the values you want to insert into the db.

Just setting the values in the html like you are doing

Code: Select all

<input type=hidden value=timex name="time">
doesn't work because it just assigns the string 'timex', because you are using plain old html, not javascript here.

one other possible problem :?:

Code: Select all

var timex=myhour+":"+mymin;
if mymin is less than 10, (for example 9 minutes past the hour) you will end up with timex looking something like:

Code: Select all

10:9
.

hope this helps, Sam :)
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Thanx, it works;

I tried following function before;

Code: Select all

function sendDate()&#123;
window.document.silex.time=timex;
window.document.silex.date=datex;
&#125;
and this did not work, and i gave up using this function. However when i used your suggestion

Code: Select all

function sendDate(){
   document.all.silex.time.value = timex; 
   document.all.silex.date.value = datex; 
}
it works. Thanx :)
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

You have to assign the value of your variables to your form fields when your javascript runs.

i.e.

formname.fieldname.value=timex;

or something like that
Post Reply