Page 1 of 2
Saving, using POST and $variable.
Posted: Sat Nov 09, 2002 4:46 pm
by hatman
Ok, here I go again... back to the basics this time.
Can somebody show me a code here how can I use a variable in a POST a form action.
Is there a diference between how functions work in php 4.1 and php 4.2? I mean, are the 4.1 functions working in 4.2.?
I want to do something like this:
How do i define the variable called in the link a href=page.php?page=somepage
Is this right? it doesn't work however.
Code: Select all
<form action="$page.php" method="post">
<input type="text" name="variable" />
<input type="submit">
</form>
Please help me with this.
PS: just to make things a bit more confusing, I'm using a class that is included that will form a text box and then the value of the text box is supposed to be saved back to the page that was just called.
Does this make any sence. If you need more information just post a reply.
Thanks so much.
Posted: Sat Nov 09, 2002 5:12 pm
by hatman
OK, yeah, I didn't read the sticky about the globals in php 4.2.
How ever I'm still not understanding how would i define the variable?
Posted: Sat Nov 09, 2002 8:03 pm
by volka
Code: Select all
<form action="page.php" method="POST">
<input type="text" name="variable" />
<input type="submit" />
</form>
form's method is POST so php will put all entries into the superglobal array $_POST. Each named element in your from will have it's entry in there. So the value of the input element is in $_POST['variable']
if uncertain try
Code: Select all
<?php
session_start();
echo '<fieldset><legend>POST</legend><pre>'; print_r($_POST); echo '</pre></fieldset>';
echo '<fieldset><legend>GET</legend><pre>'; print_r($_GET); echo '</pre></fieldset>';
echo '<fieldset><legend>COOKIE</legend><pre>'; print_r($_COOKIE); echo '</pre></fieldset>';
echo '<fieldset><legend>FILES</legend><pre>'; print_r($_FILES); echo '</pre></fieldset>';
echo '<fieldset><legend>SESSION</legend><pre>'; print_r($_SESSION); echo '</pre></fieldset>';
echo '<fieldset><legend>ENV</legend><pre>'; print_r($_ENV); echo '</pre></fieldset>';
echo '<fieldset><legend>SERVER</legend><pre>'; print_r($_SERVER); echo '</pre></fieldset>';
echo '<fieldset><legend>REQUEST</legend><pre>'; print_r($_REQUEST); echo '</pre></fieldset>';
?>
it will show you the entries of all

superglobals listed at
http://www.php.net/manual/en/reserved.variables.php
Posted: Sun Nov 10, 2002 12:13 am
by hatman
Volka, please forgive me my stupidity. I honestly didn't understand a thing.
All I want to know what do I need to do in order to use a variable in the section
<form action=$page" method "POST">
Thanks
Posted: Sun Nov 10, 2002 2:29 am
by volka
depends on the variable

if your variable is called $page then one of the following will do
Code: Select all
<?php echo '<form action="',$page,'" method="POST">'; ?>
Code: Select all
<?php echo "<form action="$page" method="POST">";
Code: Select all
<form action="<?php echo $page; ?>" method="POST">
if $page contained e.g.
process.php all those snippets will produce the output
Code: Select all
<form action="process.php" method="POST">
seen by the client.
Posted: Sun Nov 10, 2002 2:31 am
by mydimension
its quite simple. lets take volka's form for example:
Code: Select all
<form action="page.php" method="POST">
<input type="text" name="variable" />
<input type="submit" />
</form>
now in page.php you want to access the value of text box named "variable". in PHP versions prior to 4.1 you could simply use $variable. but since then the default has been to use this instead: $_POST['variable']. had your method been GET than you would access "variable" like this: $_GET['variable'].
yeah the switch messed up some people but in the long run it'll help people write cleanerand more secure code. hope this has been helpful for you.
Posted: Sun Nov 10, 2002 2:33 am
by volka
...the form element wasn't my example...

