Forum Moderators: coopster

Message Too Old, No Replies

cant access $ POST variables

         

abhishek akj

12:16 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



i wrote my first script yesterday, a pretty basic one, reads data from a from using POST method, and writes these data to a text file on the server.
Everything else is working fine, i mean the text file writing part, but the PHP script does not seem to read the $_POST variables. I even tried $_REQUEST and $_GET, but in vain....When i dont read the variables from $_POST array, i.e. just initialize then to a fixed value, it works as desired. so, as far as i could understand, the problem lies with reading the $_POST array....please suggest something...whether there is problem with HTML code or it is the PHP script...i am pasting the code snippets of both the html form and php script..

html form

<FORM action="sendmail.php" method="POST">
<table border = "0">
<tr>
<td>First name: </td>
<td><INPUT type="text" name = "firstname" size = "60"></td>
</tr>
<tr>
<td>Last name: </td>
<td><INPUT type="text" name = "lastname" size = "60"></td>
</tr>
<tr>
<td>Email: </td>
<td><INPUT type="text" name = "email" size = "60"></td>
</tr>
..
.
.
.
.</html>

php script

<?
print "Content-type: text/html\n\n";
$firstname=$_GET['firstname'];
$lastname=$_GET['lastname'];
$email=$_GET['email'];
$sex=$_GET['sex'];
..
.
.
.
$data="\rFirst Name: "."$firstname"."\t"."Last Name: ".$lastname."\t"."Email: ".$email."\t"."Sex: ".$sex."\t"."Year of Passing: ".$yr_pass."\t"."Course Taken: ".$course."\t"."IT Roll No: ".$it_roll."\t"."Current Employer: ".$employer."\t"."Give a talk: ".$present."\t"."No of Guests: ".$guests."\n";
$fh = fopen("data.txt","a");
fwrite($fh,$data);
fclose($fh);
echo "Your data has been recieved, thank you.";
?>

please help me out..

barns101

12:35 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



You should be accessing $_POST[variable], not $_GET[variable].

eelixduppy

2:21 pm on Sep 19, 2006 (gmt 0)



Welcome to WebmasterWorld, abhishek akj!

For more information regarding the difference between $_GET and $_POST, refer to the Predefined Variable Documentation [us2.php.net].

You will find your experience at this community pleasant and informing!

RonPK

4:38 pm on Sep 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



print "Content-type: text/html\n\n";

By default, all PHP output is sent as HTML. So there is no need to specify the content-type, contrary to Perl.

Also, if you do need to specify the content-type, send a header:

header("Content-type: text/plain");

edit: all this is less relevant if you're trying to mail something...

[edited by: RonPK at 4:40 pm (utc) on Sep. 19, 2006]

abhishek akj

7:48 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



thanks for replying.....
but in my earlier post also, i mentioned that i am unable to access the variables...whether i use $_POST or $_REQUEST or $_GET...i have ensured that i use $_POST when the form method attribute is POST, and $_GET whaen it is GET....
in my earlier post, the error pointed out by barns101 was just a copy-paste error as i had been trying various combinations of GET and POST to set things straight, (so constantly editing both the files, sorry for that typo)....but to no use.

thanks in advance.

abhishek

edit: one more thing, i would like to ask...why do i have to add the line #!D:\PHP\install\php.exe
in the beginning of the script, failing which i get the following error log:
D:/WEB_ROOT/sendmail.php is not executable; ensure interpreted scripts have "#!" first line.....please throw some light..

eelixduppy

10:51 am on Sep 20, 2006 (gmt 0)



What version of PHP are you using?

abhishek akj

11:19 am on Sep 20, 2006 (gmt 0)

10+ Year Member



PHP 5.1.1
alongwith Apache 2.0.55

mgraphic

3:58 pm on Sep 20, 2006 (gmt 0)

10+ Year Member



PHP may not be correctly installed with apache during runtime, which would explain as to why global vars are not being registered.

?

spinnercee

1:45 pm on Sep 21, 2006 (gmt 0)

10+ Year Member



$_REQUEST should always work for GET or POST, ie:

$host = $_REQUEST[host];
$port = $_REQUEST[port];
$user = $_REQUEST[user];
$pass = $_REQUEST[pass];

the field names should NOT be enclosed in any quotes, and following the case of vars from the form vars is not a bad idea either.

re: #!D:\PHP\install\php.exe

Means you're running the script as a CGI app, and not with the server module -- as such PHP.exe must be found and run everytime you call the script -- why this happens is the way you installed PHP in Apache -- there should be two different directories for the two ways of running PHP.

# The module runs scripts here...
Alias /php/ "C:/php/"

# This is actually the CGI directory...
ScriptAlias /php-cgi/ "C:/php/cgi/"

ScriptAlias forces the CGI method for apps in the specified path, whereas when the module is installed, PHP scripts can be run anywhere in the server tree unless restricted by other Apache directives.