Jump to content

Recommended Posts

Hi, I'm making a registration form, and I need help with the form fields. How can I make fields OPTIONAL, and how can I make it so users HAVE to fill in a certain field in order to register? Here's my current code:

 

 

register.php

<html>
<head>
<title>UniDex - Registration</title>
<link rel="stylesheet" href="style.css" type="text/css">
<style type="text/css">
#content {
border: 1px solid #006699;
background:white;
padding-top: 20px;
text-align:center;
}
</style>
</head>
<body>
<div id="content">

<form action="register2.php" method="post">
<table width="100%" align="center">
<h2>Registrater for UniDex</h2>
<p>Fields marked with a Poke Ball are required.</p>

<tr>
<td><img src="images/pokeball.png" alt="Required Field">Username</td>
<td><input name="uname" type="text"></td>
</tr>

<tr>
<td><img src="images/pokeball.png" alt="Required Field">Password</td>
<td><input name="pass" type="password"></td>
</tr>

<tr>
<td><img src="images/pokeball.png" alt="Required Field">E-Mail</td>
<td><input name="email" type="text"></td>
</tr>


<tr>
<td><input name="submit" value="Register" type="submit"></td>
</tr>
</table>
</form>

</div>


</body>
</html>

 

register2.php

<?
$pagename = "Registration";
include "session2.inc.php";
include "../config.inc.php";
include "top.txt";
//Connect to mySQL Database
mysql_connect("$host", "$username", "$password")or die("Can't connect");
mysql_select_db("$db_name")or die("Can't connect");

// Get the information the user submitted for registration
$uname=$_POST['uname'];
$pass=$_POST['pass'];
$email=$_POST['email'];

// Encrypt the password using MD5, to safely store the password.
$md5pass=md5($password);

$tbl_name = "admins";
$sql = "INSERT INTO $tbl_name (`uname` , `pass` , `email`) VALUES ('$uname' , '$md5pass' , '$email')";
$result=mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $sql);

if($result){
echo "Thank you for registering, $uname. You may now <a href=\"index.php\">Login</a>";
}
else {
echo "There was a mistake processing your registration. Go <a href=\"register.php\">back</a> and try again. ^^;";
}
mysql_close();
include "bottom.txt";
?>

Link to comment
https://forums.phpfreaks.com/topic/58109-solved-registration-form-help/
Share on other sites

From what I can see, register2.php doesn't even check that all the fields were filled in.

 

register2.php

<?php
$pagename = "Registration";
include "session2.inc.php";
include "../config.inc.php";
include "top.txt";
//Connect to mySQL Database
mysql_connect("$host", "$username", "$password")or die("Can't connect");
mysql_select_db("$db_name")or die("Can't connect");

// Get the information the user submitted for registration
$uname=$_POST['uname'];
$pass=$_POST['pass'];
$email=$_POST['email'];

if(empty($uname)) {
    echo 'You did not fill in your username';
}
elseif(empty($pass)) {
    echo 'You did not fill in your password.';
}
elseif(empty($email)) {
    echo 'You did not fill in your e-mail.';
}

// Encrypt the password using MD5, to safely store the password.
$md5pass=md5($password);

$tbl_name = "admins";
$sql = "INSERT INTO $tbl_name (`uname` , `pass` , `email`) VALUES ('$uname' , '$md5pass' , '$email')";
$result=mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $sql);

if($result){
echo "Thank you for registering, $uname. You may now <a href=\"index.php\">Login</a>";
}
else {
echo "There was a mistake processing your registration. Go <a href=\"register.php\">back</a> and try again. ^^;";
}
mysql_close();
include "bottom.txt";
?>

 

For each of the fields that are required, do what I have done. If a field is optional, ignore it.

Yep, and also validating the e-mail address. This is just a function that I wrote a long time ago that validates the e-mail address..

 

<?php
function validate_email ($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
	// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
	return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
	if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
		return false;
	}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
	// Check if domain is IP. If not, it should be valid domain name
	$domain_array = explode(".", $email_array[1]);
	if (sizeof($domain_array) < 2) {
		// Not enough parts to domain
		return false;
	}
	for ($i = 0; $i < sizeof($domain_array); $i++) {
		if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
		return false;
		}
	}
}
return true;
}
?>

that's only for input text.

 

but lets say a user go directly to your php code?

 

your mysql well give out an error since there is no value at all saying syntax error and give out some information of your database..

 

so thats why you need to check that the form was submitted from the registration page and that the necessary fields are fill.

 

 

sorry my English not very good.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.