Posted: Sun Nov 10, 2002 2:42 am
by mydimension
sorry miss read the posts.
Posted: Sun Nov 10, 2002 2:28 pm
by hatman
Thanks guys. This information has been very helpful.
I liked the version that volka showed. I just have a variable name "somepahe" and then I simply add the extention .php
Code: Select all
<form action="<?php echo $page; ?>.php" method="POST">
if I think about it now, it's realy quite obvious. Thanks for pushing me to the right direction.
Now I have my next problem. the function is not saving the data from the form. before it gave me an error http 405 but now it just displays the page i'm updating, but without the update.
What's going on?
The first part starting at
if is the part thats supposed to save the content of the form and the second part after
else is what displays the page I'm about to edit. I'm having trouble getting the saving part to work. The included
class.ewp.php is where the
EWP function is defined. It's simply a text field with rich text editing options like "setFont", "setBold" and so forth.
here's the page:
Code: Select all
<?php
include("class.ewp.php");
$myEWP = new EWP;
if(@$_POSTї"save"] == "true")
{
$fileContent = $myEWP->GetValue(false);
$fp = fopen("$page.php", "w");
fputs($fp, $fileContent);
fclose($fp);
echo "Content saved!<br><br><a href='<?php echo $page; ?>.php'>Continue</a>";
}
else
{
$fp = fopen("$page.php", "rb");
while(!feof($fp))
{
$data .= fgets($fp, 1000);
}
$myEWP->SetValue($data);
?>
<form action="<?php echo $page; ?>.php" method="post">
<input type="hidden" name="save" value="true">
<?php $myEWP->ShowControl(400, 300, "imgs"); ?>
<br><input type="submit" value="Save">
</form>
I hope it makes some sence...
Posted: Sun Nov 10, 2002 2:44 pm
by hatman
Some clarification:
There are two things loaded to the update.php where the previously posted code resides. The textbox (class.ewp.php) and inside it the text from page.php. so the textbox named $fileContent that i'm trying to vet the value of is already within the update.php. the page.php contains simply text - nothing else.
Posted: Sun Nov 10, 2002 2:49 pm
by hatman
eh... i'm getting realy messed up here.
OK, let me correct myself - the name of textbox is $myEWP not $fileContent.
Posted: Sun Nov 10, 2002 3:40 pm
by volka
take a look in your (web-)server's error.log
while developing set
error_reporting = E_ALL in your php.ini
use indents in your code and use them so that you can trust them.
Your code with indents:
Code: Select all
<?php
include("class.ewp.php");
$myEWP = new EWP;
if(@$_POSTї"save"] == "true")
{
$fileContent = $myEWP->GetValue(false);
$fp = fopen("$page.php", "w");
fputs($fp, $fileContent);
fclose($fp);
echo "Content saved!<br><br><a href='<?php echo $page; ?>.php'>Continue</a>";
}
else
{
$fp = fopen("$page.php", "rb");
while(!feof($fp))
{
$data .= fgets($fp, 1000);
}
$myEWP->SetValue($data);
?>
<form action="<?php echo $page; ?>.php" method="post">
<input type="hidden" name="save" value="true">
<?php $myEWP->ShowControl(400, 300, "imgs"); ?>
<br><input type="submit" value="Save">
</form>
Posted: Sun Nov 10, 2002 4:45 pm
by hatman
that was a good point, I forgot to do that, still doesn't work though. I did a test and replaced all the variables with the actual page name. still same result.
The previous http 405 error was because I forgot to chmod the target file. Looked into the error log - nothing.
I'm kinda clueless here...
Posted: Sun Nov 10, 2002 6:45 pm
by volka
sure there is nothing in the logs? your code was at least mising a }.
How about installing php on your local box for testing?
Posted: Mon Nov 11, 2002 8:07 pm
by hatman
Yeah, I actually had another piece of code at the end of it and the
} was at the very end like so:
The page would not even show up correctly otherwise, right?
Yeah, I probably might have to do just that. Or ask somebody to test it for me. Could you maybe test it for me?