Is there a way to post javascript variables into PHP?
Moderator: General Moderators
Is there a way to post javascript variables into PHP?
The question is obvious, I think.... 
Is there a way to post javascript variables into PHP?
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.
junrey
junrey
I have following code....
and i want to post this data, so;
The problem arises vith value part.
when i send following query
However, in table,dati, the inserted values are just date, time. How can i insert the values of this variables (datex and timex).
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>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">when i send following query
Code: Select all
$result = mysql_query ("INSERT INTO dati (DATE, TIME)
VALUES ('$date', '$time') ");-
samscripts
- Forum Commoner
- Posts: 57
- Joined: Tue Apr 23, 2002 4:34 pm
- Location: London, UK
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
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
doesn't work because it just assigns the string 'timex', because you are using plain old html, not javascript here.
one other possible problem
if mymin is less than 10, (for example 9 minutes past the hour) you will end up with timex looking something like:
.
hope this helps, Sam
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(){
document.all.silex.time.value = timex;
document.all.silex.date.value = datex;
}
</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>Just setting the values in the html like you are doing
Code: Select all
<input type=hidden value=timex name="time">one other possible problem
Code: Select all
var timex=myhour+":"+mymin;Code: Select all
10:9hope this helps, Sam
Thanx, it works;
I tried following function before;
and this did not work, and i gave up using this function. However when i used your suggestion
it works. Thanx 
I tried following function before;
Code: Select all
function sendDate(){
window.document.silex.time=timex;
window.document.silex.date=datex;
}Code: Select all
function sendDate(){
document.all.silex.time.value = timex;
document.all.silex.date.value = datex;
}