Home

Forums

Web development

 

 

 

 
     
 
dna88 Web development and Technology Forum
 
Profile   Register   Memberlist   Usergroups   FAQ   Search  Log in
Need simple subscribe unsubscribe newsletter email solution

 
Post new topic   Reply to topic    dna88 Forum Index -> Web scripting language Discussion Forum
Author Message
emm
Power User
Power User


Joined: 13 Jul 2004
Posts: 310

Post Post subject: Need simple subscribe unsubscribe newsletter email solution Reply with quote

Hi all,

I need a very basic script to let users subscribe or unsubscribe their email address. It should be very simple. Don't need any complex programming, so long as it stores the email addresses, it will be working for me. I don't need those big newsletter scripts. Any recommendation from anyone?

Thank you.
_________________
“You might say reality is the result of complex negotiations between the observer and the observed. But that is simply a point of view…”
Digital Bangladesh
Fri Jul 16, 04 5:53 am
Back to top
emm View user's profile Send private message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Make a simple subscription form in php Reply with quote

Funny, I just had to write a very simple script in php for adding a subscription and unsubscription option to a page. It takes two fields. Name and email.

a. The form inside a table below goes in the page where you want people to submit the info. You can change the look anyway you choose, just make sure the form and the field names inside the form are unchanged.

Code:

<form name="form" method="post" action="./mailsubmitted.php">
  <table width="300" border="0" align="center" cellpadding="0" cellspacing="3" bordercolor="#CCCCCC">
    <tr>
      <td align="right" valign="middle" bgcolor="#CCCCCC"><p>Name:</p></td>
      <td align="left" valign="middle" bgcolor="dddddd">
        <p>
          <input name="name" type="text" class="input" id="contactname2" size="10" maxlength="25">
      </p>        </td>
    </tr>
    <tr>
      <td align="right" valign="middle" bgcolor="#CCCCCC">
        <p>Email:</p></td>
      <td align="left" valign="middle" bgcolor="dddddd">
        <p>
          <input name="email" type="text" class="input" id="email" size="10" maxlength="25">
*        </p>        </td>
    </tr>
    <tr>
      <td width="65%" align="left" valign="middle">Please enter your name and email address
      to get notified when the site is updated. </td>
      <td align="center" valign="middle"><div align="center">
          <input type="submit" name="Submit" value="Submit" class="button">
      </div></td>
    </tr>
  </table>
  <p>&nbsp;</p>
</form>


b. The php code below goes to a page named mailsubmitted.php . This is where the user is redirected after submission. Create the layout anyway you choose according to your site theme. The php code below goes where you want to show the user the message about a successful submission or invalid email address. The name field is optional. The php code only checks for a valid email address in the form someone@somehost.com . If invalid the user can go back and enter the address again.

Code:
<?php
    $what = true;
    $chkemail = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email);
    if (!$chkemail) {
                echo "Please enter a valid email address in the form someone@somehost.com<br>
                Hit the browser's back button to go back and enter a valid email address.<br>";
                $what = false;
                }
    $formatted = $name." : ".$email;
    if ($what == true)
    {
    $fp = fopen("./maildata.txt", "a");//change the file name maildata.txt to somethingelse.txt for lil security.
    if (!$fp) die ("Sorry, Cannot open file to write Try Later.");
    fwrite($fp, "$formatted \r\n");
    fclose($fp);
                echo "Thank you for entering your email address. We will get back to you as soon as we launch our new site.";     
                }
?> 


c. Before you test it, make a txt file named maildata.txt or somethingelse.txt that you specify above in the code. As it is all files must be in the same directory. CHMOD the file permission to world writable. 766 or 777 whatever works in your server.

d. Now try and submit. To view the data download and open the text file. It should output something like this:

Quote:

sumon : sumon@hotmail.com
erich : erich@hotmail.com
loverboy : loverboy@yahoo.com
: loverboy@yahoo.com





Here is a slightly different version:

Quote:
Subscribe : test@yahoo.com
Unsubscribe : test1@yahoo.com
Unsubscribe : test2@yahoo.com
Subscribe : test3@yahoo.com
Subscribe : test4@yahoo.com
Unsubscribe : test5@yahoo.com



Form code:

Code:
<form method="post" action="mailsubmitted.php">
<table>
<tr align=left><td><b>Enter Your E-mail</b><br><input name="email" type=text id="email" style="width:120px;" size=10 maxlength="25"></td></tr>
<tr align=left><td><input name="chk" type=radio value="sub" checked>
&nbsp;<b>Subscribe</b><br></td></tr>
<tr align=left><td><input name="chk" type=radio value="unsub">
&nbsp;<b>Un Subscribe</b><br></td></tr>
<tr align=center><td><input type="submit" name="Submit" value="Submit" class="button">
                    <br></td></tr>
</table>
</form>



