Jump to content

[SOLVED] writing to file


spillage

Recommended Posts

Hi this is all new to me and cannot spot my mistake here so hoping someone can point out the obvious. Why will this not send to my .txt file???. $cust is been posted from an email.

 

<form action="http://localhost/mail01.php" method="post">

<table border="0" align="left">

<tr>  <td>Friends Email: </td>

  <td align="left"><input type="text" name="address" size=40 maxlength=50></td>

</tr>

  <td colspan=2 align=center><input type=submit value="Send Details"></td>

</tr>

<?php

$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];

$cust=$_POST["email"];

$friend=$_POST["address"];

$output= "\t".$friend."\n";

echo "Your email is: ".$cust;

@ $fp= fopen ("DOCUMENT_ROOT/rcfriend.txt",'a+');

fwrite ($fp,$output, strlen($output));

fclose($fp);

?>

 

Thanks

 

Mark

Link to comment
https://forums.phpfreaks.com/topic/102394-solved-writing-to-file/
Share on other sites

<?php
$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
$cust=$_POST["email"];
$friend=$_POST["address"];
$output= "\t".$friend."\n";
echo "Your email is: ".$cust;
@ $fp= fopen ("$DOCUMENT_ROOT/rcfriend.txt",'a+');
fwrite ($fp,$output, strlen($output));
fclose($fp);
?>

 

You missed a $.  Does it echo the Your email is: part?

Hi Dark Water.

 

Thanks it would have taken me all night to spot that one and has sorted out that problem.

 

Yes it does echo 'your email' although when you submit the page this then disapears which looks a bit cacky would you have any ideas on that. Trying to find out if

I can $_POST into a form so that it would always loop when the submit button is clicked.

 

Thanks

 

Mark

 

 

<?php
if (isset($_POST['submitted']) {
$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
$cust=$_POST["email"];
$friend=$_POST["address"];
$output= "\t".$friend."\n";
echo "Your email is: ".$cust;
@ $fp= fopen ("$DOCUMENT_ROOT/rcfriend.txt",'a+');
fwrite ($fp,$output, strlen($output));
fclose($fp);
}
?>
<form action="http://localhost/mail01.php" method="post">
<table border="0" align="left">
<tr>  <td>Friends Email: </td>
  <td align="left"><input type="text" name="address" size=40 maxlength=50></td>
</tr>
  <input type="hidden" name="submitted" value="true" />
  <td colspan=2 align=center><input type=submit value="Send Details"></td>
</tr>

 

The script sends it back to itself and handles the data, right?  This is a better version if that's the case.

 

When running this I get

 

Parse error: syntax error, unexpected '{' in C:\wamp\www\mail01.php on line 19

 

The $cust=$_POST["email"]; comes from another html page (email). This variable is the one that I would like to loop within this code.

 

Cheers.

 

<?php

$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
$cust=$_POST["email"];
$friend=$_POST["address"];
$output= "\t".$friend."\n";
echo "Your email is: ".$cust;
@ $fp= fopen ("$DOCUMENT_ROOT/rcfriend.txt",'a+');
fwrite ($fp,$output, strlen($output));
fclose($fp);

?>
<form action="http://localhost/mail01.php" method="post">
<table border="0" align="left">
<tr>  <td>Friends Email: </td>
  <td align="left"><input type="text" name="address" size=40 maxlength=50></td>
</tr>

  <td colspan=2 align=center><input type=submit value="Send Details"></td>
</tr>

 

Woops, didn't understand your file structure the first time.  I fixed the code so it works, but now what exactly are you trying to do after this?

When they submit the information I want to have the $cust (your email is) email still showing on the page so if they then submit another friends email both emails are written to the file and not just the friends email. Hope this makes sense.

 

Cheers.

 

Add this to the top:

session_start();

if (isset($_POST['email'])) {

  $_SESSION['email']=$_POST['email'];

}

$cust = $_SESSION['email'];

 

Put the instead of your current $cust lines, and add session_start() as the VERY first line under <?php.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.