3

I wrote a html form just with an email field. I want to transfer that email that the user inputs into a text file on my web server.

When I click on the submit button I get a white screen. Nothing is written to my text file. I also want to redirect the user to another page when they click the submit button. Is this possible? I am a newbie.

<?php

$file = fopen("emaillist.txt","a+");
fwrite($file,$email);
fclose($file); 
print_r(error_get_last());

?>

<form action= "emaillist.php" method="post" name="email ">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="email" value="submit"><br>
2
  • You are depending on register_globals being activated. it is not a deprecated and REMOVED mis-feature in PHP. your code is also trying to append to that file, whether a POST was actually performed or not... Commented Jul 25, 2013 at 20:56
  • You have two names of the same <form name="email" and <input type="submit" name="email" value="submit">. For starters, rename your <form name to something else. Commented Jul 25, 2013 at 21:10

7 Answers 7

1

In addition to what Andy G said about changing the name of submit button:

<?php
//Get the email from POST
$email = $_REQUEST['email'];
$file = fopen("emaillist.txt","a+");
fwrite($file,$email);
print_r(error_get_last());

//redirect
header("Location: http://www.example.com/");

Don't leave any blank lines between <?php and the beginning of the file, otherwise redirect won't work.

Sign up to request clarification or add additional context in comments.

Comments

1

In php theres an array called $_POST that stores all of the variables from a form when you post it. So $email would be found in $_POST['email']

Comments

1

The submission results will be stored in the $_POST variable:

<?php

$file = fopen("emaillist.txt","a+");

$email = $_POST['email']; 

fwrite($file,$email);
fclose($file); 
print_r(error_get_last());

?>

Comments

1

You have a few things you'll need to update here:

Form elements need to have different names, so you can differentiate between them in the PHP server-side code. Something like this:

<form action="emaillist.php" method="post">
    <input type="text" name="email" />
    <br />
    <br />
    <input type="submit" name="emailSubmit" value="submit" />
    <br />
</form>

You need to wrap your server-side code in a conditional to make sure it's a form post. Currently, when you first load this page, it's going to write to the file. But there's no content until you post back to the page again with the form data. So you need to check if it's a form post. A common way to do this is to check for the presence of the submit button in the post data:

if (isset($_POST["emailSubmit"])) {
    $file = fopen("emaillist.txt","a+");
    fwrite($file,$email);
    fclose($file); 
    print_r(error_get_last());
}

Just to confirm, since you say you're new to PHP... You're expecting the file to be written on the server and not on the client, correct?

To redirect to another page, you can simply set the location header in the server-side PHP code. This needs to be done before any content is emitted to the client. Since you only want this to happen on a post event, it should be in the same conditional as before:

if (isset($_POST["emailSubmit"])) {
    $file = fopen("emaillist.txt","a+");
    fwrite($file,$email);
    fclose($file); 
    print_r(error_get_last());

    header("Location: http://www.yoursite.com/newPage.php") ;
}

1 Comment

Written on the server and not on the client you are correct. Thanks seems like it is working now, I got the redirect working and now just need to put a \n like somewhere in the code.
1

Try this, Use different name for submit button also check if the submit button was pressed than write to file. You also didn't get the posted email values

<?php
if(isset($_POST['submit']))
{
    $email = $_POST['email'];
    $file = fopen("emaillist.txt","a+");
    fwrite($file,$email);
    fclose($file); 
    print_r(error_get_last());
}
?>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>

2 Comments

<form name="email " and <input type="text" name="email">? Something is bound to go wrong.
@Fred yup he was having so many errors in the code, Edited the answer. Thanks!
1
<?php
if (isset($_POST['emailSubmit'])) {
    $file = fopen('emaillist.txt','a+');
    $email = $_POST['nail'];
    $fmail = $email.PHP_EOL;
    fwrite($file,$fmail);
    fclose($file); 
    print_r(error_get_last());

    header("Location: http://www.yourdomain.com/thanks.html") ;
}

?>
<form action='emaillist.php' method='post'>
    <input type='text' name ='nail' size= '30'/>
    <br />
    <br />
    <input type='submit' name='emailSubmit' value='OK'/>
    <br />
</form>

Comments

0

Your submit button and input element and the form, have the same name "email". The first thing to do is to give them different names.

The name of the form also includes an extra space "email ".

2 Comments

Thank you all. Now I am able to write to a text file. Renaming from email to other names helped. Now all the emails in the text document are on the same line. I know that you need to have a /n in the php code. Not sure where exactly. Also the header tag is not redirecting either even though I have it in the php code. I am not a JEDI YET!!! Thanks guys.
You mean \n ;). Note that the name property of a form isn't really used these days. Give the form an id though, which can be used by JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.