Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Form to Flat file

Posted on 2010-09-02
19
Medium Priority
?
519 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 668 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 84

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 668 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
 
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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 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:Shinesh Premrajan
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

876 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