...

View Full Version : Registration Form Drop Down List Dilemma!



KiwiChic
10-09-2012, 11:26 AM
Hi Folks,

I'm a bit of a newbie to web devl and am learning how to code. I have taken the coding of a simple registration form and modified it to collect the added information I need. The whole thing is working perfectly until I bring in a couple of drop down lists. More perplexing is that if I leave the start of an orphaned <option tag at the end of each list it works great. The only draw back to this, is I then have a blank space among my options that is able to be selected and returns zero input to my database. I'm sure it's something very simple but with my limited experience, the solution has eluded me. I have searched and searched numerous coding forums and can not come up with any clues. The code (also attached in txt format) is as follows:


<?php

// Check if he wants to register:
if (!empty($_POST["username"]))
{
// Check if passwords match.
if ($_POST["password"] != $_POST["password2"])
exit("Error - Passwords don't match. Please go back and try again.");

// Assign some variables.
$date = mktime('d' - 'm' - 'Y');
$ip = $_SERVER['REMOTE_ADDR'];

require_once("connect.php");

// Register him.
$query = mysql_query("INSERT INTO members
(username, firstname, lastname, dateofbirth, occupation, livesin, postcode, income, password, date, ip)
VALUES ('$_POST[username]','$_POST[firstname]','$_POST[lastname]','$_POST[dateofbirth]','$_POST[occupation]','$_POST[livesin]','$_POST[postcode]','$_POST[income]','$_POST[password]','$date','$ip')")
or die ("Error - Couldn't register user.");

echo "Welcome $_POST[username]! You've been successfully registered as a Kiwi Chic!<br /><br />
Please login <a href='login.php'><b>here</b></a>.";
exit();
}

?>


<html>
<head>
<title>Register</title>
</head>
<body>
<form action="register.php" method="post">
<table width="75%" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
<td width="100%"><h3>Registration</h3></td>
</tr>
<tr>
<td width="100%"><label>Create Username: <input type="text" name="username" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>First Name: <input type="text" name="firstname" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>Last Name: <input type="text" name="lastname" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>Date of Birth: <input type="text" name="dateofbirth" size="15" value="dd/mm/yyyy"></label></td>
</tr>
<tr>
<td width="100%"><label>Occupation: <form action="">
<select name="occupation">
<option value="student">Student</option>
<option value="parttimeempl">Work Part-time</option>
<option value="fulltimeempl">Work Full-time</option>
<option value="domduties">Domestic Goddess</option>
<option value="umempl">Unemployed</option>
<option </select>
</form></label></td>
</tr>
<tr>
<td width="100%"><label>Lives In: <input type="text" name="livesin" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>Postcode: <input type="text" name="postcode" size="15" value=" "></label></td>
</tr>
<tr>
<td width="100%"><label>Income: <form action="">
<select name="income">
<option value="<40000">Less than $40,000</option>
<option value="41000 to 80000">$41,000 to $80,000</option>
<option value="81000 to 120000">$81,000 to $120,000</option>
<option value=">120000">More than $120,000</option>
<option </select>
</form></label></td>
</tr>
<tr>
<td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>Verify Password: <input type="password" name="password2" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><input type="submit" value="Register!"></td>
</tr>
</table>
</form>
</body>
</html>

Thanking you in advance!

KiwiChic

Redcoder
10-09-2012, 05:08 PM
Hi Folks,


<select name="occupation">
<option value="student">Student</option>
<option value="parttimeempl">Work Part-time</option>
<option value="fulltimeempl">Work Full-time</option>
<option value="domduties">Domestic Goddess</option>
<option value="umempl">Unemployed</option>
<option </select> //Why have you included this unclosed empty <option tag?...it shouldn't be there
</form></label></td>
</tr>
<tr>
<td width="100%"><label>Lives In: <input type="text" name="livesin" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>Postcode: <input type="text" name="postcode" size="15" value=" "></label></td>
</tr>
<tr>
<td width="100%"><label>Income: <form action="">
<select name="income">
<option value="<40000">Less than $40,000</option>
<option value="41000 to 80000">$41,000 to $80,000</option>
<option value="81000 to 120000">$81,000 to $120,000</option>
<option value=">120000">More than $120,000</option>
<option </select> //Why have you included this unclosed empty <option tag?...it shouldn't be there
</form></label></td>
</tr>
<tr>
<td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><label>Verify Password: <input type="password" name="password2" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><input type="submit" value="Register!"></td>
</tr>
</table>
</form>
</body>
</html>

Thanking you in advance!

KiwiChic

Check out the comments I've added in the code. You seem to have an unclosed option tag

sunfighter
10-09-2012, 06:19 PM
Removed the form tags around the dropdowns also removed the extra <option that wasn't closed or had an end tag. Removed the width=100% from the <td>'s because they don't belong there. Separated row content into two td's. (if your gonna use a table, use it.) Added a css section. Used the &#60; and &#62; to replace the < and > in the select values because they some times are read as ending the tag. Redid the register.php file. This is not completely correct but will do for now. You will need to check the user inputs and evaluate them before adding them to your database.
The is the HTML file:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Register</title>
<style type="text/css">
td.even{
width: 120px;
text-align:right;
}
</style>
</head>
<body>
<form action="register.php" method="post">
<table border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
<td colspan="2"><h3><u>Registration</u></h3></td>
</tr>
<tr>
<td class="even"><label>Create Username:</label></td><td> <input type="text" name="username" size="25" value=""></td>
</tr>
<tr>
<td class="even"><label>First Name:</label></td><td> <input type="text" name="firstname" size="25" value=""></td>
</tr>
<tr>
<td class="even"><label>Last Name:</label></td><td> <input type="text" name="lastname" size="25" value=""></td>
</tr>
<tr>
<td class="even"><label>Date of Birth:</label></td><td> <input type="text" name="dateofbirth" size="15" value="dd/mm/yyyy"></td>
</tr>
<tr>
<td class="even"><label>Occupation:</label></td><td>
<select name="occupation">
<option value="student">Student</option>
<option value="parttimeempl">Work Part-time</option>
<option value="fulltimeempl">Work Full-time</option>
<option value="domduties">Domestic Goddess</option>
<option value="umempl">Unemployed</option>
</select>
</td>
</tr>
<tr>
<td class="even"><label>Lives In:</label></td><td> <input type="text" name="livesin" size="25" value=""></td>
</tr>
<tr>
<td class="even"><label>Postcode:</label></td><td> <input type="text" name="postcode" size="15" value=" "></td>
</tr>
<tr>
<td class="even"><label>Income:</label></td><td>
<select name="income">
<option value="&#60;40000" >Less than $40,000</option>
<option value="41000 to 80000">$41,000 to $80,000</option>
<option value="81000 to 120000">$81,000 to $120,000</option>
<option value="&#62;120000">More than $120,000</option>
</select>
</td>
</tr>
<tr>
<td class="even"><label>Password:</label></td><td> <input type="password" name="password" size="25" value=""></td>
</tr>
<tr>
<td class="even"><label>Verify Password:</label></td><td> <input type="password" name="password2" size="25" value=""></td>
</tr>
<tr>
<td class="even"><input type="submit" value="Register!"></td>
</tr>
</table>
</form>
</body>
</html>


And the register.php file:


<?php
$username = $_POST["username"];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$dateofbirth = $_POST['dateofbirth'];
$occupation = $_POST['occupation'];
$livesin = $_POST['livesin'];
$postcode = $_POST['postcode'];
$income = $_POST['income'];
$password = $_POST['password'];
$password2 = $_POST['password2'];


// Check if he wants to register:
if (!empty($_POST["username"]))
{
// Check if passwords match.
if ($_POST["password"] != $_POST["password2"])
exit("Error - Passwords don't match. Please go back and try again.");

// Assign some variables.
$date = mktime('d' - 'm' - 'Y');
$ip = $_SERVER['REMOTE_ADDR'];

//require_once("connect.php");

// Register him.
$query = "INSERT INTO members
(username, firstname, lastname, dateofbirth, occupation, livesin, postcode, income, password, date, ip)
VALUES
('$username','$firstname','$lastname','$dateofbirth','$occupation','$livesin','$postcode','$income', '$password',$date,$ip)";
//$results = mysql_query($query);
echo $query.'<br />';
echo "Welcome ".$username."! You've been successfully registered as a Kiwi Chic!<br /><br />
Please login <a href='login.php'><b>here</b></a>.";
exit();
}else{
echo 'You do not have a username entered.';
}

?>
I echo out the query instead of adding to a database and commented out the require_once.

KiwiChic
10-12-2012, 01:14 AM
Hi Sunfighter,

Many thanks for taking to time to review my code. Your revisions are an approach I had also tried myself, the result has been the same. When I open the page I get the following notices:

Notice: Undefined index: username in C:\xampp\htdocs\KiwiChic\KiwiChic_Test_Copy\includes\register.php on line 3

Notice: Undefined index: firstname in C:\xampp\htdocs\KiwiChic\KiwiChic_Test_Copy\includes\register.php on line 4

Notice: Undefined index: lastname in C:\xampp\htdocs\KiwiChic\KiwiChic_Test_Copy\includes\register.php on line 5

Notice: Undefined index: dateofbirth in C:\xampp\htdocs\KiwiChic\KiwiChic_Test_Copy\includes\register.php on line 6

Notice: Undefined index: occupation in C:\xampp\htdocs\KiwiChic\KiwiChic_Test_Copy\includes\register.php on line 7

Notice: Undefined index: livesin in C:\xampp\htdocs\KiwiChic\KiwiChic_Test_Copy\includes\register.php on line 8

Notice: Undefined index: postcode in C:\xampp\htdocs\KiwiChic\KiwiChic_Test_Copy\includes\register.php on line 9

Notice: Undefined index: income in C:\xampp\htdocs\KiwiChic\KiwiChic_Test_Copy\includes\register.php on line 10

Notice: Undefined index: password in C:\xampp\htdocs\KiwiChic\KiwiChic_Test_Copy\includes\register.php on line 11

Notice: Undefined index: password2 in C:\xampp\htdocs\KiwiChic\KiwiChic_Test_Copy\includes\register.php on line 12
You do not have a username entered.

The final note 'You do not have a username entered' I have not seen before. I might have mentioned earlier, I have been using XAMPP to develop my site before going live. Undefined indices are an issue I am finding with a couple of other scripts. Do you expect the scenario may be different once my website goes live (or maybe I'm well off the track)?

Again many thanks for your help, very much appreciated.

sunfighter
10-12-2012, 02:10 PM
You can not run the register.php file by it's self. It is called from the main file that has the form that will define the first 12 items in the php file that I also gave you.
final note 'You do not have a username entered' I have not seen before. Correct, I added that. You should keep your users informed. If everything stops without an explanation they will think things are broke.

And again I warn:
I echo out the query instead of adding to a database and commented out the require_once.
That's these lines
//$results = mysql_query($query);
echo $query.'<br />';and
//require_once("connect.php");
They have to be set right before the script works correctly.

So, run the other file, fill in the form, and things should work. Remove the comments and your database will be changed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum