Web Hosting Talk







View Full Version : PHP and radio buttons


leprakawn
04-24-2004, 03:25 AM
Okay, I've used HTML since '96, but I'm just now starting with PHP... obviously I've been too busy with school!

I have a form that asks for name, e-mail and provides two radio buttons (un/subscribe). I've tried the if statement, but I can't figure out what I'm doing wrong. Any help will be greatly appreciated!

Form:
<form action="newsletter.php" method="post" name="form">
<tr><td align=center colspan=2><font size=2><b>Free Newsletter</b></font></td></tr>
<tr><td>Name:</td><td><input type="text" name="name" size="24" maxlength="25" value=""></td></tr>
<tr><td>E-mail:</td><td><input type="text" name="email" size="24" maxlength="30" value=""></td></tr>

<tr><td colspan=2 align=center><input type="radio" name="choice" value="sub" checked>Subscribe <input type="radio" name="choice" value="unsub">Unsubscribe</td></tr>
<tr><td colspan=2 align=center><input type="submit" value="Submit"> </td></tr>
</form>

PHP:
<?
$name=$_POST['name'];
$email=$_POST['email'];
$to="leprakawn@hotmail.com";
$message="$name came to the site. E-mail: $email\n";
if ($choice == "sub") {$message = $message "Subscribed"; }
if ($choice == "unsub") {$message .= "Not Subscribed"; }

if(mail($to,"Newsletter Sub/Unsub",$message,"From: $email\n")) {
echo "Thank you for your interest. We will update our list depending on your choice.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
?>

ricocheting
04-26-2004, 06:59 PM
you probably want to use:
if ($_POST['choice'] == "sub") {$message .= "Subscribed"; }
if ($_POST['choice'] == "unsub") {$message .= "Not Subscribed"; }

kkbbzzaa
04-26-2004, 11:31 PM
Just declare the variable at the top as you did with $name and $email

$choice = $_POST['choice'];

Just with this your code should work fine.

leprakawn
04-26-2004, 11:32 PM
I tried it, but that didn't work. :(
I also changed it to:
if ($choice == "sub") {$message .= "Subscribed"; }
if ($choice == "unsub") {$message .= "Not Subscribed"; }
and that doesn't work either.

Thanks though. If anyone else can figure this out, I'll be very, very happy!

leprakawn
04-26-2004, 11:39 PM
I also did the second suggestion:

$name=$_POST['name'];
$email=$_POST['email'];
$to="leprakawn@hotmail.com";
$message="$name came to the site. E-mail: $email\n";
$choice = $_POST['choice'];

No luck there either. :confused:

kkbbzzaa
04-27-2004, 12:03 AM
<table>
<form action="newsletter.php" method="post" name="form">
<tr>
<td align=center colspan=2><font size=2><b>Free Newsletter</b></font></td>
</tr>
<tr>
<td>Name:</td><td><input type="text" name="name" size="24" maxlength="25" value=""></td>
</tr>
<tr>
<td>E-mail:</td><td><input type="text" name="email" size="24" maxlength="30" value=""></td>
</tr>
<tr>
<td colspan=2 align=center><input type="radio" name="choice" value="sub" checked>Subscribe <input type="radio" name="choice" value="unsub">Unsubscribe</td>
</tr>
<tr>
<td colspan=2 align=center><input type="submit" value="Submit"></td>
</tr>
</form>
</table>

<?
$name=$_POST['name'];
$email=$_POST['email'];
$choice=$_POST['choice'];
$to="leprakawn@hotmail.com";

$message="$name came to the site. E-mail: $email\n";

if ($choice == "sub") {$message = $message."Subscribed"; }
if ($choice == "unsub") {$message = "Not Subscribed"; }

if ($email) {
if(mail($to,"Newsletter Sub/Unsub",$message,"From: $email\n")) {
echo "Thank you for your interest. We will update our list depending on your choice.";
}
else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
}
?>

leprakawn
04-27-2004, 12:12 AM
SWEET!!!!