Gå til innhold

PHP form - registreringsskjema


Anbefalte innlegg

Eg held på med eit registreringsskjema på ei nettside der alle nye brukarar må registrera seg. Meininga med skjemaet er at alle feilfrie/ferdig utfylte skjema skal hamne i e-postkassa mi for manuell handsaming der. Eller ein annan praktisk stad for dette. Men korleis får eg sett opp dette?

 

Finnast det elles feil på skjemaet?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Join</title>
<style>
body {background:#000000; font-family:"Fixedsys", Impact, sans-serif; color:#00ff00; text-align:center; font-size:1.3em;}
div {background:#000000}
div.img {text-align:center;}
div.rules {margin-left:auto; margin-right:auto; width:60%;}
ol.t {list-style-type:upper-roman; text-decoration:underline; text-align: left;}

a:link {color:#00FF00; background-color:transparent; text-decoration:none;}
a:visited {color:#00FF00; background-color:transparent; text-decoration:none;}
a:hover {color:#00FF00; background-color:transparent; text-decoration:underline;}
a:active {color:#00FF00; background-color:transparent; text-decoration:underline;}

.error {color: #FF3300; font-family:"Fixedsys"}
</style>
</head>
<body>
<div class="rules">
<h3>Registration form:</h3>
</div>
You agree with the <a href="http://vjkanvkjdv.org/rules.html"
target="_self">rules</a> when submitting the form.
<!--?php
// define variables and set to empty values
$userameErr = $passwordErr = $emailErr = $genderErr = $ageErr = $vErr = $aoaErr = "Error test";
$userame = $password = $email = $gender = $age = $v = $aoa = "Error test";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$usernameErr = "Username is required";
} else {
$username = test_input($_POST["username"]);
// check if username only contains letters, numbers and space, and 3-20
characters long if (!preg_match("/^[a-zA-Z0-9 ]*$/",$username)(elem.length > 2 $username)(elem.length < 21 $username)) {
$usernameErr = "Only letters, numbers and space allowed, and a length of 3 to 20 characters.";
}
}

if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} else {
$password = test_input($_POST["password"]);
// check if password only contain letters and numbers, and 6-16
characters long
if (!preg_match("/^[a-zA-Z0-9]*$/",$password)(elem.length > 5 $password)(elem.length < 17 $password)) {
$passwordErr = "Only letters and numbers allowed, and a length of 6 to 16 characters.";
}
}

if (empty($_POST["email"])) {
$email = "E-mail is required";
} else {
$email = test_input($_POST["E-mail"]);
// check if e-mail address syntax is valid
if ((!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid e-mail address";
}
}

if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
if (empty($_POST["age"])) {
$ageErr = "Age is required";
} else {
$age = test_input($_POST["0"]);
// check if password only contain numbers, and max 2 characters long
if (!preg_match("/^[0-9]*$/",$age)(elem.length < 3 $age)) {
$ageErr = "Only numbers allowed, must contain 2 numbers.";
}
}
if (empty($_POST["v"])) {
$genderErr = "Information is required";
} else {
$v= test_input($_POST["v"]);
}
}

if (empty($_POST["aoa"])) {
$aoaErr = "Information is required";
} else {
$aoa = test_input($_POST["aoa"]);
// check if username only contains letters, numbers and space, and max 15 characters long
if (!preg_match("/^[a-zA-Z0-9 ]*$/",$aoa)(elem.length < 15 $aoa)) {
$aoaErr = "Only letters, numbers and space allowed, and a length up to 15 characters.";
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?-->
<p><span class="error">* Required fields.</span></p>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER[" php_self"]);?=""> We recommend that
you avoid personally information in your username.<br>
<br>
Preferred username: <input name="username" value="<?php echo
$username;?>" type="text"> <span class="error">*
<!--?php echo $usernameErr;?--></span> <br>
<br>
<br>
New password: <input name="password" value="<?php echo
$password;?>" type="text"> <span class="error">*
<!--?php echo $passwordErr;?--></span> <br>
<br>
<br>
We recommend that you here insert an active e-mail address that
does not contain personally information.<br>
You will receive registration confirmation on this address. <br>
<br>
E-mail: <input name="email" value="<?php echo $email;?>"
type="text"> <span class="error"><!--?php echo $emailErr;?--></span>
<br>
<br>
Your gender: <input name="gender" <?php="" if=""
(isset($gender)="" &&="" $gender="="female")" echo=""
"checked";?="" type="radio"> value="female">Female <input
name="gender" <?php="" if="" (isset($gender)="" &&=""
$gender="="male")" echo="" "checked";?="" type="radio">
value="male">Male <span class="error">*
<!--?php echo $genderErr;?--></span> <br>
<br>
<br>
Your age: <input name="age" value="<?php echo $age;?>"
type="text"> <span class="error">*
<!--?php echo $ageErr;?--></span> <br>
<br>
<br>
<input name="v" <?php="" if="" (isset($v)="" &&=""
$virgin="="e")" echo="" "checked";?=""
type="radio"> value="e">E <br>
<br>
<input name="v" <?php="" if="" (isset($v)="" &&=""
$v="="i")" echo="" "checked";?=""
type="radio"> value="i">I<br>
<br>
<input name="v" <?php="" if="" (isset($v)="" &&=""
$v="="v")" echo="" "checked";?=""
type="radio"> value="v">V <span
class="error">*
<!--?php echo $vErr;?--></span> <br>
<br>
<br>
AoA: <input name="aoa" value="<?php echo $aoa;?>"
type="text"> <span class="error">*
<!--?php echo $ageErr;?--></span> <br>
<br>
<input name="submit" value="Submit" type="submit"> </form>
<!--?php
echo "<h2-->Your Input: "; echo $username; echo "<br>
"; echo $password; echo "<br>
"; echo $email; echo "<br>
"; echo $gender; ?><br>
"; echo $age; echo "<br>
"; echo $v; ?><br>
"; echo $aoa; echo "<br>
</body>
</html>

Takk for svar.

Endret av LonelyPunker
Lenke til kommentar
Videoannonse
Annonse

Hei!

 

Om eg var deg, ville eg ha skilt ut alle slike funksjoner til en egen klasse i PHP, slik som det her:

 

HTML Skjema -> php kontroller som validerer all input og delegerer deretter.

 

Jeg kan også varmt anbefale Laravel og deretter kan du google alt du trenger med laravel i søkeordet: http://bensmith.io/email-verification-with-laravel

 

Å bruke et slikt rammeverk gjør alt enklere, mye sikrere og ikke minst så er rammeverket og dens funksjoner godt testet og holdes hyppig oppdatert.

 

Det ser kanskje litt "avansert" ut på overflaten, men det tar kort tid å komme inn i prosessen, som igjen du vil merke er ekstremt lettvint ;)

 

Min anbefaling!

 

Uansett, for å sende en mail er det vel en funksjon i PHP som heter "mail()". Du kan også bruke en litt mer konfigurer-bar klasse "Tectite formmail". Har brukt den selv en gang, og den funket fint.

 

For å verifisere brukeren så må du evt. ha en verifiseringkode som blir tilegnet brukeren som holder på å registrere seg. Denne koden må lagres en plass, og da helst i en database. Deretter må du sende mailen til deg selv, istedenfor brukeren, så kan du legge inn mulighet (linker i mailen) for "Aksepter registrering" eller "Avvis registrering" feks.

 

Du kan også lage en administrativ profilside på nettsiden din, der du har en tabell som blir oppdatert med registrerings kandidater som du da derfra kan akseptere/avvise, og heller sende en mail til deg selv som sier ifra om at noen har registrert seg.

 

Mange mange muligheter, alt kommer an på hvordan du setter opp siden din, og det er derfor jeg anbefaler et slikt rammeverk som feks. Laravel.

 

:)

Lenke til kommentar

Har nå sett opp skjemaet på nytt, denne gongen bygd på ein mal med eigen php-fil i tillegg til utgangspunktet som er ei html-fil.

 

Slik situasjonen er nå så får eg fylt ut skjemaet og blir sendt vidare til php-fila for validering. Men det er her prosessen stoppar opp.

 

Er det nokon som kan sjå kva som er gale?

 

Og er det nokon som veit kva eg må legge til for å setja min/max tal på karakterar i input?

Eg har prøvd fleire ulike formlar, men får det ikkje til å fungera.

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style>
div.contact {background:#000000; font-family:"Fixedsys"; color:#00ff00; text-align:center;}

.error {color: #FF3300; font-family:"Fixedsys"}
</style>
</head> 
<?php
/* Set e-mail recipient */
$myemail = "[email protected]";

/* Check all form inputs using check_input function */
$username = check_input($_POST['username'], "Enter username");
$password = check_input($_POST['password'], "Enter password");
$email = check_input($_POST['email'] "Enter your e-mail");
$gender = check_input($_POST['gender']);
$age = check_input($_POST['age']);
$v = check_input($_POST['v']);
$aoa = check_input($_POST['aoa'] "Enter your AoA");

if (!preg_match("/^[a-zA-Z0-9 ]*$/", $username))
{show_error( "Only letters, numbers and space allowed, and a length of 3 to 20 characters.");}

if (!preg_match("/\s/",$password))
{show_error("Only letters and numbers allowed, and a length of 6 to 16 characters.");}

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{show_error("E-mail address not valid");}

if (!preg_match("/\D/", $age))
{show_error("Only numbers allowed, must contain 2 numbers.");}

if (!preg_match("/^[a-zA-Z0-9 ]*$/"$aoa))
{show_error("Only letters, numbers and space allowed, and a length up to 15 characters.");}

/* Let's prepare the message for the e-mail */
$message = "Your registration form has been submitted by:


Username: $username

Password: $password

E-mail: $email

Gender: $gender

Age: $age

Vat: $v

AoA: $aoa

End of message";

$subject = "New registration!"
/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location:submitted.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?--> <div class="contact" <b>Please correct the following error:</b></div><br />
<?php echo $myError; ?>
<?php
exit();
}
?-->
Lenke til kommentar

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style>
div.contact {background:#000000; font-family:"Fixedsys"; color:#00ff00; text-align:center;}

.error {color: #FF3300; font-family:"Fixedsys"}
</style>
</head>
<?php
/* Set e-mail recipient */
$myemail = "[email protected]";

/* Check all form inputs using check_input function */
$username = check_input($_POST['username'], "Enter username");
$password = check_input($_POST['password'], "Enter password");
$email = check_input($_POST['email'], "Enter your e-mail");
$gender = check_input($_POST['gender']);
$age = check_input($_POST['age']);
$v = check_input($_POST['v']);
$aoa = check_input($_POST['aoa'], "Enter your AoA");

if (!preg_match("/^[a-zA-Z0-9 ]*$/", $username))
{show_error( "Only letters, numbers and space allowed, and a length of 3 to 20 characters.");}

if (!preg_match("/\s/",$password))
{show_error("Only letters and numbers allowed, and a length of 6 to 16 characters.");}

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{show_error("E-mail address not valid");}

if (!preg_match("/\D/", $age))
{show_error("Only numbers allowed, must contain 2 numbers.");}

if (!preg_match("/^[a-zA-Z0-9 ]*$/", $aoa))
{show_error("Only letters, numbers and space allowed, and a length up to 15 characters.");}

/* Let's prepare the message for the e-mail */
$message = "Your registration form has been submitted by:


Username: $username

Password: $password

E-mail: $email

Gender: $gender

Age: $age

Vat: $v

AoA: $aoa

End of message";

$subject = "New registration!";
/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location:submitted.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
echo '<div class="contact" <b>Please correct the following error:</b></div><br />';
echo $myError;
exit();
}
?>
Lenke til kommentar

Jeg ville tatt i bruk phpmailer (https://github.com/Synchro/PHPMailer). Fantastisk lett å bruke, og øker sannsynligheten for at epostene faktisk kommer frem, og ikke havner i søppelpost etc. Kan formatere en html-layout uten problemer og være sikker på at ting når frem. Sette opp flere mottakere, svar til etc. enkelt og raskt.

 

Bare å laste inn phpmailer biblioteket, og så starte en ny mail med

$mail = new PHPMailer;

og videre fylle opp med hva enn man måtte ønske, og avslutte med

$mail->send()
Lenke til kommentar

 

 



<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style>
div.contact {background:#000000; font-family:"Fixedsys"; color:#00ff00; text-align:center;}

.error {color: #FF3300; font-family:"Fixedsys"}
</style>
</head> 
<?php
/* Set e-mail recipient */
$myemail = "[email protected]";

/* Check all form inputs using check_input function */
$username = check_input($_POST['username'], "Enter username");
$password = check_input($_POST['password'], "Enter password");
$email = check_input($_POST['email'], "Enter your e-mail");
$gender = check_input($_POST['gender']);
$age = check_input($_POST['age']);
$v = check_input($_POST['v']);
$aoa = check_input($_POST['aoa'], "Enter your AoA");

if (!preg_match("/^[a-zA-Z0-9 ]*$/", $username))
{show_error( "Only letters, numbers and space allowed, and a length of 3 to 20 characters.");}

if (!preg_match("/\s/",$password))
{show_error("Only letters and numbers allowed, and a length of 6 to 16 characters.");}

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{show_error("E-mail address not valid");}

if (!preg_match("/\D/", $age))
{show_error("Only numbers allowed, must contain 2 numbers.");}

if (!preg_match("/^[a-zA-Z0-9 ]*$/", $aoa))
{show_error("Only letters, numbers and space allowed, and a length up to 15 characters.");}

/* Let's prepare the message for the e-mail */
$message = "Your registration form has been submitted by:


Username: $username

Password: $password

E-mail: $email

Gender: $gender

Age: $age

Vat: $v

AoA: $aoa

End of message";

$subject = "New registration!";
/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location:submitted.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
	$data = trim($data);
	$data = stripslashes($data);
	$data = htmlspecialchars($data);
	if ($problem && strlen($data) == 0)
	{
		show_error($problem);
	}
	return $data;
}

function show_error($myError)
{
	echo '<div class="contact" <b>Please correct the following error:</b></div><br />';
	echo $myError;
	exit();
}
?>

 

 

 

Takk, har lagt inn denne med visse endringar. :)

Lenke til kommentar
  • 2 uker senere...

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...