From a form to a database?

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

AbrasiveRock
Forum Commoner
Posts: 40
Joined: Sat Feb 08, 2003 10:47 pm
Location: Olympia,WA
Contact:

From a form to a database?

Post by AbrasiveRock »

Ok, being the major newbie that I am, I may have bitten off more than I can chew. What I want to do is be able to type something into a text box on one page, press the button, and have it go to another page that displays the text in a semi-permanent way.

Here is my attempt:First page

Code: Select all

<form action="secondpage.php" method="POST">
Enter Your Name:
<input type="text" name="userName">
<input type="submit" name="submit" value="go">
</form>
That then goes to...

Code: Select all

<?php echo $_POST&#1111;"userName"]; ?>
This does not seem to work at all and any help would be appreciated.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What version of PHP are you using? If you're not sure, use this code to find out:

Code: Select all

echo phpversion();
Mac
Cogs
Forum Newbie
Posts: 17
Joined: Thu Mar 27, 2003 4:57 pm

Post by Cogs »

Try $HTTP_POST_VARS["userName"];
AbrasiveRock
Forum Commoner
Posts: 40
Joined: Sat Feb 08, 2003 10:47 pm
Location: Olympia,WA
Contact:

Post by AbrasiveRock »

twigletmac wrote:What version of PHP are you using? If you're not sure, use this code to find out:

Code: Select all

echo phpversion();
Mac
Ok, I tried that as...

Code: Select all

echo phpversion();
as well as...

Code: Select all

&lt;?php echo phpversion(); ?&gt;
(the question mark/close bracket wont show up at the end, but I used it)

and neither told me what version of PHP I am using. Did I have something in the code wrong? Although I'm pretty sure it's the latest one. I think it's PHP 4.3.2RC1, but I could be wrong.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

That's weird, the code came from here:
http://www.php.net/manual/en/function.phpversion.php

Is PHP running properly? Do you get anything if you run a file with just:

Code: Select all

<?php
phpinfo();
?>
in it?

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if not open the "source view" in your browser. If you can see the complete text of the script there your server does not parse php at all and you have to check your installation again.
btw: you opened the script via http://localhost/... or similar? ;)
AbrasiveRock
Forum Commoner
Posts: 40
Joined: Sat Feb 08, 2003 10:47 pm
Location: Olympia,WA
Contact:

Post by AbrasiveRock »

That is what is so strange, all the other PHP works fine. Also, View Source only gives me the html like it should, not the PHP. That little script should have worked.

FTR-I should be on AIM and MSN all day Saturday (after 12 noon PST) if anyone is willing. My nic on both is just 'abrasiverock'.
AbrasiveRock
Forum Commoner
Posts: 40
Joined: Sat Feb 08, 2003 10:47 pm
Location: Olympia,WA
Contact:

Post by AbrasiveRock »

Ok, I know this is going to sound sad and pathetic, but I just got the PHP info thing to work...I think? :oops: http://216.177.247.144/phpinfo.php and it says that "Current PHP version: 4.2.2".


So now I'm going to crash and work all Sunday on trying to learn how to conquer the original task of this post...taking information from a text box and adding it to a database. :?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if still nothing appears try

Code: Select all

<html><body><pre>
<?php
echo 'POST: ';
print_r($_POST);
echo 'GET: ';
print_r($_GET);
echo 'REQUEST: ';
print_r($_REQUEST);


echo '$HTTP_POST_VARS: ';
print_r($HTTP_POST_VARS);
echo 'HTTP_GET_VARS: ';
print_r($HTTP_GET_VARS);
?>
</pre></body></html>
AbrasiveRock
Forum Commoner
Posts: 40
Joined: Sat Feb 08, 2003 10:47 pm
Location: Olympia,WA
Contact:

Post by AbrasiveRock »

Ok, my first page says...

Code: Select all

<html>
<head>
<title>Enter text page</title>
</head>
<BODY BGCOLOR="#303030" TEXT="FFFFFF" LINK="yellow" VLINK="orange" ALINK="red">
<form action="secondpage.php" method="POST">
Enter Your Name:
<input type="text" name="userName">
<input type="submit" name="submit" value="go">
</form>

</body>
</html>
and the second page looks like...

Code: Select all

<html>
<head>
<title>second page</title>
</head>
<body>

<table border=1 BGCOLOR="#303030" width="100%" height="100%">
<TR>
<td width="25%" valign="top"><font color="#FFFFFF"><B>My stuff should post below</b><hr>

#This is the only part I care about right now
#This is just where I want stuff to echo, post, print

<?php echo $_POST&#1111;"userName"]; ?>
<?php print "$userName<BR>"; 
?>

<?php
$filePointer = fopen("data.txt","w");

fputs($filePointer, "$userName\r\n", 1024);
fclose($filePointer);
?>

</font>
</td><td width="75%" valign="top"><font color="#FFFFFF"><?php echo $_POST&#1111;"NothingRightNow"]; ?></font>

</td></tr><tr height="1%"><td>
<font color="#FFFFFF">Blah Blah Blah!</font>
</td><td>

<form action="secondpage.php" method="POST">

<textarea name="NothingRightNow" COLS="63" ROWS="1"></textarea>
<input type="submit" name="submit" value="GO!">
</form>

</tr>
</TABLE>
</body>
</html>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Is it still not working? Did you try the code Volka gave you?

Mac
AbrasiveRock
Forum Commoner
Posts: 40
Joined: Sat Feb 08, 2003 10:47 pm
Location: Olympia,WA
Contact:

Post by AbrasiveRock »

Well, it shows up on the next page the first run through, but does not write to the database. So the text on the page goes away as soon as someone tries to type something in the text box at the bottom.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Make sure that your file is CHMODed to 777.
Image Image
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

with database you refer to
<?php
$filePointer = fopen("data.txt","w");

fputs($filePointer, "$userName\r\n", 1024);
fclose($filePointer);
?>
? If so keep in mind that fopen(...,"w"); will not set the filepointer to the end of the file; every request will overwrite the previous content.
fputs($filePointer, "$userName\r\n", 1024);
which one did work, echo $_POST["userName"]; or print "$userName<BR>" ?

What do you want to achieve?
AbrasiveRock
Forum Commoner
Posts: 40
Joined: Sat Feb 08, 2003 10:47 pm
Location: Olympia,WA
Contact:

Post by AbrasiveRock »

So if I understand you correctly, the text put into the text box on the first page, goes to the second page, and then is covered up the the...

Code: Select all

fputs($filePointer, "$userName\r\n", 1024);
...line of code? Should I take it out or change it to make it write to a database? Or did I totally miss what you said?
Post Reply