Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variables not recognized 2

Status
Not open for further replies.

gosuc2000

Technical User
Jun 2, 2004
59
DE
Hello list,

I have the following problem with variables in my
program (well .. I'm new to PHP)

For exercising purposes I wrote a little guestbook-program,
which consists of a textarea for comments, a name-field and an e-mail addres, that must be filled in. After submitting the data, the latest contents of the textarea should be written in a file and echoed on top of the previously printed comments from the textarea.
But unfortunally, the contents of the form-variables are neither written to disk, nor displayed on screen.
I also found, that some variables could only be examined
when using the $_POST['var_name'] notation instead of just the $var_name.
Here is an extract of the code.


<html>
<head>
<title>Einfaches G&auml;stebuch</title> //German ..
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="linen">
<h1>Einfaches G&auml;stebuch</h1> //German ...
<form action="<?php echo $PHP_SELF; ?>" method="post">Your comment please:<br>
//<form action="guestbook.php" method="post">Your comment please:<br> <<<< THIS DOESN'T WORK...WHY ??
<textarea cols="55" rows="4" wrap="soft" name="comment"></textarea><br>
Ihr Name:<br>
<input Type="text" name="name"><br>
Ihre E-mail Address:<br>
<input type="text" name="email"> <input type="submit" value="Ver&ouml;ffentlichen"></form>
<h3>Previous Comments</h3>
<?php
// Store filename in Variable
$datei="comment.txt"; // the filename the data should be written to ..
// Variable comment set? Name und E-mail not empty?
// >>> $comment $name and $email DOES NOT WORK IN THE FOLLOWIN LINE -- WHY???
if (isset($_POST['comment']) && $_POST['name'] != "" && $_POST['email'] != "") {
// Test-printout follows, and it shows correct results ..
echo "All variables are correctly set\n";
echo $_POST['comment'] . "\n";
echo $_POST['name'] . "\n";
echo $_POST['email'] . "\n";
echo $datei; // the filename
// Open file
$zeiger=fopen($datei,"r+");
// Read data from file and store in in $alt
$alt=fread($zeiger,filesize($datei));
// E-mail-Link creation
$email="<a href=\"$email\">$email<a>";
// Get and format date ...

datum=date("j.n.Y");
// Remove slashes and \n ..
// "Assemble" the comment from the textarea ...
$meinung="<p><b>$name</b> ($email) wrote on <i>$datum</i>:<br>$comment</p>\n";
// Move filepointer to the beginning of the file ...
rewind($zeiger);
// Write new comment before the old one to file ...:
fputs($zeiger,"$meinung \n $alt");
// Close file
fclose($zeiger);
}
// Print entire File-contents (Comments)
readfile($datei);
?>
</body>
</html>

No comments of the textarea is written to the file, and not shown on the display.
Can someone explain me, why the $varname - notation (pointed out in this program) does not work, while the $_PUTS['varname'] does work?

ANy help is apreaciated.

Thanks in advance.

Regards ..
fred

 
>> also found, that some variables could only be examined
when using the $_POST['var_name'] notation instead of just the $var_name

the concept of referring a form variable without declaring it was possible using in older versions. this is called "Register Globals". this is off by default in newer versions. i suggest u use the $_POST[] method...

Known is handfull, Unknown is worldfull
 
Because you are assuming variable_glonbals to be on. PHP disbaled this for security reasons, the only way to access variables from a submitted form is using any of the following superglobals:

$_REQUEST[]
$_POST[]
$_GET[]
$_PUTS[]


So you would access any variable from the form as $_POST['somevariable'];


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hello vbkris and vacunita,

thank you for your rapid response. Most of the problems disapeared after using the _POST['varname'] notation.
However, SOME lines in my script refuse to accept the
$_POST['..'] notation with the following error:

Parse error: parse error, expecting `T_STRING` or
`T_VARIABLE` or `T_NUM_STRING in file xy.z on line xx.

So my solution for those remaining lines now is to assign the variable values like this:

if (isset($_POST['comment']) && $_POST['name'] != "" && $_POST['email'] != "") {
$name=$_POST['name'];
$email=$_POST['email'];
$comment=$_POST['comment']; }

After this, I can use the $varname notation on those lines that do not accept the $_POST['vartname'] notation.

Vacunita, you mentioned the _globals variable. Is this the register_globals variable? This was the only var-name I found. For the purpose of testing, I set this var to on,
like $register_globals=on; , but this didn't change anything, I still have to use the $_POST['varname'] notation and make the assignments like in my example above.

Perhaps there is a better way to solve this, in which case I'd be greatful for any advice.


Thanks a lot.

regards,

Fred
 
Yes I meant register globals..

As for the error: Wher exactrly is it happening? please prvodie the line it points to, and 2 lines above it. So we can see whats going on.

Then we can help.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Register globals is an ini setting not a var. In your ini file you can specify whether you want register globals on or off. In newer versions of php they are off by the default because of security issues. It is best to leave them like that.

As for the error you're getting. PHP does not like associative arrays within doublequotes. For instance:
Code:
echo "My name is $_POST['name']";
will fail, while this
Code:
echo "My name is " . $_POST['name'];
will work.
 
I have to thank you all for your quick help.

vacunita, the errors I mentioned disapeared after cleaning up the script from all kind of test-printouts (for test-purposes).

Thank you also to vragabond for the code-tips.

I now do understand how to pass the variables and what the register_globals setup is used for.

Good to know, there are knowlegeable programmers out there
that are willing to spend their time to help the unexperienced to get up to speed.

regards,

Fred
 
even though this will fail:
Code:
echo "My name is $_POST['name']";

this will work:
Code:
echo "My name is {$_POST['name']}";

Capsulate the variable inside {}.
I *think* that will parse faster.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top