Log in

View Full Version : Need PHP Help



derekm
07-17-2007, 10:59 PM
Ok what I am trying to do is on my website I am wanted people to go to the form page fill it out and when they hit submit place the text onto another page on my site. I did a test on it and it worked but only I can see it and no one else can. Then the texted just disappeared. I called a buddy of my and he did it on his computer and it worked but I could not see it. I refreshed my browser several times. The following is what I have so far some of you can probably tell me exactly what is wrong:

/form.html

<form action="agenda.php" method="post">
Type Agenda Information Here:<br>
&nbsp;<p> <input type="text" name="agenda" <br size="46"></p>
<p><br>
&nbsp;<input type="submit" value="Submit"></p>
</form>

/agenda.php (where the text should appear)

<?php echo $_POST['agenda'];
?>

Could Someone plz help.
Thanks DM

djr33
07-17-2007, 11:18 PM
The data of a form is sent to the next page, where it is temporarily available in the $POST[] array. Or, if you use method="get", the $_GET[] array.

Upon loading the page, there is no post data, so nothing would be displayed, just as there is nothing when you load a page after your friend submits something.

If you do want this data to not disappear, you will need to store it. There are several ways to do this, but the two options you should consider are:
1. Store it in a database. More info here:
http://php-mysql-tutorial.com
2. Store it in text files on your server. This code will work--
$file = 'my.txt';
//store the name of the file


$f = fopen($file,'w+'); //open file and if it doesn't exist, create it
fwrite($f,$stuff); //write $stuff into the file. This can be POST vars, or whatever you want.
fclose($f); //close the file, to free up memory.


$file_contents = file_get_contents($file); //get what is in the file
//now you can echo $file_contents; to display it
//or you could use this to add to the content, using:
//$file_contents.$_POST['agenda'];, etc.

derekm
07-18-2007, 03:08 AM
Ok I created a text file named agenda.txt. Do I need to post the php script you gave me in the agenda.php page or what?
I am a little confused with your post. Maybe I am just reading it wrong
Thanks
DM

djr33
07-18-2007, 04:48 AM
Each bit of the PHP code does exactly what I wrote that it does. Simply include it in <?php ?> tags, and that's it.
You just need to decide what goes where.
Is agenda.php the page that receives the post data? If so, then yes, this is where you would put the code, in whatever order you want.
However, you could put the display code for what is in the file already in another page, if you want this form to change content that is displayed on another page.

Another thing to note here is security. Anyone can change the content of the text file now, so you might want to deal with passwords nd such. The easiest way to do this would be to add a field to your form, call it 'password'.
In the receiving PHP code, simply use an if statement to be sure the person is authorized to change the password. Something like this:
<?php
if ($_POST['password'] == 'mypass') {
////Put any code here, like updating the txt file
//
//
//
}
else {
//do the form here, and maybe something like this:
if (isset($_POST['password'])) { echo 'The password was incorrect'; }
//
//
//
}
?>

derekm
07-19-2007, 06:58 PM
Ok I put that code in the php script and created a txt file and it still is not working here is what I have so far

<?php
echo $_POST['agenda'];
$file = 'agenda.txt';
//store the name of the file


$f = fopen($file,'w+'); //open file and if it doesn't exist, create it
fwrite($f,$stuff); //write $stuff into the file. This can be POST vars, or whatever you want.
fclose($f); //close the file, to free up memory.


$file_contents = file_get_contents($file); //get what is in the file
//now you can echo $file_contents; to display it
//or you could use this to add to the content, using:
//$file_contents.$_POST['agenda'];, etc.

?>

djr33
07-19-2007, 07:04 PM
Read the comments (//comments like this aren't part of the code, just notes), and do what they say.

At the moment, all that script is actually doing is writing $stuff to your text file, which is nothing at best, or giving an error if the system has strict errors turned on.

You need to use something as the value of $stuff, for example $stuff = $_POST['agenda'], etc. (I'd rename $stuff if I were you... I just was using it as an example.)

If this is still confusing, you really need to look up a tutorial for basic PHP functionality, and this should start to make sense.

derekm
07-19-2007, 07:40 PM
wait a minute...
am i susposed to be putting any of this php script on the page where we fill the form out?

djr33
07-19-2007, 07:58 PM
It's all up to you, and what you need done, when.

There are 3 different pieces of code there.

You may use them however the want. Each does something, and it's not big mystery what it will do if you move it... it'll do the same thing.

If you want to have something done on the page with the form on it, you can use the PHP code there. If you are sending the form data to the same page from which it is sent, then you will need to have the receiving PHP on that page as well.

And for displaying the contents of the file, you can put that wherever you want.


Here's one possible layout:
<?php
//PHP here to receive form data
?>
<form action=""> (pointing to the same page)
...
</form>


And then on your other page, for the public,
<?php echo file_get_contents('agenda.txt'); ?>
that will dispay what is in the text file on your page.

derekm
07-24-2007, 03:51 AM
Ok, I think I am getting the hang of this.
So let me put it in my terms
Basically what I am doing is

When I fill the form out and hit submit, it thens saves the information in the text file. Then the php code on the page for it to be displayed echo's it to show it on that page all the time.

Correct????

Now if the above is correct what part of the below code needs to go on the form page and what part of the code needs to go on the recieving page and does any of the code need to go in the txt file?


$file = 'my.txt';
//store the name of the file


$f = fopen($file,'w+'); //open file and if it doesn't exist, create it
fwrite($f,$stuff); //write $stuff into the file. This can be POST vars, or whatever you want.
fclose($f); //close the file, to free up memory.


$file_contents = file_get_contents($file); //get what is in the file
//now you can echo $file_contents; to display it
//or you could use this to add to the content, using:
//$file_contents.$_POST['agenda'];, etc.


Thanks DM

derekm
07-24-2007, 04:26 AM
Also,

fwrite($f,$_POST); //write $stuff into the file. This can be POST vars, or whatever you want.

In the above line is the word in red correct format?

Thanks DM

derekm
07-24-2007, 04:41 AM
Ok here it goes I am going to show you what I have on each page and tell you what it is doing.

form.php

<form action="agenda.php" method="post">

<?php $file = 'my.txt';
$f = fopen($file,'w+'); //open file and if it doesn't exist, create it
fwrite($f,$_GET); //write $stuff into the file. This can be POST vars, or whatever you want.
fclose($f); //close the file, to free up memory.
?>
Type Agenda Information Here:<br>
&nbsp;<p> <input type="text" name="agenda" <br size="46"></p>
<p><br>
&nbsp;<input type="submit" value="Submit"</p></form>


agenda.php (which is the page that the info from the form should appear)

<?php echo file_get_contents('my.txt'); ?>

Granted the above codes are not all that is on the page but the rest has nothing to do with this subject.

Ok when I typed City Wide Cleanup in the form it went to the agenda page and all that was added was the word "Array"
So I went back to the form page and did another submission and it went to the agenda page and the word "Array" was still there and nothing else was added. I opened the txt file it is saving in and there is the word "Array" and nothing else.
So there is a submission and it is staying there but the words i am submitting is not showing up and it is not adding anything to the txt file it seems to be overwriting it???

Any Suggestions
THanks
DM