Page 1 of 1

Is there a way to post javascript variables into PHP?

Posted: Thu May 09, 2002 8:09 pm
by dethron
The question is obvious, I think.... :)

Is there a way to post javascript variables into PHP?

Posted: Thu May 09, 2002 9:53 pm
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

Posted: Fri May 10, 2002 5:37 am
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).

Posted: Fri May 10, 2002 7:12 am
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 :)

Posted: Fri May 10, 2002 7:21 am
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 :)

Posted: Fri May 10, 2002 7:29 am
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