PDA

View Full Version : radio buttons - php - to database


Tjobbe
December 1st, 2004, 18:31
I'm adding a registration script for this members login area and I want to give the user an option to opt-in or out of recieving further contact upon sign up.
I know how to add the buttons, thats just simple html, but I do not know how to make it write a simple yes or no to the database so that when I find the details I can see who wants further contact or not.

I thought it might be easier that if NO was selected i could make a "simple"
if $no == true, then do not write emailaddy to db

but I got stuck on that too!

heres what I have:


<? session_start() ?>
<HTML>
<HEAD>
<TITLE>Register</TITLE>
</HEAD>

<BODY>
<H2>Register</H2>
<?
// check to see if these two variables have been passed from
// the HTML form at the foot of this script (which calls this page again)
if ($user && $pass) {

// connect to database and select the 'userlist' database
$db = mysql_connect('localhost', 'nowdes1a_login', 'login');
mysql_select_db("nowdes1a_userlist", $db);

// check to see if username is already on the database
$result = mysql_query ("SELECT * FROM users WHERE name = '".$user."'");
if (mysql_num_rows($result) == 0) {

// if it's not, add the new details to the database
$result = mysql_query ("INSERT INTO users (name, password, firstname, lastname, email, tel, address1, address2, pcode, county) VALUES
('".$user."', PASSWORD('".$pass."'),'".$name1."','".$name2."','".$emailaddy."','".$phonenum."','".$add1."','".$add2."','".$pcode1."','".$county1."')");
if ($result) {

// if successful, log in the user and show a welcome message
// then exit the script
$logged_in_user = $user;
session_register("logged_in_user");
echo "Your details have been added to the database, ".$user."<BR><BR>";
echo "<A HREF='main.php'>Click here to proceed to the main page.</A><BR><BR>";
echo "<A HREF='logout.php'>Click here to log out.</A>";
exit;
} else {

// if the details could not be added for some reason,
// show an error message
echo "Sorry, there has been a technical hitch. We cannot enter your details.";
exit;
}
} else {

// if the details were already on the database, let the user try again
echo "Sorry, that username has been taken. Please try another.<BR>";
}
} else if ($user || $pass) {

// if the user has filled in one field but not the other,
// throw up this error
echo "Please fill in all fields.";
}
// show HTML form as follows
?>
<FORM METHOD=POST ACTION="register.php">
<p>Please enter a username:
<INPUT NAME="user" TYPE=TEXT MAXLENGTH=20 SIZE=20>
<BR>
Please enter a password:
<INPUT NAME="pass" TYPE=PASSWORD MAXLENGTH=10 SIZE=10>
</p>
<p>
First Name:
<input name="name1" type="text" id="name1" size="20">
<br>
Last Name:
<input name="name2" type="text" id="name2" size="20">
<br>
Email address:
<input name="emailaddy" type="text" id="emailaddy" size="20">
<br>
Telephone number (including STD):
<input name="phonenum" type="text" id="phonenum2" size="20">
<br>
Address 1:
<input name="add1" type="text" id="add1" size="20">
<br>
Address 2:
<input name="add2" type="text" id="add2" size="20">
<br>
Postcode
<input name="pcode1" type="text" id="pcode1" size="20">
<br>
County:
<input name="county1" type="text" id="county1" size="20">
<BR>
<INPUT TYPE=SUBMIT VALUE="Register">
</p>
</FORM>
</BODY>
</HTML>



any help appreciated, thanks.

sonicgroup
December 1st, 2004, 19:13
Personally, what I would do, rather than adding or not the email address, is add another field to your DB (like "noContact"), and adding a checkbox to the form. The checkbox's value is 1 and it's checked by default (i.e. they want further contact). Then, when the form is submitted, you can just check the checkbox:

[syntax:53b331bbfd="php"]
// assume checkbox's name is cbox

// if the checkbox is unchecked, it will not get submitted
// (and is therefore empty), but we also make sure that it's
// not 1 (since 1 indicates true) in case some glitch causes a
// different value to be submitted

if (empty($_POST['cbox']) || $_POST['cbox'] != '1') {
$contact = false;
} else {
$contact = true;
}
[/syntax:53b331bbfd]