Solved

PHP Form to Flat file

Posted on 2010-09-02
19
494 Views
Last Modified: 2012-08-14
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
Comment
Question by:kinetik20
  • 6
  • 5
  • 4
  • +4
19 Comments
 
LVL 3

Expert Comment

by:JtR
ID: 33588561
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
 
LVL 3

Accepted Solution

by:
JtR earned 167 total points
ID: 33588587
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
 
LVL 3

Expert Comment

by:JtR
ID: 33588603
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
 
LVL 4

Author Comment

by:kinetik20
ID: 33588615
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33588650
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
 
LVL 5

Assisted Solution

by:ploftin
ploftin earned 167 total points
ID: 33588667
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
 
LVL 3

Expert Comment

by:JtR
ID: 33588710
plotfins version would not fit the "not mandatory" clause.. ;)
0
 
LVL 3

Expert Comment

by:JtR
ID: 33588728
ah, no it would, sorry.. but maybe writing directly into the header could later conflict with session management.
0
 
LVL 4

Author Comment

by:kinetik20
ID: 33588739
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:ploftin
ID: 33588826
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
 
LVL 5

Expert Comment

by:ploftin
ID: 33588839
As a clarification, my previous post WOULD fit the "not mandatory" clause.
0
 
LVL 4

Author Comment

by:kinetik20
ID: 33588850
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
 
LVL 3

Expert Comment

by:russianryebread
ID: 33589002
@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
 
LVL 4

Author Comment

by:kinetik20
ID: 33589054
@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
 
LVL 5

Expert Comment

by:ploftin
ID: 33589056
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
 
LVL 3

Expert Comment

by:JtR
ID: 33589212
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
 
LVL 5

Expert Comment

by:ploftin
ID: 33589309
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 33589606
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
 
LVL 17

Expert Comment

by:shinuq
ID: 33593622
<?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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now