Hola cmarti, tienes razón pero estoy empezando con esto de PHP y es un ejercicio que tengo que hacer. Había un par de errores que he corregído como:
Código PHP:
function validatefield ( $fieldname, $missingfields ) {
if ( in_array( $fieldname, $missingfields ) ) {
echo ' class id="error" ';
}
}
Le quite el id. Ahorra me funciona todo menos el campo lastname. No se por que. El codigo lo deje asi:
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Membership Form</title>
<link rel="stylesheet" type="text/css" href="common.css">
<style type="text/css">
.error { background: #90EE90; color: white; padding: 0.2em;}
</style>
</head>
<body>
<?php
if ( isset ( $_POST["submitbutton"] ) ) {
processform ();
} else {
displayform ( array() );
}
function validatefield ( $fieldname, $missingfields ) {
if ( in_array( $fieldname, $missingfields ) ) {
echo ' class="error" ';
}
}
function setvalue ( $fieldname ) {
if ( isset ( $_POST[$fieldname] ) ) {
echo $_POST[$fieldname];
}
}
function setchecked ( $fieldname, $fieldvalue ) {
if ( isset ( $_POST[$fieldname]) and $_POST[$fieldname] == $fieldvalue) {
echo ' checked="checked" ' ;
}
}
function setselected ( $fieldname, $fieldvalue ) {
if ( isset ( $_POST[$fieldname] ) and $_POST[$fieldname] == $fieldvalue ) {
echo ' selected="selected" ' ;
}
}
function processform () {
$requiredfields = array ("firstname", "lastname", "password1", "password2", "gender" );
$missingfields = array ();
foreach ( $requiredfields as $requiredfield ) {
if ( !isset ( $_POST[$requiredfield]) or !$_POST[$requiredfield] ) {
$missingfields[] = $requiredfield;
}
}
if ($missingfields) {
displayform ($missingfields);
} else {
displaythanks ();
}
}
function displayform ($missingfields) {
?>
<h1>Membersship Form</h1>
<?php if ( $missingfields ) { ?>
<p class="error">There were some problems with the form you submitted.
Please complete the fields highlighted below and click Send Details to
resend the form.</p>
<?php } else { ?>
<p>Thanks for choosing to join The Widget Club. To register, please fill
in your details below and click Send Details. Fields marked with an
asterisk (*) are requiered. </p>
<?php } ?>
<form action="registration.php" method="post">
<div style="width: 30em;">
<label for="firstname" <?php validatefield ( "firstname", $missingfields ) ?> > First name * </label>
<input type="text" name="firstname" id="firstname" value="<?php setvalue("firstname") ?>" />
<label for="lastname" <?php validatefield ("lastname", $missingfields ) ?> > Last name * </label>
<input type="text" name="lastname" id="lastname" value="<?php setvalue ("lastname") ?> "/>
<label for="password1"<?php if ($missingfields) echo ' class="error" ' ?>> Choose a password *</label>
<input type="password" name="password1" id="password1" value="" />
<label for="password2"<?php if ($missingfields) echo ' class="error" ' ?>> Retype password *</label>
<input type="password" name="password2" id="password2" value="" />
<label <?php validatefield ("gender", $missingfields ) ?> > Your gender: * </label>
<label for="gendermale">Male</label>
<input type="radio" name="gender" id="gendermale" value="M"<?php setchecked ("gender" , "M" ) ?> />
<label for="genderfemale">Female</label>
<input type="radio" name="gender" id="gendermale" value="F"<?php setchecked ("gender" , "F") ?> />
<label for="favoritewidget">What's your favorite widget? * </label>
<select name="favoritewidget" id="favoritewidget" size="1">
<option value="superwidget"<?php setselected ("favoritewidget", "superwidget") ?>> The Superwidget</option>
<option value="megawidget" <?php setselected ("favoritewidget", "megawidget" ) ?>> The Megawidget</option>
<option value="wonderwidget"<?php setselected ("favoritewidget", "wonderwidget") ?>> The Wonderwidget</option>
</select>
<label for="newsletter">Do you want to receive our newsletter? </label>
<input type="checkbox" name="newsletter" id="newsletter" value="yes" <?php setchecked ("newsletter", "yes" ) ?> />
<label for="comments">Any comments?</label>
<textarea name="comments" id="comments" rows="4" cols="250"><?php setvalue ("comments") ?></textarea>
<div style="clear:both;">
<input type="submit" name="submitbutton" id="submitbutton" value="Send Details" />
<input type="reset" name="resetbutton" id="resetbutton" value="Reset Form" style="margin-right: 20px;" />
</div>
</div>
</form>
<?php
}
function displayThanks() {
?>
<h1>Thank You</h1>
<p>Thank you, your applicaton has been received.</p>
<?php
}
?>
</body>
</html>