Saving, using POST and $variable.

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

hatman
Forum Newbie
Posts: 13
Joined: Sat Nov 09, 2002 4:46 pm

Saving, using POST and $variable.

Post 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

<?php
    echo $variable;
?>

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.
Last edited by hatman on Tue Nov 12, 2002 11:04 am, edited 1 time in total.
hatman
Forum Newbie
Posts: 13
Joined: Sat Nov 09, 2002 4:46 pm

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

Post 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
hatman
Forum Newbie
Posts: 13
Joined: Sat Nov 09, 2002 4:46 pm

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

Post by volka »

depends on the variable ;)
if your variable is called $page then one of the following will do

Code: Select all

&lt;?php echo '&lt;form action="',$page,'" method="POST"&gt;'; ?&gt;

Code: Select all

&lt;?php echo "&lt;form action="$page" method="POST"&gt;";

Code: Select all

&lt;form action="&lt;?php echo $page; ?&gt;" method="POST"&gt;
if $page contained e.g. process.php all those snippets will produce the output

Code: Select all

&lt;form action="process.php" method="POST"&gt;
seen by the client.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

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

Post by volka »

...the form element wasn't my example... :roll:
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

sorry miss read the posts.
hatman
Forum Newbie
Posts: 13
Joined: Sat Nov 09, 2002 4:46 pm

Post 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

&lt;form action="&lt;?php echo $page; ?&gt;.php" method="POST"&gt;
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

&lt;?php

include("class.ewp.php");
$myEWP = new EWP;

if(@$_POST&#1111;"save"] == "true")
{
$fileContent = $myEWP-&gt;GetValue(false);
$fp = fopen("$page.php", "w");
fputs($fp, $fileContent);
fclose($fp);
echo "Content saved!&lt;br&gt;&lt;br&gt;&lt;a href='&lt;?php echo $page; ?&gt;.php'&gt;Continue&lt;/a&gt;";
}
else
{
$fp = fopen("$page.php", "rb");
while(!feof($fp))
{
$data .= fgets($fp, 1000);
}
$myEWP-&gt;SetValue($data);
?&gt;
&lt;form action="&lt;?php echo $page; ?&gt;.php" method="post"&gt;
&lt;input type="hidden" name="save" value="true"&gt;
&lt;?php $myEWP-&gt;ShowControl(400, 300, "imgs"); ?&gt;
&lt;br&gt;&lt;input type="submit" value="Save"&gt;
&lt;/form&gt;
I hope it makes some sence...
hatman
Forum Newbie
Posts: 13
Joined: Sat Nov 09, 2002 4:46 pm

Post 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.
hatman
Forum Newbie
Posts: 13
Joined: Sat Nov 09, 2002 4:46 pm

Post by hatman »

eh... i'm getting realy messed up here.
OK, let me correct myself - the name of textbox is $myEWP not $fileContent.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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

&lt;?php
	include("class.ewp.php");
	$myEWP = new EWP;
	
	if(@$_POST&#1111;"save"] == "true")
	{
		$fileContent = $myEWP-&gt;GetValue(false);
		$fp = fopen("$page.php", "w");
		fputs($fp, $fileContent);
		fclose($fp);
		echo "Content saved!&lt;br&gt;&lt;br&gt;&lt;a href='&lt;?php echo $page; ?&gt;.php'&gt;Continue&lt;/a&gt;";
	}
	else
	{
		$fp = fopen("$page.php", "rb");
		while(!feof($fp))
		{
			$data .= fgets($fp, 1000);
		}
		$myEWP-&gt;SetValue($data);
?&gt;
&lt;form action="&lt;?php echo $page; ?&gt;.php" method="post"&gt;
&lt;input type="hidden" name="save" value="true"&gt;
&lt;?php $myEWP-&gt;ShowControl(400, 300, "imgs"); ?&gt;
&lt;br&gt;&lt;input type="submit" value="Save"&gt;
&lt;/form&gt;
hatman
Forum Newbie
Posts: 13
Joined: Sat Nov 09, 2002 4:46 pm

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

Post 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?
hatman
Forum Newbie
Posts: 13
Joined: Sat Nov 09, 2002 4:46 pm

Post by hatman »

Yeah, I actually had another piece of code at the end of it and the } was at the very end like so:

Code: Select all

&lt;?php
}
?&gt;
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?
Post Reply