Hello, I am a newbie coder and I am trying to create a php mailing list.

I would like the users to enter their email address into the text box
and click submit and have the email addresses seperated by ;'s and
imported into my list.txt file.

Here is the code I found that I am currently using.

<?php
$email = $_POST['email'];
$data = "list.txt";
$open = fopen($data, "a");
fwrite($open, $email .  "\n" );
fclose($open);
echo '<form style="visibility:hidden" action="http://www.stukface.com" method="post" name="theForm" id="theForm"><input type="hidden" name="NextPage" value=""> <input name="email" type="text" value="' . $_POST['email'] . '"><input type="submit" name="Submit22" value="submit" alt="submit"></form>';
echo '<SCRIPT LANGUAGE="JavaScript"> javascript:document.forms[0].submit() </script>'
?>

The redirect isn't working correctly. Does anyone know what is wrong?

I am also interested in knowing if there could be a way to validate the email so that it is in proper email form ie 123@acb.com.

Thank you very much.

Hello, I am a newbie coder and I am trying to create a php mailing list.

I would like the users to enter their email address into the text box
and click submit and have the email addresses seperated by ;'s and
imported into my list.txt file.

Here is the code I found that I am currently using.

<?php
$email = $_POST['email'];
$data = "list.txt";
$open = fopen($data, "a");
fwrite($open, $email .  "\n" );
fclose($open);
echo '<form style="visibility:hidden" action="http://www.stukface.com" method="post" name="theForm" id="theForm"><input type="hidden" name="NextPage" value=""> <input name="email" type="text" value="' . $_POST['email'] . '"><input type="submit" name="Submit22" value="submit" alt="submit"></form>';
echo '<SCRIPT LANGUAGE="JavaScript"> javascript:document.forms[0].submit() </script>'
?>

The redirect isn't working correctly. Does anyone know what is wrong?

I am also interested in knowing if there could be a way to validate the email so that it is in proper email form ie 123@acb.com.

Thank you very much.

Hi,

The problem is that the javascript doing the redirect is invalid. There is also a PHP syntax error.

echo '<SCRIPT LANGUAGE="JavaScript"> javascript:document.forms[0].submit() </script>'

should be:

echo '<SCRIPT LANGUAGE="JavaScript"> javascript:document.forms[0].submit() </script>';

note the ; at the end.

It would probably be easier if you do not have a redirect to a new page, but thanked the user for adding their email right on the page used to add the email to the text file.

This can be achieved by the use of php "functions" that encapsulate a bit of PHP code that you can reuse.

eg: declare a function

function addEmailToTextFile($email) {
$data = "list.txt";
$open = fopen($data, "a");
fwrite($open, $email .  "\n" );
fclose($open);
}

This function can then be called using:

eg: call a function

addEmailToTextFile($_POST['email']);

This way you do not have to redirect to a new page just to do a new thing. It also makes your work easy.

If the concept of functions is a bit complicated, you could use if/else (conditional) statements if you know how to use those.

Example of what you want using functions:

So instead of using redirects to another PHP page. You can have something like:

// function declarations

/**
* Shows the Email list subscription form
*/
function showForm($email = '') {
	echo '
		<form name="mail_list" action="subscribe.php" method="post">
			<input type="text" name="email" value="'.$email.' />
			<input type="submit" value="Add Email To List" />
		</form>	
	';
}

/**
* Appends an email address to the File list.txt
*/
function addEmailList($email, $seperator = "\n") {
	$file_path = "list.txt";
	if ($resource_handle = fopen($file_path, "a")) {
		fwrite($resource_handle, $email .  $seperator );
		fclose($resource_handle);
	} else {
		echo 'Error: Could not Open list.txt';
	}
}

/**
* Validates an Email Address
*/
function validateEmailAddress($email) {
	if( (preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/', $email)) ||
(preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/', $email)) )
	{
		$host = explode('@', $email);
		if(checkdnsrr($host[1].'.', 'MX') ) return true;
		if(checkdnsrr($host[1].'.', 'A') ) return true;
		if(checkdnsrr($host[1].'.', 'CNAME') ) return true;
	}
	return false; 
}

// actual php  logic starts here

$email = $_POST['email'];

if ($email) {

	// validate the email
	if (!validateEmailAddress($email)) {
		echo '<div class="error">Please enter a valid email address.<div>';
		showForm($email); // show the subscription form again passing it the email address
	}

	// add the email to the list
	if (addEmailList($email, $seperator = "\n")) {
		echo '<div class="success">Thank you, Your Email has been added to the list.<div>';
	} else {
		echo '<div class="error">Sorry, We are experiencing technical difficulties.<div>';
		showForm($email); // show the subscription form again passing it the email address
	}
} else {
	// there is no email passed from HTTP, so this is a new user, show subscription form
	showForm(); 
}

notice that this will work on a single page (in the example i use subscription.php) and there is no need for redirects.

If you get how it goes, it is much simpler and easier to maintain than making your php code span more than one page for a simple project.

Note the code above is untested, its just an example.

if you want to use redirects, try using PHP to do the redirect, not javascript. JavaScript can be disabled on some browsers.

header('Location: redirect_page.php?getvar=value');
die;

The above sends a HTTP header back to the browser telling it that the location of the page has changed. This only allows you to use GET and not POST requests however.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.