Page 1 of 1
Javascript/PHP problem
Posted: Wed Jul 31, 2002 3:38 pm
by toppac
I am trying to pass a variable thru the POST method, and it does not seem to be working. I have Apache/PHP configured to use the $_POST method, because it works on other pages. Can anyone spot my error in the following code?
The HTML Form
Code: Select all
<FORM METHOD="POST" ACTION="tv/monday_report/weekly_tv_report.php" NAME="data">
<INPUT TYPE="HIDDEN" NAME="startDate" VALUE="">
<INPUT TYPE="HIDDEN" NAME="endDate" VALUE="">
The Javascript code
Code: Select all
function getDates() {
var startdate = prompt ("Start Date (in Oracle Date format, DD-MMM-YY)","");
var enddate = prompt ("End Date (in Oracle Date format, DD-MMM-YY)","");
document.data.startDate.value = startdate;
document.data.endDate.value = enddate;
alert (document.data.endDate.value);
window.document.data.submit();
}
And the PHP code on the recieving page:
Code: Select all
$startDate = $_REQUESTї'startDate'];
$endDate = $_POSTї'endDate'];
I am pretty sure it is getting the values from the prompt, but it does not seem to be passing them across the pages. I get null for both of the values on the recieveing page. Thanks
Posted: Thu Aug 01, 2002 3:26 am
by gnu2php
I tested your code, but it worked just fine for me. I used PHP 4.2.1 and IE 6.0.
By the way, what browser (and version) are you using? And what version of PHP?
The following is the code I used:
Code: Select all
<HTML>
<FORM METHOD="POST" ACTION="myfile.php" NAME="data">
<INPUT TYPE="HIDDEN" NAME="startDate" VALUE="">
<INPUT TYPE="HIDDEN" NAME="endDate" VALUE="">
</form>
<script language="JavaScript">
<!--
function getDates() {
var startdate = prompt ("Start Date (in Oracle Date format, DD-MMM-YY)","");
var enddate = prompt ("End Date (in Oracle Date format, DD-MMM-YY)","");
document.data.startDate.value = startdate;
document.data.endDate.value = enddate;
alert (document.data.endDate.value);
window.document.data.submit();
}
getDates();
//-->
</script>
</body>
</HTML>
And in PHP:
Code: Select all
<?php
var_dump($_REQUESTї'startDate']);
var_dump($_POSTї'endDate']);
?>
Posted: Thu Aug 01, 2002 3:38 am
by twigletmac
On a side note, it would probably be best to change:
to
to maintain a consistant way of accessing the data from the form.
Mac
Posted: Thu Aug 01, 2002 8:46 am
by toppac
Hmm it does not work for me. I have the latest Apache/PHP installed and I am using IE 5.5
I was only using the $_REQUEST to see if that would work, since $_POST was not working. Have any other ideas guys?
Posted: Thu Aug 01, 2002 8:52 am
by volka
I suggest to split the search into two pieces client-side and server side.
client-side: does the form really contain the data?
Code: Select all
...
function getDates() {
var startdate = prompt ("Start Date (in Oracle Date format, DD-MMM-YY)","");
var enddate = prompt ("End Date (in Oracle Date format, DD-MMM-YY)","");
document.data.startDate.value = startdate;
document.data.endDate.value = enddate;
//alert (document.data.endDate.value);
//window.document.data.submit();
}
...
<FORM METHOD="POST" ACTION="tv/monday_report/weekly_tv_report.php" NAME="data">
<INPUT TYPE="text" NAME="startDate" VALUE="">
<INPUT TYPE="text" NAME="endDate" VALUE="">
...
server-side: write a html-form without any script and let php simply print_r($_REQUEST) and/or print_r($_POST)
edit: uhh, I see, someone was that clever, too

and it didn't work out

Posted: Thu Aug 01, 2002 8:53 am
by twigletmac
There was another post recently where someone had the same problem:
http://www.devnetwork.net/forums/viewtopic.php?t=1825
Maybe their solution will help you...
Mac
Posted: Thu Aug 01, 2002 9:20 am
by toppac
I think I figured out the problem. I was doing this to activate the getDates() function
Code: Select all
<A HREF="index.php" OnClick="getDates();">Weekly Report</A><BR>
And it looks like it was just following the link rather than doing the submit() that I told it to in javascript. Does anyone know a way I can use normal text as a link to the javascript function that will then submit the form?
Posted: Thu Aug 01, 2002 1:07 pm
by gnu2php
I think it'll work if you return
false after the getDates() function is called:
- <A HREF="index.php" OnClick="getDates(); return false">Weekly Report</A><BR>
When you return false, the browser cancels the default action.
Posted: Fri Aug 02, 2002 6:19 am
by mikeq
<A HREF="index.php" OnClick="getDates(); return false">Weekly Report</A><BR>
and don't you need a 'javascript:' in there
<A HREF="index.php" OnClick="javascript: getDates(); return false">Weekly Report</A><BR>
Posted: Fri Aug 02, 2002 12:45 pm
by gnu2php
Actually, "javascript:" is only required when you do <a href="javascript: . . . ">
Everything inside onClick=" . . . " will automatically be executed as JavaScript code.