Reply
Still a PHP Newbie- post content?
Old 04-26-2007, 06:36 PM Still a PHP Newbie- post content?
Skilled Talker

Posts: 50
Hello-

I'm still learning PHP slowly and am bad at explaining things... I've been playing with some scripts where you input some variable and another page inserts them where I want them to go, like this:

HTML Code:
<form action="process.php" method="post"> 
Age: <input name="age" type="text" /> 
<input type="submit" />
</form>
process.php:
HTML Code:
<?php
$age = $_POST['age'];

echo "You are ". $age . " years old.";

?>
What I'm wondering is how I can make a very simple form like that, except instead of just having the variable you entered show up on your screen, it will be permanently changed to what you entered whenever anybody visits that page. Is there a way to do this without adding something like &variable=### to the end of a URL and instead of repeatedly posting, each time you change the variable it replaces itself?

Thanks so much!
~anwipr
anwipr is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 04-26-2007, 07:01 PM Re: Still a PHP Newbie- post content?
Republikin's Avatar
Super Moderator

Posts: 3,191
Your going to have to use some sort of storage medium and there are a couple of options for doing this.

First is creating a file to store the information (in any format you wish, such as serializing the $_GET array for use later).

Second is using some form of database such as MySQL or SQLite.
Republikin is offline
Reply With Quote
View Public Profile
 
Old 04-26-2007, 08:48 PM Re: Still a PHP Newbie- post content?
Raulică's Avatar
Ultra Talker

Posts: 251
Location: Constanta,Romania
You have some syntax errors in your script. You miss placed some dots. Your script should look like this:

PHP Code:
<?php
$age 
$_POST['age'];

echo 
"You are " $age  " years old.";

?>
__________________
Future-Host
Professional hosting for all pockets!
Check Our Uptime !
Raulică is offline
Reply With Quote
View Public Profile Visit Raulică's homepage!
 
Old 04-26-2007, 08:52 PM Re: Still a PHP Newbie- post content?
Republikin's Avatar
Super Moderator

Posts: 3,191
That actually wouldn't produce a syntax error. It looks nicer but his code will work as is.
Republikin is offline
Reply With Quote
View Public Profile
 
Old 04-26-2007, 09:01 PM Re: Still a PHP Newbie- post content?
Mattmaul1992's Avatar
Ultra Talker

Posts: 440
Name: Matt
I swear it did for me once.. Well like Rebuplikin said you need some sort of storage medium. You could store the information in a database but how would you know it was that person? Their IP address? Well that changes.. Not many people have static IP addresses. Have them login? That's a bit of a pain for something so small. Set a cookie? There we go =-). Search for cookie at php.net.
__________________
PHP Code:
$talkupation++; 
http://www.forum-front.com/ - Free IPB forum hosting (releasing today!!!), no ads, free modifications
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 04-26-2007, 10:47 PM Re: Still a PHP Newbie- post content?
Republikin's Avatar
Super Moderator

Posts: 3,191
A cookie wouldn't work in this instance because as I read it he wants the value to be persistent for everyone that comes to the page, not just the person who sets it.
Republikin is offline
Reply With Quote
View Public Profile
 
Old 04-27-2007, 05:21 AM Re: Still a PHP Newbie- post content?
Raulică's Avatar
Ultra Talker

Posts: 251
Location: Constanta,Romania
Quote:
Originally Posted by Republikin View Post
That actually wouldn't produce a syntax error. It looks nicer but his code will work as is.
You are right, sorry. I don't know what I was thinking at.
__________________
Future-Host
Professional hosting for all pockets!
Check Our Uptime !
Raulică is offline
Reply With Quote
View Public Profile Visit Raulică's homepage!
 
Old 04-27-2007, 09:12 AM Re: Still a PHP Newbie- post content?
Republikin's Avatar
Super Moderator

Posts: 3,191
Don't have to be sorry, we all do it every once in a while.
Republikin is offline
Reply With Quote
View Public Profile
 
Old 04-27-2007, 10:39 AM Re: Still a PHP Newbie- post content?
Mattmaul1992's Avatar
Ultra Talker

Posts: 440
Name: Matt
I'm sorry. I must have missed having it be the same for everyone. You can setup a database or keep it simple and just write it to a small text file and then display that text file.
__________________
PHP Code:
$talkupation++; 
http://www.forum-front.com/ - Free IPB forum hosting (releasing today!!!), no ads, free modifications
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 04-27-2007, 04:51 PM Re: Still a PHP Newbie- post content?
dansgalaxy's Avatar
Eat, Sleep, Code

Latest Blog Post:
Offending facebook ads… so?
Posts: 6,092
Name: Dan
Location: Swindon
I was just thinking that, i was like reading this think jeeze he is obviously learning the basics, just write to a txt file or infact any file type like .htm / .html / .php ... i prefer to have most of my files as php so it makes it more flexible if i want to change somethign later...

Anwyays..

PHP Code:
<?php
$configfile 
"
<?php
include ('info.php');
\$title = \"{$_POST['sitename']}\";
\$adminname = \"{$_POST['adminname']}\";

\$adminemail = \"{$_POST['adminemail']}\";
\$webmasteremail = \"{$_POST['webmasteremail']}\";
\$core = \"{$_POST['coreurl']}\";
?>
"
;
$config "config.php";
$fh fopen($config'w') or die("Can't open file");
$stringData "$configfile\n";
fwrite($fh$stringData);
fclose($fh);
?>
this is take out of a form system so they enter the info in a form on say form.php it sends to savedata.php which is this, and it saves it (in this case) as config.php,

