I made register form where you have to put in all your personal info. Now when you submit and you get some errors, it makes all the forms empty, so you have to fill everything in again. How do I prevent this and let everything stay where it stood? (except password maybe)
This is my code:
<html>
<head>
<title>Westpop</title>
<link rel="stylesheet" href="opmaak.css">
</head>
<body>
<fblack>
<div id="header"><a href="Index.php"></a></div>
<div id="registreer">
<table cellpadding="5" align="center">
<th colspan="2" align="left">Registeren</th>
<form name="registreren" method="post" action="registreer.php">
<tr>
<td><fblackbold>Inloggegevens:<fblackbold></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" size="50" placeholder="email@provider.com" name="email"></td>
</tr>
<tr>
<td>Wachtwoord</td>
<td><input type="password" size="50" placeholder="min. 8 tekens, 1 hoofdletter, 1 cijfer" name="wachtwoord"></td>
</tr>
<tr>
<td><fblackbold>Persoonlijke gegevens:<fblackbold></td>
</tr>
<tr>
<td>Voornaam</td>
<td><input type="text" size="50" placeholder="" name="voornaam" ></td>
</tr>
<tr>
<td>Achternaam</td>
<td><input type="text" size="50" placeholder="" name="achternaam"></td>
</tr>
<tr>
<td>Geboorte Datum</td>
<td><input type="hidden" size="8" name="action" value="submitform" />
<input type="text" size="50" placeholder="jjjj-mm-dd" name="geboortedatum"></td>
</tr>
<tr>
<td>Geslacht</td>
<td>M<input type="radio" size="50" value="m" name="geslacht">
V<input type="radio" size="50" value="v" name="geslacht"></td>
</tr>
<tr>
<td>Adres</td>
<td><input type="text" size="50" placeholder="Straat 00" name="adres"></td>
</tr>
<tr>
<td>Woonplaats</td>
<td><input type="text" size="50" placeholder="" name="woonplaats"></td>
</tr>
<tr>
<td>Telefoonnummer</td>
<td><input type="text" size="50" placeholder="min. 9 tekens" name="telefoonnummer"></td>
</tr>
<tr>
<td>Functie</td>
<td><select name="functie">
<option value="catering">Catering</option>
<option value="muziekpodia">Muziek en podia</option>
<option value="vervoerovernachten">Vervoer en overnachten</option>
<option value="logistiekbeveiliging">Logistiek en beveiliging</option>
<option value="diversen">Diversen</option>
</select></td>
</tr>
<tr align="right">
<td></td>
<td><input type="reset" value="Wissen"><input type="submit" name="verzenden" value="Verzenden"></td>
</tr>
</form>
</table>
<?php
if (isset($_POST['action']) && $_POST['action']=='submitform') {
$host = "localhost";
$gebruikersnaam = "root";
$wachtwoord = "";
mysql_connect($host, $gebruikersnaam, $wachtwoord);
$demooistedatabase = "c5g4westpopintranet";
mysql_select_db($demooistedatabase);
$achternaam = $_POST["achternaam"];
$voornaam = $_POST["voornaam"];
$gbdatum = $_POST["geboortedatum"];
$email = $_POST["email"];
$geslacht = $_POST["geslacht"];
$wachtwoord = $_POST["wachtwoord"];
$woonplaats = $_POST["woonplaats"];
$adres = $_POST["adres"];
$telefoonnummer = $_POST["telefoonnummer"];
$functie = $_POST["functie"];
$achternaam = stripslashes($achternaam);
$voornaam = stripslashes($voornaam);
$gbdatum = stripslashes($gbdatum);
$email = stripslashes($email);
$geslacht = stripslashes($geslacht);
$wachtwoord = stripslashes($wachtwoord);
$woonplaats = stripslashes($woonplaats);
$adres = stripslashes($adres);
$telefoonnummer = stripslashes($telefoonnummer);
$functie = stripslashes($functie);
$query ="INSERT INTO vrijwilliger (voornaam, achternaam, gbdatum, geslacht, wachtwoord, woonplaats, adres, telefoonnummer, functie, activiteitID, groepID, email)
VALUES('$voornaam','$achternaam','$gbdatum','$geslacht','$wachtwoord','$woonplaats','$adres','$telefoonnummer','$functie',null,null,'$email')";
$foutloos=true;
if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $wachtwoord) === 0)
{
echo '<fblack>Wachtwoord moet minstens 8 tekens lang zijn, een kleine letter, grote letter en cijfer bevatten.<br><fblack>';
$foutloos = false;
}
if(preg_match("/^[0-9]{10}+$/", $telefoonnummer) === 0)
{
echo '<fblack>Het telefoonnummer moet 10 cijfers bevatten.<br><fblack>';
$foutloos = false;
}
if(preg_match("/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/", $gbdatum) === 0)
{
echo '<fblack>Geboorte datum moet op dit formaat ingevoerd worden: JJJJ-MM-DD<br><fblack>';
$foutloos = false;
}
if(preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", $email) === 0)
{
echo '<fblack>Email moet hier op lijken: email@provider.com<br><fblack>';
$foutloos = false;
}
if(preg_match("/^[A-Z][a-zA-Z -]+$/", $voornaam) === 0)
{
echo '<fblack>Voornaam is niet geldig ingevoerd, heeft u een hoofdletter gebruikt?<br><fblack>';
$foutloos = false;
}
if(preg_match("/^[A-Z][a-zA-Z -]+$/", $achternaam) === 0)
{
echo '<fblack>Achternaam is niet geldig ingevoerd, heeft u een hoofdletter gebruikt?<br><fblack>';
$foutloos = false;
}
if ($geslacht == '')
{
echo '<fblack>U heeft uw geslacht niet aangegeven.<br><fblack>';
$foutloos = false;
}
if(preg_match("/^[a-zA-Z]+\ +[0-9]+$/", $adres) === 0)
{
echo '<fblack>Het adres is verkeerd ingevoerd.<br><fblack>';
$foutloos = false;
}
if(preg_match("/^[a-zA-Z\s]+$/", $woonplaats) === 0)
{
echo '<fblack>De woonplaats is verkeerd ingevoerd.<br><fblack>';
$foutloos = false;
}
if ($foutloos == true)
{
mysql_query($query)
or die('<fblack>U staat al in ons systeem<A HREF="javascript:javascript:history.go(-1)"><br><br>Klik hier om terug te gaan</A><fblack>');
echo "<fblack>Uw registratie is succesvol verwerkt!<br>Log <a href='login.php' MEDIA=screen>hier</a> in<fblack>";
}
}
?>
</div>
<?php
include ("html_end.php");
?>
Thanks in advance!
You can echo posted value like:
<input type="text" size="50" placeholder="email@provider.com" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>">
Do the same with other inputs.
You have to pass the values back to the page from your form action.
Once they are available on the page, you'd then set them as the values. For example:
In your PHP which is doing validation, set
$emailEntered = $_POST['email'];
Then in your form, echo the value:
<tr>
<td>Email</td>
<td><input type="text" size="50" placeholder="email@provider.com" name="email" value="<?php echo $emailEntered; ?>"></td>
</tr>
You have to set the values of the fields to the values that the user enters, so something like
<input type="text" size="50" placeholder="Straat 00" name="adres" value="<?php echo $_POST['adres']; ?>">
Rather than going back to history.go(-1)
(which will not work if your user doesn't use javascript, you should rather generate the page containing the form again, i.e. by putting your HTML code in a file named form.php and by including that file when something goes wrong :
include ('form.php');
if (isset($_POST) {
deal_with_post_data();
if (something_wrong()) {
include('form.php');
} else {
include('proceed.php');
}
}
And, in your form, do the following :
<input type="text" name="foo" value="<?php echo $_POST['foo']; ?>"/>
That way you will get the previous value, if any.
By the way, beware of stripslashes
. It may not be enough to prevent SQL injection. Since you're using MySQL, use mysql_real_escape_string
instead.
Browser doesn't save your data so you need put recieved data by yourself again.
<?php
$email = isset($_POST['email']) ? $_POST['email'] : '';
?>
<td><input type="text" ... name="email" value="<?php echo $email; ?>"></td>
You can do more complex, but i hope you get the idea. Don't forget to sanitaze values you recieve from $_POST!
you can use session variables:
at the beggining of the page
<?php
session_start();
//checking if variables are setted
if !(isset($_SESSION("achternaam")){
$_SESSION["achternaam"]=""
}
//same for all fields
in the form
<td><input type="text" size="50" placeholder="" name="achternaam" value="<?php echo $_SESSION["achternaam"];?>"></td>
same to all
and then before the $query:
$_SESSION["achternaam"] = $achternaam;
At the end if success you must reset the variables
$_SESSION["achternaam"] = "";
Instead of reloading the values after some server side error , you can validate the fields in client side using javascript
//HTML
// the submit button on page
<input type="submit" name="verzenden" id= 'verzenden' value="verzenden" onclick='return validate();' />
// javascript
<script type='text/javascript'>
function validate()
{
// check for all the text fields if they are null
var fname= document.getElementById('firstname');
var lname= document.getElementById('lastname');
if(fname.value == '')
{
// if the field is null it does not submit page and focus() function means
// cursor will be in that textbox
alert('Please enter first name');
fname.focus();
return false;
}
}
</script>
I'm not sure this will work for you, but usually my form's action was on the same page, so when there was an error, I would echo the values back in the values attributes. Since this is not your case, you could change the action to that same page, and do something like this in that page:
<?php
if (sizeof($_POST)) {
require 'the_action_you_have_now.php';
// If it succeeds, it would redirect to another page and exit, otherwise, just continue, and set a variable with a message that says there's an error.
}
?>
To set values:
value="<?php print (isset($_POST['email']) ? $_POST['email'] : '');?>
Or, it might work depending on your config:
value="<?=(isset($_POST['email']) ? $_POST['email'] : '')?>
This will be different for s and s:
<textarea><?php print htmlentities($_POST['comment']); ?></textarea>
<select>
<?php
foreach ($options as $value => $text) {
$selected = ($_POST['the_select'] == $value)? "selected='selected'" : "";
print "<option value='$value' $selected>$text</option>";
}
?>
</select>