PHP:
Code:
<?php
    $what = true;
    $chkemail = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email);
    if (!$chkemail) {
                echo "Please enter a valid email address in the form someone@somehost.com<br>
                Hit the browser's back button to go back and enter a valid email address.<br>";
                $what = false;
                }
    if ($chk == "sub") {
    $formatted = "Subscribe"." : ".$email;
    }
    else $formatted = "Unsubscribe"." : ".$email;

    if ($what == true)
    {
    $fp = fopen("./maildata.txt", "a");//change the file name maildata.txt to somethingelse.txt for lil security.
    if (!$fp) die ("Sorry, Cannot open file to write Try Later.");
    fwrite($fp, "$formatted \r\n");
    fclose($fp);
    echo "Thank you for entering your email address. We will get back to you as soon as we launch our new site.";
    }
?> 

_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Sat Jul 17, 04 7:36 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
emm
Power User
Power User


Joined: 13 Jul 2004
Posts: 310

Post Post subject: Reply with quote

Wow, thank you quantum. That was very nice of you!
_________________
“You might say reality is the result of complex negotiations between the observer and the observed. But that is simply a point of view…”
Digital Bangladesh
Sun Jul 18, 04 3:34 am
Back to top
emm View user's profile Send private message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: simple Sunscribe unsubscribe form Reply with quote

Okay the old php code does not seem to work when register global is turned off on the server, which is the default case for php version 4.20 or above. In that case, the form variables do not seem to pass to the php page. The work around is posted below:

Code:
  <?php
  $email = $HTTP_POST_VARS['email'];
  $chk =  $HTTP_POST_VARS['chk'];
  $sub = $HTTP_POST_VARS['sub'];
  $unsub = $HTTP_POST_VARS['unsub'];
     
    $what = true;
    $chkemail = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email);
    if (!$chkemail) {
                echo "Please enter a valid email address in the form someone@somehost.com<br>
                Hit the browser's back button to go back and enter a valid email address.<br>";
                $what = false;
                }
    if ($chk == "sub") {
    $formatted = "Subscribe"." : ".$email;
    }
    else $formatted = "Unsubscribe"." : ".$email;

    if ($what == true)
    {
    $fp = fopen("./maildata.txt", "a");//change the file name maildata.txt to somethingelse.txt for lil security.
    if (!$fp) die ("Sorry, Cannot open file to write Try Later.");
    fwrite($fp, "$formatted \r\n");
    fclose($fp);
    echo "Thank you for entering your email address. We will get back to you as soon as we launch our new site.";
    }

?>

_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Wed Aug 04, 04 11:34 pm
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
emm
Power User
Power User


Joined: 13 Jul 2004
Posts: 310

Post Post subject: Reply with quote

Thanks for the update. I was using 4.01 or something below 4.20 for sure. That is why I had no problem using it. Thanks anyway. Gentle smile
_________________
“You might say reality is the result of complex negotiations between the observer and the observed. But that is simply a point of view…”
Digital Bangladesh
Mon Aug 09, 04 3:11 am
Back to top
emm View user's profile Send private message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Php simple Subscription form Reply with quote

So here is another useful variation of the original subscribe unsubscribe script that I wrote. My original script was modified by someone else, then I fixed it. This new version basically adds and deletes email entries depending on whether the user subscribed or unsubscribed. The html form code is the same with the same exact variables.

Code:
<?php
$chk = $HTTP_POST_VARS['chk'];
$email = $HTTP_POST_VARS['email'];
$submit = $HTTP_POST_VARS['submit'];

$what = true;
$chkemail = ereg("^[^@ ]+@[^@ ]+.[^@ .]+$", $email);

if (!$chkemail) {
echo 'Please enter a valid email, in the form someone@somehost.com'
;
$what = false;
}

if ($what == true)
{

// subscribe
if ($chk == "sub") {
    $formatted = $email;
    $fp = fopen("./mailinfo.txt", "a");
    if (!$fp) die ("Unable to open database.");
    fwrite($fp, "$formatted \r\n");
    fclose($fp);
    echo'Successfully subscribed.';
}
// end subscribe
// unsubscribe
if ($chk == "unsub") {
$formatted = $email;
    $filename="./mailinfo.txt";
    $file=file($filename);
    for($i=0;$i<count($file);$i++)
    {
        if(trim($file[$i])!=$formatted)
            $writeString.=$file[$i];
        else
            $deleted=1;
    }
    $openF=fopen($filename,"w");
    fwrite($openF,$writeString);
    fclose($openF);

    if($deleted)
    echo 'Successfully un-subscribed.';
    else {
    echo 'Page content other.';

    }

}
}
// end unsubscribe
?>

_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Fri Aug 13, 04 7:09 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
Display posts from previous:   
Post new topic   Reply to topic    dna88 Forum Index -> Web scripting language Discussion Forum All times are GMT - 7 Hours
Page 1 of 1

 

Partners and Resources

Bangladesh hosting company

Bangladesh web design

Driven by phpBB © phpBB Group