Javascript/PHP problem

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
toppac
Forum Commoner
Posts: 29
Joined: Fri Jun 14, 2002 10:44 am

Javascript/PHP problem

Post 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() &#123;
	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();
	
&#125;
And the PHP code on the recieving page:

Code: Select all

$startDate = $_REQUEST&#1111;'startDate'];
$endDate = $_POST&#1111;'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
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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() &#123;
   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();

&#125;

getDates();
//-->
</script>
</body>
</HTML>
And in PHP:

Code: Select all

<?php
var_dump($_REQUEST&#1111;'startDate']);
var_dump($_POST&#1111;'endDate']);
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

On a side note, it would probably be best to change:

Code: Select all

$_REQUEST&#1111;'startDate']
to

Code: Select all

$_POST&#1111;'startDate']
to maintain a consistant way of accessing the data from the form.

Mac
toppac
Forum Commoner
Posts: 29
Joined: Fri Jun 14, 2002 10:44 am

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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() &#123; 
   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(); 
&#125; 
...
<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 :wink: and it didn't work out :cry:
Last edited by volka on Thu Aug 01, 2002 8:54 am, edited 2 times in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
toppac
Forum Commoner
Posts: 29
Joined: Fri Jun 14, 2002 10:44 am

Post 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?
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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>
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

Actually, "javascript:" is only required when you do <a href="javascript: . . . ">

Everything inside onClick=" . . . " will automatically be executed as JavaScript code.
Post Reply