Jump to content

[SOLVED] Help doing script!!


juapo2

Recommended Posts

Hi,

 

I have various very good scripts but i need help coding something some people may say is easy...

 

I need a form, where my users add their e-mail adress to receive newsletter, i just need them to write their e-mail adress, and make the e-mail adress go into a .txt file, something like emails.txt

 

I dont need script to send the newslettter or to unsubcribe them, just that their email adress goes into the .txt file,

 

I would appreciate if you could help,

 

Thany YOU. ;)

Link to comment
Share on other sites

No offense....  But that's extremely simple, and it sounds like you want someone to do it for you, which this board is not for (well, a section of it is, but....).

 

http://www.tizag.com/phpT/files.php

http://php.net/fopen

http://php.net/fwrite

http://www.google.com/search?hl=en&q=php+file+writing

 

Try writing something, and then we can try to help ;p.

Link to comment
Share on other sites

what corbin is trying to say is that we all have jobs and lives. we are not gonna sit here and write code for others. (if you want that, hire a web developer) You can write code, see if it works, if it doesn't, then post it here and we'll tell you whats wrong with it.

 

but we can tell you HOW do to it...

 

specify a file

open it

add text to it

save it

close it

 

thats the general guideline of it.

Link to comment
Share on other sites

I got the form here is it, the HTML form:

 

<h1>NewsLetter Subscribers</h1> <form action="subscribe.html" method="post" name="form">  Name: <input name="mailing_list_name" type="text" size="40"><br><br>  Email: <input name="mailing_list_email" type="text" size="40">  <br><br>  <input name="submit" type="submit" value="Submit">  <input name="reset" type="reset" value="Reset"> </form>

 

Then this is my PHP code to add the e-mail to the list:

 

<?php
//Add item to list if(isset($_POST['submit']) && $_POST['mailing_list_name'] != "" && $_POST['mailing_list_email'] != "") { $file_ptr = fopen("list.txt", "a+") or die("Couldn't create new file"); fwrite($file_ptr, $_POST['mailing_list_name'] . "\r\n"); fwrite($file_ptr, $_POST['mailing_list_email'] . "\r\n\r\n"); fclose($file_ptr); } 
?>

 

And in my web server, i have a lists.txt file chmod to 777

 

Then i call the subscribe.html by doing this:

 

http://mydomain.com/subscribe.html

 

then, i add my name and email and i click submit, the same page appears and the lists.txt is still empty,

now is there any1 that could help me?

Link to comment
Share on other sites

this should be the code on subscribe.php

 

<?php
//Add item to list 
if(isset($_POST['submit']) && $_POST['mailing_list_name'] != "" && $_POST['mailing_list_email'] != "") {
$file_ptr = fopen("list.txt", "a+") or die("Couldn't create new file");
fwrite($file_ptr, $_POST['mailing_list_name'] . "\r\n");
fwrite($file_ptr, $_POST['mailing_list_email'] . "\r\n\r\n");
fclose($file_ptr); } 
?>

 

see the difference?

Link to comment
Share on other sites

hey jaupo2...

 

i have the same exact function in php. visitors enter an e-mail and it gets stored in a text file...

 

but i have a suggestion for you...

 

(heres my code...)

 

<?php

 

  if (!isset($_POST['submit'])) {?>

<form method="post">

E-Mail:<input type="text" size="15" maxlength="30" name="email"/>

<br/>

<input type="submit" name="submit" value="Submit"/>

<form>

<?php

} else {

  $email = $_POST["email"];

  $emailfile = "../emaillist.txt";

  $FH = fopen($emailfile, 'a') or die("can't open file");

  fwrite($FH,$email . ','); //LOOK AT THIS PART

  fclose($FH);

}

 

?>

 

look at the part that the comment tells you too....

see the ',' in "fwrite($FH,$email . ','); ", this tells the script to write a comma after the name, this will make your life a whole lot easier, all you have to do is copy and paste the e-mails, rather tahn seperating it youself. also, unless you have a realy good reason to want to know their names, it unneccesary

 

also, put the

if(isset($_POST['submit'])

before your html text fields, if they have already submitted, it will not show the fields.

 

but one question.. what does

"\r\n\r\n");

do?

 

 

 

