How Do I Make Form Fields Compulsory?
Posted: Sun Jul 11, 2004 7:38 am
Hey there everyone... my problem is probably fairly simple for anyone with a decent understanding of PHP but I'm pretty much an amateur in that respect.
Anyway, what I'm doing is creating a user database where visitors are able to enter their details which will be stored in a MySQL database.
I have no problem creating the form which submits the information, but I want some fields to be compulsory, and others not. To achieve this, I want the page to refresh (or use DHTML...) to point out to the user that a particular field is not filled in, or is invalid. (eg "You have not filled in blah blah field"). Only after all the fields are filled in should their submission be accepted. Anyway without further adieu, here is the code I have:
I would really appreciate any help. Related to this same database, I want users to be able to give themselves a username and password and be automatically assigned a 5 character alphanumerial random membership ID but I'm unsure how to go about it. But for now I just need to get the first part working... I hope this isn't asking too much. 
Anyway, what I'm doing is creating a user database where visitors are able to enter their details which will be stored in a MySQL database.
I have no problem creating the form which submits the information, but I want some fields to be compulsory, and others not. To achieve this, I want the page to refresh (or use DHTML...) to point out to the user that a particular field is not filled in, or is invalid. (eg "You have not filled in blah blah field"). Only after all the fields are filled in should their submission be accepted. Anyway without further adieu, here is the code I have:
Code: Select all
<form enctype="multipart/form-data" action="street_team.php" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr valign="top">
<td width="34%">First Name:</td>
<td width="66%"><input class="input" name="first_name" type="text" size="25"></td>
</tr>
<tr valign="top">
<td>Last Name:</td>
<td><input class="input" name="last_name" type="text" size="25"></td>
</tr>
<tr valign="top">
<td>Birth Year:</td>
<td><input class="input" name="birth_year" type="text" size="4"></td>
</tr>
<tr valign="top">
<td>Email Address:</td>
<td><input class="input" name="email" type="text" size="35"></td>
</tr>
<tr valign="top">
<td>Postal Address:</td>
<td><input class="input" name="address" type="text" size="35"></td>
</tr>
<tr valign="top">
<td>City:</td>
<td><input class="input" name="city" type="text" size="20"></td>
</tr>
<tr valign="top">
<td>State / Province:</td>
<td><input class="input" name="state_province" type="text" size="20"></td>
</tr>
<tr valign="top">
<td>Zip / Post Code: </td>
<td><input class="input" name="zip" type="text" size="8"></td>
</tr>
<tr valign="top">
<td>Country:</td>
<td><select size="1" name="country" class="input">
<option selected>Other</option>
<option>Argentina</option>
<option>Australia</option>
<option>Belgium</option>
<option>Brazil</option>
<option>Canada</option>
<option>Chile</option>
<option>China</option>
<option>Colombia</option>
<option>Croatia</option>
<option>Czech Republic</option>
<option>Denmark</option>
<option>Ecuador</option>
<option>England</option>
<option>Finland</option>
<option>France</option>
<option>Germany</option>
<option>Great Britain</option>
<option>Hungary</option>
<option>Ireland</option>
<option>Italy</option>
<option>Japan</option>
<option>Mexico</option>
<option>N Ireland</option>
<option>Netherlands</option>
<option>New Zealand</option>
<option>Norway</option>
<option>Portugal</option>
<option>Russia</option>
<option>Scotland</option>
<option>South Africa</option>
<option>Spain</option>
<option>Sweden</option>
<option>Switzerland</option>
<option>Uruguay</option>
<option>USA</option>
<option>Wales</option>
</select></td>
</tr>
<tr valign="top">
<td>Continent</td>
<td><input class="input" name="continent" type="text" size="20"></td>
</tr>
<tr valign="top">
<td>Phone Number:</td>
<td><input class="input" name="phone" type="text" size="20"></td>
</tr>
<tr valign="top">
<td>Nearest Major City:</td>
<td><input class="input" name="major_city" type="text" size="20"></td>
</tr>
<tr valign="top">
<td>Favorite Venues/Hangouts:</td>
<td><textarea rows="5" name="faves" cols="30" class="input"></textarea></td>
</tr>
<tr valign="top">
<td>Comments:</td>
<td><textarea rows="5" name="comments" cols="30" class="input"></textarea></td>
</tr>
<tr valign="top">
<td colspan="2" align="center"><input type="submit" value="Join The Team!" name="street_team_data">
</td>
</tr>
</table>
</form>Code: Select all
<?PHP
$database_name = "my_database";
$dbh = mysql_connect("localhost","my_username","my_password");
if (!mysql_select_db($database_name)) {
echo "Unable to select "$database_name" database";
}
$first_name = $_POST['first_name'];
$date=date("Y-m-d");
$last_name = $_POST['last_name'];
$birth_year = $_POST['birth_year'];
$email = $_POST['email'];
$address = $_POST['address'];
$city = $_POST['city'];
$state_province = $_POST['state_province'];
$country = $_POST['country'];
$zip = $_POST['zip'];
$continent = $_POST['continent'];
$phone = $_POST['phone'];
$major_city = $_POST['major_city'];
$faves = $_POST['faves'];
$comments = $_POST['comments'];
if ($_POST['street_team_data']){
// log user ip address and check bans
$banned = false;
$banlist[] = '';
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else
$ip = $_SERVER["REMOTE_ADDR"];
$octets1 = explode(".", $ip);
foreach ($banlist as $val){
$octets2 = explode(".", $val);
if ( ($octets1[0] == $octets2[0] || $octets2[0] == "*") && ($octets1[1] == $octets2[1] || $octets2[1] == "*") &&
($octets1[2] == $octets2[2] || $octets2[2] == "*") && ($octets1[3] == $octets2[3] || $octets2[3] == "*")){
$banned = true;
break;
}
}
$sql = "insert into street_team (date, first_name, last_name, birth_year, email, address, city, state_province, country, zip, continent, phone, major_city, faves, comments, ip)
values '$date', '$first_name', '$last_name', '$birth_year', '$email', '$address', '$city', '$state_province', '$country', '$zip', '$continent', '$phone', '$major_city', '$faves', '$comments', '$ip')";
if (!$banned)
{
$res = mysql_query($sql,$dbh);
if (!$res) {
echo mysql_errno().": ".mysql_error ()."";
return 0;
}
}
echo"Submission was successful!";
}
?>