• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 529
  • Last Modified:

PHP Form to Flat file

Hello Experts,

I'm a bit new to PHP and I'm trying to get a very very simple php form to work. I need to have a single input field where users can type an email address and click "submit".

The email address needs to be written to a flat file on the server. And the submit button should redirect the users to the next page on my site.

I also need the field to not be mandatory. Meaning they can click submit without entering an email address.

We're just looking to as the user "who referred you".

Any help would be greatly appreciated.
0
kinetik20
Asked:
kinetik20
  • 6
  • 5
  • 4
  • +4
3 Solutions
 
JtRCommented:
try this:
<?php

if ($GET['do']=='next')
{
	if (!empty($_POST["mmail"]))
	{
		$fh = fopen("myfile.dat","a+");
		fputs($fh,$_POST["mmail"]."\r\n");
		fclose($fh);
		
	}
	
	echo '<meta http-equiv="Refresh" content="0;URL=http://www.google.com" />';
	
	
}
else
{
	echo <<<MYFORM
<form method="post" action="?do=next">
		<p>
		  <input name="mmail" type="text" />
  </p>
		<p>
		  <input type="submit" name="button" id="button" value="Senden" />
        </p>
</form>	
MYFORM;


}











?>

Open in new window

0
 
JtRCommented:
corrected version:
<?php

if (!empty($_GET['do']) && $_GET['do']=='next')
{
	if (!empty($_POST["mmail"]))
	{
		$fh = fopen("myfile.dat","a+");
		fputs($fh,$_POST["mmail"]."\r\n");
		fclose($fh);
		
	}
	
	echo '<meta http-equiv="Refresh" content="0;URL=http://www.google.com" />';
	
	
}
else
{
	echo <<<MYFORM
<form method="post" action="?do=next">
		<p>
		  <input name="mmail" type="text" />
  </p>
		<p>
		  <input type="submit" name="button" id="button" value="Senden" />
        </p>
</form>	
MYFORM;


}











?>

Open in new window

0
 
JtRCommented:
the emails are stored in myfile.dat then and it's redirected to google.. so you can take it as a base for improvement

maybe you still should build in a check if it is really a email address that is put in
and i don't know how it works when there are two synchronous accesses to the script (only one can write to the file)
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
kinetik20Author Commented:
Thank you! I'll give it a try, and it shouldn't be an issue with synchronous access as it will be a very low volume script.
0
 
Dave BaldwinFixer of ProblemsCommented:
I was still typing when @JtR posted.  Here is a different version.  The redirect is in a hidden field in the form.  The PHP file include a section to display the file if you comment out the header statement.
formEm.html
formEm1.php
0
 
ploftinCommented:
I was typing as well.
<?php
if ($_POST["email"] != "") { // If an email address was submitted.
	$content = $_POST["email"]; 
	$fp = fopen("myText.txt","wb"); 
	fwrite($fp,$content); 
	fclose($fp); 
}

// Redirect the user.
header('Location: http://www.yoursite.com/thankyou.html');
?>
<html>
<head>
<title>Email</title>
</head>
<body>
<form action="" method="post">
Email address: <input type="text" name="email" id="email"><br />
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</body>
</html>

Open in new window

0
 
JtRCommented:
plotfins version would not fit the "not mandatory" clause.. ;)
0
 
JtRCommented:
ah, no it would, sorry.. but maybe writing directly into the header could later conflict with session management.
0
 
kinetik20Author Commented:
JtR,

I'm trying to use your solution and it seems to work well however is there a way to do it without the meta refresh?
0
 
ploftinCommented:
The meta refresh is the redirection to the "thank you" page after the end user submits the form.  Change the address to whatever you like.
0
 
ploftinCommented:
As a clarification, my previous post WOULD fit the "not mandatory" clause.
0
 
kinetik20Author Commented:
We're making use of mod_rewrite and I believe its preventing the meta refresh from forwarding to the correct site. It just dumps us back to the homepage with a www.domain.com/?do.next 
0
 
russianryebreadCommented:
@kinetik20:
Any reason you are not wanting to use something like sqlite?  Convenience of a flat file, flexibility of a database.  It's all built right into PHP.  There is a quick and dirty tutorial on the php website in the user notes: http://us.php.net/manual/en/book.sqlite.php#82875
0
 
kinetik20Author Commented:
@russianryebread

I suppose a database would be the right way to go about things however I just need a simple way to collect email addresses, then monthly go in and clear the file.

Same as dropping data out of a database I suppose I just thought this would be the easiest way to go about things.
0
 
ploftinCommented:
If you can't use a meta refresh, try a php header statement like this.  Replace JtR's line13 with this:
header('Location: http://www.yoursite.com/thankyouhtml');

Open in new window

0
 
JtRCommented:
yes, this is excactly what i said above.. he should combine yours and mine BUT it can be problematically when he uses sessions/cookies because he always need to write the cookie informations before the header. but this is just a problem of code organisation ;D
0
 
ploftinCommented:
JtR:
Strange. I don't see any posts where you suggested combining your code with mine. You had said writing to the headers could be a problem. :)

Kinetik20:
On a side note, my entire first code segment may work exactly as needed. :)
0
 
Ray PaseurCommented:
Here is a good book that will help you get your thinking straight about this and much more in PHP.
http://www.sitepoint.com/books/phpmysql4/

PHP does not have forms.  Forms are written in HTML.  PHP is the server-side language that processes the data from the forms.  The information from the forms is typically found in $_REQUEST.

The action script is named in the "action=" attribute of the <form> tag.
http://www.w3.org/TR/html401/interact/forms.html

Your action script does not need to do any redirecting -- a competent design would make it the next page of the site.

HTH (and buy that book), ~Ray
0
 
Shinesh PremrajanEngineering ManagerCommented:
<?php

if (isset($_POST['button']) && $_POST['button']=='Senden')
{
      if (!empty($_POST["mmail"]))
      {
            $fh = fopen("myfile.dat","a+");
            fputs($fh,$_POST["mmail"]."\r\n");
            fclose($fh);
            
      }
      
     echo '<meta http-equiv="Refresh" content="0;URL=http://www.google.com" />';
      
}
else
{
      echo <<<MYFORM
<form method="post" action="scriptname.php">
            <p>
              <input name="mmail" type="text" />
  </p>
            <p>
              <input type="submit" name="button" id="button" value="Senden" />
        </p>
</form>      
MYFORM;
}
?>

Hope this helps
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 6
  • 5
  • 4
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now