Form write to a file.

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

Post Reply
sketteksalfa
Forum Newbie
Posts: 5
Joined: Sun Aug 10, 2008 7:32 am

Form write to a file.

Post by sketteksalfa »

Hi guys, im not so good in programming, can you help me with this, i can sense this would be very simple to some of you. My problem is, i changed the action page from index.php (where this code is present) into a new page which is thankyouform.php. The issue here is, the form doesnt write to the file after i changed the action page. What seems to be the problem. Can someone help me pls. Below is the code.

Code: Select all

 
<form method="post" action="thankyouform.php">
<table align="center" width="751" bgcolor="#fe7202" border="0">
  <tr>
    <td width="277" valign="top" align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><strong>Sign up to this site so you can subscribe to our extensive list of financial newsletters and take advantage of our unique offers. </strong></font></td>
    <td width="70" height="24" valign="top">
<b><font face="Arial, Helvetica, sans-serif" >Name<br>
<br>
E-mail</font></b></td>
    <td width="390" valign="top"><input type="text" name="name" size="40">
      <br>
      <br>
      <input type="text" name="email" size="40"><br>
</td>
    </tr>
  <tr>
    <td align="left">    </td>
    <td colspan="2" align="left"> <input type="submit" name="submit" value="Find out now!" />
    
<?php
 
if ($_POST['submit'] == "Find out now!") {
$filename = "file.txt";
$ip = $_SERVER['REMOTE_ADDR'];
$content = "\r\n".$_POST['name'].",";
$content .= $_POST['email'].",";
$content .= $_POST['$ip'];
if (!$handle = fopen($filename, "a+<br>")) {
echo 'Cannot open file: ' . $filename;
exit;
}
if (!fwrite($handle, $content)) {
echo 'Cannot write to file: ' . $filename;
exit;
}
echo "<font color='#000000'>   Thank You!</font>";
}
?></td>
    </tr>
</table>
</form>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Form write to a file.

Post by requinix »

You have to move the PHP code to thankyouform.php.
sketteksalfa
Forum Newbie
Posts: 5
Joined: Sun Aug 10, 2008 7:32 am

Re: Form write to a file.

Post by sketteksalfa »

Yeah the code will definitely work. However, the form should be on a different page, such as when i submit the form, it will go to the thank you page and the form will also write to the file. What happened here is that, the form does not save the data from the txt file.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Form write to a file.

Post by requinix »

So did you move the PHP code to thankyouform.php or not?

(I assume this was working before?)
sketteksalfa
Forum Newbie
Posts: 5
Joined: Sun Aug 10, 2008 7:32 am

Re: Form write to a file.

Post by sketteksalfa »

Sorry i was kinda slow.. yes the code is working now after i transfer it to the thankyouform.php. Thanks for the assistance. But another thing... not sure if I did it correctly on writing the IP address. Something is wrong since it doesnt write the IP address to the file.. but the name and email does.
Loki
Forum Commoner
Posts: 25
Joined: Wed Jun 03, 2009 9:23 pm

Re: Form write to a file.

Post by Loki »

sketteksalfa wrote:Sorry i was kinda slow.. yes the code is working now after i transfer it to the thankyouform.php. Thanks for the assistance. But another thing... not sure if I did it correctly on writing the IP address. Something is wrong since it doesnt write the IP address to the file.. but the name and email does.
take the $ out of this line:

Code: Select all

$content .= $_POST['$ip'];
it should be:

Code: Select all

$content .= $_POST['ip'];
Post Reply