Page 1 of 1

Form write to a file.

Posted: Tue Jun 16, 2009 2:39 pm
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>
 

Re: Form write to a file.

Posted: Tue Jun 16, 2009 2:57 pm
by requinix
You have to move the PHP code to thankyouform.php.

Re: Form write to a file.

Posted: Thu Jun 18, 2009 12:19 pm
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.

Re: Form write to a file.

Posted: Thu Jun 18, 2009 12:46 pm
by requinix
So did you move the PHP code to thankyouform.php or not?

(I assume this was working before?)

Re: Form write to a file.

Posted: Fri Jun 19, 2009 11:03 am
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.

Re: Form write to a file.

Posted: Fri Jun 19, 2009 11:35 am
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'];