Link to comment
Share on other sites

Thanks ohdang888 for sharing your code,

 

So its just one .php file that i have to create with this code inside?

<?php

  if (!isset($_POST['submit'])) {?>
<form method="post">
E-Mail:<input type="text" size="15" maxlength="30" name="email"/>


<input type="submit" name="submit" value="Submit"/>
<form>
<?php
} else {
  $email = $_POST["email"];
  $emailfile = "../emaillist.txt";
  $FH = fopen($emailfile, 'a') or die("can't open file");
  fwrite($FH,$email . ','); //LOOK AT THIS PART
  fclose($FH);
}

?>

 

Or i have to separate the code in an html code and php...?

 

OH, and Northern Flame, isnt the same

<?php
//Add item to list if(isset($_POST['submit']) && $_POST['mailing_list_name'] != "" && $_POST['mailing_list_email'] != "") { $file_ptr = fopen("list.txt", "a+") or die("Couldn't create new file"); fwrite($file_ptr, $_POST['mailing_list_name'] . "\r\n"); fwrite($file_ptr, $_POST['mailing_list_email'] . "\r\n\r\n"); fclose($file_ptr); } 
?>

 

Than

<?php
//Add item to list 
if(isset($_POST['submit']) && $_POST['mailing_list_name'] != "" && $_POST['mailing_list_email'] != "") {
$file_ptr = fopen("list.txt", "a+") or die("Couldn't create new file");
fwrite($file_ptr, $_POST['mailing_list_name'] . "\r\n");
fwrite($file_ptr, $_POST['mailing_list_email'] . "\r\n\r\n");
fclose($file_ptr); } 
?>

 

If it is not, i save that code in my subscribe.html, and change it to subscribe.php?

After i change it, i upload subscribe.php, createlists.php, and lists.txt (CHMOD 777)?

Link to comment
Share on other sites

http://www.w3schools.com/php/php_syntax.asp

 

Glance through or read "Comments in PHP" please.

 

You can mix HTML and PHP easily, and that's actually done a lot of the time....

 

The basic thing you would want to follow would be:

 

$added = false;
if($_POST) {
     if(<vars validate>) {
          //add to file
          if(<Added to file>) {
               $added = true;
          }
     }
}

if($added == true) {
      //success message
}
else {
?>
HTML for form here
<?php

//or you could use heredoc format:
echo <<<HERE
<!--html for form here -->
HERE;

//ory ou could just use echos, with a little escaping

echo '<html for form here>';

}

Link to comment
Share on other sites

Check this, i have this code, it works pretty good, writes the email to the file and eveything, but when i click submit, the screen goes white, and in the address bar it appears:

 

 

What can i do so when they click submit, they redirect to a thank page?

Link to comment
Share on other sites

Heres my code:

 

test.html:

 

<form action='addemail.php' method='GET'>
E-mail: <input name="email" type="text" />
<input name="submit" type="submit" value="Submit" />
<input name="reset" type="reset" value="Reset" />
</form>

 

addemail.php:

 

<?php
if (isset($_GET['submit'])){

$email = $_GET['email'];

//adding a new line character so that each email address has its own line
$email = "$email \n";

//opening the file for writing
$textfile = fopen("emails.txt","a");

//writing the email
fwrite($textfile, $email);

//closing the textfile
fclose($textfile);
}
?>

 

And lists.txt (CHMOD 777)

Link to comment
Share on other sites

<?php
if (isset($_GET['submit'])){

$email = $_GET['email'];

//adding a new line character so that each email address has its own line
$email = "$email \n";

//opening the file for writing
$textfile = fopen("emails.txt","a");

//writing the email
fwrite($textfile, $email);

//closing the textfile
fclose($textfile);
}
?>

<h1>Thank you!</h1>

Thanks for subscribing!

 

Or, if you want it to go to a different page:

 

<?php
if (isset($_GET['submit'])){

$email = $_GET['email'];

//adding a new line character so that each email address has its own line
$email = "$email \n";

//opening the file for writing
$textfile = fopen("emails.txt","a");

//writing the email
fwrite($textfile, $email);

//closing the textfile
fclose($textfile);
}

header("Location: http://yoursite.com/thanks.html");

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.