Solved

PHP Form to Flat file

Posted on 2010-09-02
19
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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 83

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
 
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 110

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: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

Independent Software Vendors: 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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
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 …

749 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