sammen smeltning af 2 mailinglists plus lidt AJAX
Hej Eksperter :-)Jeg ville høre om en herinde kunne hjælpe mig med at "smelte" 2 php nyhedsbrevs scripts sammen, da jeg ikke er den store haj til php.
Det ene fungerer med et mindre admin modul nemlig, og den anden er der AJAX på formen, og det er AJAX funktionerne jeg vil have med over i førstenævnte script :-)
index.php scriptet med AJAX:
<?php
/*
Part of the code for the article "Use Ajax and PHP to Build Your Mailing List"
by Aarron Walter (aarron@aarronwalter.com)
http://www.sitepoint.com/article/use-ajax-php-build-mailing-list
*/
require_once("inc/storeAddress.php");
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Painless AJAX and PHP Mailing List Sign Up</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/mailingList.js"></script>
</head>
<body>
<form id="addressForm" action="index.php" method="get">
<fieldset>
<legend>Join our mailing list!</legend>
<p>
<input type="text" name="address" id="address" />
<input type="submit" value="Sign Up" />
</p>
<p id="response"><?php echo(storeAddress()); ?></p>
</fieldset>
</form>
</body>
</html>
inc/storeAddress.php scriptet med AJAX:
<?
require_once("dbConstants.php");
function storeAddress() {
$message = " ";
// Check for an email address in the query string
if( !isset($_GET['address']) ){
// No email address provided
}
else {
// Get email address from the query string
$address = $_GET['address'];
// Validate Address
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $address)) {
$message = "<strong>Error</strong>: An invalid email address was provided.";
}
else {
// Connect to database
$con = mysql_connect(DBHOST ,DBUSER, DBPASS);
mysql_select_db(DBNAME, $con);
// Insert email address into mailinglist table
$result = mysql_query("INSERT INTO mailinglist SET email='" . $address . "'");
if(mysql_error()){
$message = "<strong>Error</strong>: There was an error storing your email address.";
}
else {
$message = "Thanks for signing up!";
}
}
}
return $message;
}
?>
Det er de 2 overstående scripts med ajax jeg vil have smeltet sammen i dette register.php:
<?php
include("connect.php");
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!preg_match('/([a-zA-z0-9\.\-]+)@([a-zA-Z0-9\.\-]+)\.([a-zA-Z]{2,3})/',$_POST['email'],$m))
die('Invalid Email Address.');
$name = htmlentities($_POST['name']);
if (empty($name))
die("Please fill in out the whole form.");
$email = $m[0];
$link = "SELECT * FROM brugere WHERE email='$email'";
$res = mysql_query($link) or die(mysql_error());
if (mysql_num_rows($res) > 0)
die("This email address is already registered.");
$link = "INSERT INTO brugere VALUES ('$name','$email',NOW(),'subscribed','0','')";
$res= mysql_query($link) or die(mysql_error());
if ($res)
die("Email Succesfully Registerted.");
}
else
{
echo '<html><head><title>Sign Up</title></head>
<body>
<form action="" method="POST">
Name <input type="text" name="name"><br />
Email <input type="text" name="email"><br /><br />
<input type="submit" value="Sign up">
</form>
</body>
</html>';
}
?>
Mvh. Jimmy Rittenborg