PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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:
<?
$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?
Last edited by cupaball on Sun Jul 30, 2006 7:36 pm, edited 1 time in total.
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.
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?
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.
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!!!
<?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();
?>
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"
<?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();
?>
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.
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.
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.
<?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();
?>