Small PHP Problem

They have: 51 posts

Joined: Dec 2002

G'day,

Having a small prob with a tutorial 'm trying. I'm trying to display data from a textbox on another page but keep getting an error (see below). I've done it before in other ways but wanna know why this way doesn't work.

The Error...
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\PROGRAM FILES\ABYSS WEB SERVER\htdocs\php\form\form.php on line 11

FORM.HTM

FORM.PHP
<?
if (isset($_POST['button']))
{
echo "Button Hit";
echo "Text Box Data: $_POST['textbox']";
}
else
{ echo "Button Not Hit"; }
?>

btw, 've only just started learning PHP.

cheers

druagord's picture

He has: 335 posts

Joined: May 2003

try it like this i took the habit of not putting arrays in the string directly but use the concatenation operator

Quote:
<?
if (isset($_POST['button']))
{
echo "Button Hit";
echo "Text Box Data:".$_POST['textbox'];
}
else
{ echo "Button Not Hit"; }
?>

IF , ELSE , WHILE isn't that what life is all about

They have: 461 posts

Joined: Jul 2003

it's this line: echo "Text Box Data: $_POST['textbox']";

the issue is that you can't put an array directly in. there's three ways around this.
1: always assign array values to something else before sticking them in
2: concatinate (as shown above)
3: use {}

i used to always use the first. then i felt it might be better to just concatinate. at that point i pulled a lot out of some pages making the code look a lot better, but then someone put me onto the third. now i've sdecided the best thing is 2 or 3 dependingon the situation. i'll let you decide which is better and what to do on your own, because i'm thuroughly convinced it's primarily a personal preference considering the argument i saw at a diff board about how to fix a problem someone had based on this.

the best thing i can do is give you an example of each of the ways not already exampled for you to tthen decide which you like best.

however, i'm going to take the liberty of combining both your pages into one.

method 1:

<?php
$textdata
=$_POST['textbox'];
$page=$_SERVER['PHP_SELF'];

if (isset(
$_POST['button'])){
    echo \
"Button Hit\";
    echo \"<br>Text Box Data:
$textdata\";
}else{
    echo <<<END
<form method=\"post\" action=\"
$page\">
<input type=\"text\" name=\"textbox\" size=\"20\">
<input type=\"submit\" name=\"button\" value=\"Submit\">
</form>
END;
}
?>

method 2: see the other druagod's post
method 3:
<?php
if (isset($_POST['button'])){
    echo \
"Button Hit\";
    echo \"<br>Text Box Data:
{$_POST['textbox']}\";
}else{
    echo <<<END
<form method=\"post\" action=\"
{$_SERVER['PHP_SELF']}\">
<input type=\"text\" name=\"textbox\" size=\"20\">
<input type=\"submit\" name=\"button\" value=\"Submit\">
</form>
END;
}
?>

fyi: using $_SERVER['PHP_SELF'] returns the page you have, therefore you can now rename the page without ever braking the form.

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

You can embed single deminsional arrays within double quoted strings. Simply ommit the single quotes within the array's key.

echo "Text Box Data: $_POST[textbox]";

And for m3rajk's method 1, you may want to consider using refereces. They'll save memory, and I believe are created faster than an assignment opperation (as you'd be copying a memory address, rather than the contents of the address).

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 51 posts

Joined: Dec 2002

wow, alot of responses

I think 'll use method three

cheers again

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.