If you need more explination please message back or PM me and please feel free to give me soem nice green REP!

Hope it helps...
Dan
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 04-27-2007, 08:19 PM Re: Still a PHP Newbie- post content?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Wireless Audio
Posts: 2,314
Name: Keith Marshall
Location: West Hartford, CT
Quote:
Originally Posted by dansgalaxy View Post
I was just thinking that, i was like reading this think jeeze he is obviously learning the basics, just write to a txt file or infact any file type like .htm / .html / .php ... i prefer to have most of my files as php so it makes it more flexible if i want to change somethign later...

Anwyays..

PHP Code:
<?php
$configfile 
"
<?php
include ('info.php');
\$title = \"{$_POST['sitename']}\";
\$adminname = \"{$_POST['adminname']}\";
 
\$adminemail = \"{$_POST['adminemail']}\";
\$webmasteremail = \"{$_POST['webmasteremail']}\";
\$core = \"{$_POST['coreurl']}\";
?>
"
;
$config "config.php";
$fh fopen($config'w') or die("Can't open file");
$stringData "$configfile\n";
fwrite($fh$stringData);
fclose($fh);
?>
this is take out of a form system so they enter the info in a form on say form.php it sends to savedata.php which is this, and it saves it (in this case) as config.php,

If you need more explination please message back or PM me and please feel free to give me soem nice green REP!

Hope it helps...
Dan
I'm sorry Dan, but I this logic does not make any sense to me. First off, how would one php script know if another php exists to include it and know of which vars are defined?

I think the best way of storing data in a flat text file is to create a simple text .dat file or .csv (comma seperated value) file.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-28-2007, 12:54 PM Re: Still a PHP Newbie- post content?
dansgalaxy's Avatar
Eat, Sleep, Code

Latest Blog Post:
Offending facebook ads… so?
Posts: 6,092
Name: Dan
Location: Swindon
Sorry if your on about what i tihnk you are, then its because i just stright copied it from another script which creates a php config file... so it has php in it...

He could use this:
Code:
<form method="post" action="agescript.php">
<input type="text" name="age" maxlength="2" />
<input type="submit" value="Submit Age." />
</form>
Save the below as agescript.php
PHP Code:
Age some text here which the user will see when the age has been saved fine... or anything really...
<?php
$age 
$_POST['age']; //* what $age equals is what will be saved in the file. if you would like to save more than what is posted in the age field on that form say text you will need to put it in quotes... */
$agefile "agefile.txt"// you can change to any file type.
$fh fopen($agefile'w') or die("Can't open file");
$stringData "$age\n";
fwrite($fh$stringData);
fclose($fh);
?>
The above is untested but should work fine.

And then where every he wants the age to show on the page he just does <? include ('agefile.txt'); ?>

With regards to the file type, i only said php so that if he later needs it to save php or html or whatever it makes it easier. you can use any file type which a broswer can read as plain text...

Hope this helps.
And when it does TP will be greatly apprieciated...

Dan
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)

Last edited by dansgalaxy : 04-28-2007 at 01:04 PM.
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 04-30-2007, 07:50 PM Re: Still a PHP Newbie- post content?
Skilled Talker

Posts: 50
Thanks for all your help everybody- I'll try messing around with these codes for a little bit before I ask for more help. I'm trying to figure it out myself, but if I can't, I'll be sure to come get a few hints. *TP added*
anwipr is offline
Reply With Quote
View Public Profile
 
Old 05-01-2007, 12:54 PM Re: Still a PHP Newbie- post content?
dansgalaxy's Avatar
Eat, Sleep, Code

Latest Blog Post:
Offending facebook ads… so?
Posts: 6,092
Name: Dan
Location: Swindon
Thanks If you want more help, i can try add me if u fancy dansgalaxy@hotmail.com i can always try and share what knowledge i do haev
XD
Dan
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 05-02-2007, 04:47 PM Re: Still a PHP Newbie- post content?
Skilled Talker

Posts: 50
I'm using this code and I have four pages:
index.php - just the page with the content, implemented w/ PHP include
script.php - same as agescript.php above
calendar.txt - same as agescript.txt above
edit.php - page with the form given above

I've checked my variables to make sure they've been changed accordingly.

Anyways, when I fill out the form, I get directed to "script.php". How can I make it so that it directs me to "index.php" with all the changes made?
anwipr is offline
Reply With Quote
View Public Profile
 
Old 05-03-2007, 08:49 AM Re: Still a PHP Newbie- post content?
dansgalaxy's Avatar
Eat, Sleep, Code

Latest Blog Post:
Offending facebook ads… so?
Posts: 6,092
Name: Dan
Location: Swindon
ok, i supose u could make the index have the script in it, but it might be nicer to ahve a messgae like you info submitted, you are being redirected and have a meta redirect in there.

i think
<meta http-equiv="refresh" content="5;url=index.php">
This SHOULD make the page redirect to your index after 5 seconds.
you might have to make it the full path to it ie. http://dansgalaxy.co.uk/index.php

Hope this helps
Dan
__________________
Personal UK Webhosting
Get 25% of ANY shared pac