Page 1 of 2
Another Form Question - Add/Remove an E-mail
Posted: Sun Jul 30, 2006 2:12 pm
by cupaball
I have seen this done serveral times. A join our mailing list with the option to add/remove. I have read a few tutorials but I still don't see to understand. Here is what I have so far:
maillist.html
Code: Select all
<html>
<<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<style type="text/css">
<!--
.style3 { color: #FFFFFF;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
}
-->
</style>
</head>
<body>
<table width="183" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="middle" bgcolor="#C07502" class="style3">Join Our Mailing List </td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><form action="email2.php" method="post" name="mailform" id="mailform">
<table width="183" border="0" cellspacing="0" cellpadding="5">
<tr>
<td colspan="2" align="left" valign="top"><label>Email:
<input name="email" type="text" id="email" size="15">
</label></td>
</tr>
<tr>
<td width="77" height="28" align="left" valign="top"><label>
<input name="Select" type="radio" value="add" checked>
Add</label>
<label></label></td>
<td width="86" align="left" valign="top"><input name="select" type="radio" value="remove">
Remove</td>
</tr>
<tr>
<td colspan="2" align="left" valign="top"><div align="center">
<input type="submit" name="Submit" value="Submit">
</div></td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>
and here is the php
email2.php
Code: Select all
<?
$username="123";
$password="123";
$database="123";
$email=$_POST['email'];
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO contact VALUES ('','n/a','n/a','$email','n/a','n/a','n/a','n/a','n/a')";
mysql_query($query);
mysql_close();
$myredirect = "http://www.ddmconsultinggroup.com/thanks.html";
header("Location:$myredirect");
?>
Can someone help me or point me in the direction for the script to say that if the add radio button is selected the add and if the remove radio button is selected then remove?
Posted: Sun Jul 30, 2006 3:16 pm
by RobertGonzalez
Radio buttons are sent as arrays (like select multiples), so you need to treat them as such when processing. It might be easier for you to use a select list instead, and have the user choose either Add or Delete from the select list. That is always sent as a string var of the POST array.
But in your case, your HTML will actually allow both Add and Delete since your radio buttons have different names. It would be feasible to check both and send them, which pretty much destroys any logic you could use to decide what to do.
Posted: Sun Jul 30, 2006 7:31 pm
by cupaball
Everah wrote:Radio buttons are sent as arrays (like select multiples), so you need to treat them as such when processing. It might be easier for you to use a select list instead, and have the user choose either Add or Delete from the select list. That is always sent as a string var of the POST array.
But in your case, your HTML will actually allow both Add and Delete since your radio buttons have different names. It would be feasible to check both and send them, which pretty much destroys any logic you could use to decide what to do.
Thanks, if I change it to a drop down, would yuo be able to help me code the php?
Posted: Sun Jul 30, 2006 11:41 pm
by RobertGonzalez
If it is a drop down it will pass like a text field ($_POST['dropdownfieldname']). But post what you got done and I, or someone around here, can help you if you get stuck.
Posted: Mon Jul 31, 2006 9:18 am
by cupaball
Okay I took a stab at changing the php to have an if statement which say if radio button select equal add then add it and if radio button equals remove then delete it?
I am sure this is wrong, can someone look and help!!!
Code: Select all
<?
$username="123";
$password="123";
$database="123";
$email=$_POST['email'];
$addremove=$_POST['select'];
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
if ($addremove="add")
{
$query = "INSERT INTO contact VALUES
('','NULL','NULL','$email','NULL','NULL','NULL','NULL','NULL')";
mysql_query($query);
mysql_close();
$myredirect = "http://www.ddmconsultinggroup.com/thanks.html";
header("Location:$myredirect");
}
if ($addremove="remove")
{
$query = "DELETE FROM contact WHERE email="$email"
mysql_query($query);
mysql_close();
$myredirect = "http://www.ddmconsultinggroup.com/thanks.html";
header("Location:$myredirect");
}
?>
Posted: Mon Jul 31, 2006 9:35 am
by RobertGonzalez
Not bad first timer... try this and see if works.
PS Thanks for trying something first. It makes me feel a lot better about throwing code out there when someone tries something first.
Code: Select all
<?php
$username="123";
$password="123";
$database="123";
$email=$_POST['email']; // You should validate this to prevent SQL injection attacks and such
$addremove=$_POST['select'];
if (!$cnx = mysql_connect("localhost",$username,$password))
{
die('Could not connect to the database server: ' . mysql_error());
}
if (!mysql_select_db($database))
{
die('Unable to select the ' . $database . ' database: ' . mysql_error());
}
if ($addremove == 'add') // use double equal as single equal will set the var value
{
$query = "INSERT INTO contact VALUES
('','NULL','NULL','$email','NULL','NULL','NULL','NULL','NULL')";
if (!$result = mysql_query($query))
{
die('Could not execute the query<br />' . $query . '<br />because: ' . mysql_error());
}
header("Location: http://www.ddmconsultinggroup.com/thanks.html");
exit;
}
elseif ($addremove == 'remove') // again use double equal
{
$query = "DELETE FROM contact WHERE email='$email'";
if (!$result = mysql_query($query))
{
die('Could not delete the email address <strong>' . $email . '</strong> because: ' . mysql_error());
}
header("Location: http://www.ddmconsultinggroup.com/thanks.html");
exit;
}
mysql_close();
?>
Posted: Mon Jul 31, 2006 11:33 am
by cupaball
thanks, I will give it a try tonight.
In English does the script say
1. if unable to connect and uable to "select" the database then error
2. then process, if radio button "select" equals add then add , if unable to process then say "'Could not execute the query and error"
3. then else if radio equals remove then remove or say "Could not delete the email address because and error"
Thanks for your help.
Posted: Mon Jul 31, 2006 3:14 pm
by cupaball
Can I validate the e-mail like this?
Code: Select all
<?php
$username="123";
$password="123";
$database="123";
if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-
]+)*\.([a-zA-Z]{2,6})$/", $email=$_POST['email'] )) {
die("Invalid e-mail address");
echo "Valid e-mail address, processing..."; // is it okay to have this echo or should it just continue porcessing
// You should validate this to prevent SQL injection attacks and such
$addremove=$_POST['select'];
if (!$cnx = mysql_connect("localhost",$username,$password))
{
die('Could not connect to the database server: ' . mysql_error());
}
if (!mysql_select_db($database))
{
die('Unable to select the ' . $database . ' database: ' . mysql_error());
}
if ($addremove == 'add') // use double equal as single equal will set the var value
{
$query = "INSERT INTO contact VALUES
('','NULL','NULL','$email','NULL','NULL','NULL','NULL','NULL')";
if (!$result = mysql_query($query))
{
die('Could not execute the query<br />' . $query . '<br />because: ' . mysql_error());
}
header("Location: http://www.ddmconsultinggroup.com/thanks.html");
exit;
}
elseif ($addremove == 'remove') // again use double equal
{
$query = "DELETE FROM contact WHERE email='$email'";
if (!$result = mysql_query($query))
{
die('Could not delete the email address <strong>' . $email . '</strong> because: ' . mysql_error());
}
header("Location: http://www.ddmconsultinggroup.com/thanks.html");
exit;
}
mysql_close();
?>
Posted: Mon Jul 31, 2006 9:07 pm
by RobertGonzalez
You're getting there. Run it. you are going to get an error, but run it and see if you can find the error.
Posted: Mon Jul 31, 2006 9:29 pm
by cupaball
Where have you been!!!!
Okay, you were right I did get an error. I think I fixed it. Below is what I have so far, the only issue is the validation will allow more the one email to be submitted.
Code: Select all
<?php
$username="123";
$password="123";
$database="123";
if (!(preg_match("/^.{2,}?@.{2,}\./", $_POST['email']))) {
echo '<font size="+1" color="#FF0000">Error: Invalid E-mail</font><BR>';
echo 'The e-mail address you entered (<i>'.$_POST['email'].'</i>) is invalid. Press back to re-enter you email address';
die();
}
$email=$_POST['email'];
$addremove=$_POST['select'];
if (!$cnx = mysql_connect("localhost",$username,$password))
{
die('Could not connect to the database server: ' . mysql_error());
}
if (!mysql_select_db($database))
{
die('Unable to select the ' . $database . ' database: ' . mysql_error());
}
if ($addremove == 'add')
{
$query = "INSERT INTO contact VALUES
('','NULL','NULL','$email','NULL','NULL','NULL','NULL','NULL')";
if (!$result = mysql_query($query))
{
die('Could not execute the query<br />' . $query . '<br />because: ' . mysql_error());
}
header("Location: http://www.ddmconsultinggroup.com/thanks.html");
exit;
}
elseif ($addremove == 'remove') // again use double equal
{
$query = "DELETE FROM contact WHERE email='$email'";
if (!$result = mysql_query($query))
{
die('Could not delete the email address <strong>' . $email . '</strong> because: ' . mysql_error());
}
header("Location: http://www.ddmconsultinggroup.com/thanks.html");
exit;
}
mysql_close();
?>
Posted: Mon Jul 31, 2006 9:56 pm
by RobertGonzalez
Look into the
substr_count() function for seeing if you can count the number of at signs in the email address that is passed. You got me excited for you, so keep pumping this out. I want to see it working.
EDIT | Sorry, I used the wrong function reference.
Posted: Tue Aug 01, 2006 10:30 am
by cupaball
Everah wrote:Look into the
substr_count() function for seeing if you can count the number of at signs in the email address that is passed. You got me excited for you, so keep pumping this out. I want to see it working.
EDIT | Sorry, I used the wrong function reference.
Man this is a tough one, I'm reading up...
Posted: Tue Aug 01, 2006 10:47 am
by cupaball
I think I got it!
Code: Select all
<?php
$username="123";
$password="123";
$database="123";
if (!(preg_match("/^.{2,}?@.{2,}\./", $_POST['email']))) {
echo '<font size="+1" color="#FF0000">Error: Invalid
E-mail</font><BR>';
echo 'The e-mail address you entered (<i>'.$_POST['email'].'</i>) is
invalid. Press back to re-enter you email address';
die();
}
if (substr_count($_POST['email'], '@') > "1")
{
echo '<font size="+1" color="#FF0000">Error: More than one e-mail address</font><BR>';
echo '(<i>'.$_POST['email'].'</i>) contains more than one e-mail
address. Press back to re-enter you email address';
die();
}
$email=$_POST['email'];
$addremove=$_POST['select'];
if (!$cnx = mysql_connect("localhost",$username,$password))
{
die('Could not connect to the database server: ' . mysql_error());
}
if (!mysql_select_db($database))
{
die('Unable to select the ' . $database . ' database: ' . mysql_error());
}
if ($addremove == 'add')
{
$query = "INSERT INTO contact VALUES
('','NULL','NULL','$email','NULL','NULL','NULL','NULL','NULL')";
if (!$result = mysql_query($query))
{
die('Could not execute the query<br />' . $query . '<br />because: ' .
mysql_error());
}
header("Location: http://www.ddmconsultinggroup.com/thanks.html");
exit;
}
elseif ($addremove == 'remove') // again use double equal
{
$query = "DELETE FROM contact WHERE email='$email'";
if (!$result = mysql_query($query))
{
die('Could not delete the email address <strong>' . $email . '</strong>
because: ' . mysql_error());
}
header("Location: http://www.ddmconsultinggroup.com/thanks.html");
exit;
}
mysql_close();
?>
Posted: Wed Aug 02, 2006 9:00 pm
by RobertGonzalez
So, did it work?
Posted: Mon Aug 07, 2006 10:32 am
by cupaball
Everah wrote:So, did it work?
it worked perfectly