Jump to content

Recommended Posts

Posted

I've been following the video's to the letter, literally :) , but still don't get the error message when not all fields in the form are filled in.

Here is my code (but, if it's easier for you, send me yours and I'll be able to figure it out by myself).

Thanks,

judith

 

records.php

 

<?php

 

function renderForm($first = '', $last = '' , $error = '', $id = '')

 

{ ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<title>

<?php

if ($id !='')

{

echo "Edit Record";

}

else

{

echo "New Record";

}

?>

</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<body>

<h1>

<?php

if ($id !='')

{

echo "Edit Record";

}

else

{

echo "New Record";

}

?>

</h1>

<?php

if ($error !='')

{

echo "<div style='padding:4px; border:1px solid red; color:red" . $error . "</div>";

}

?>

<form action="" method="post">

<div>

<?php

if ($id != ''){ ?>

<input type="hidden" name="id" value="<?php echo $id;?>" />

<p>ID: <?php echo $id; ?></p>

<?php }

?>

<strong> First name: *</strong> <input type="text" name="firstname"

value="<?php echo $first; ?>" /> <br />

<strong> Last name: *</strong> <input type="text" name="lastname"

value="<?php echo $last; ?>" />

<p>* required </p>

<input type="submit" name="submit" value="submit" />

</div>

</form>

</body>

</html>

<?php }

 

if (isset($_GET['id']))

{

//echo "id is set"; //TEST

//editing existing record

renderForm(NULL, NULL, NULL, $_GET['id']);

}

else

{

//echo "id is not set"; //TEST

//creating new record

if (isset($_POST['submit']))

{

 

$firstname = htmlentities($_POST['firstname'], ENT_QUOTES);

$lastname = htmlentities($_POST['lastname'], ENT_QUOTES);

 

if ($firstname == '' || $lastname == '')

{

$error = 'ERROR: please fill in all required fields';

renderForm($firstname, $lastname, $error); //if the user entered one of the fields, he doesn't lose that

}

}

else

{

renderForm();

}

 

}

 

?>

Posted

Just a quick skim over the above script, I noticed this line:

 

if ($firstname == '' || $lastname == '')

 

try changing this to:

 

if ((!isset($firstname)) || (!isset($lastname)))
{

 

I tend to use the isset method rather than comparing to empty strings. Im not sure if this will fix your problem, but I believe it is a better way to compare against. I have has problems in the past doing it the way this script does. I haven't tested this unfortunately.

Posted

if ($firstname == '' || $lastname == '')

 

try changing this to:

 

if ((!isset($firstname)) || (!isset($lastname)))
{

 

 

Thanks for suggestion; didn't work, though -no error, just empty page....

Posted

Looks like the issue is with this line:

 

echo "<div style='padding:4px; border:1px solid red; color:red" . $error . "</div>";

You're missing a "'>" after "red" to close the div before you show the error message.

 

It should be like this:

 

echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>";

Posted

Looks like the issue is with this line:

 

echo "<div style='padding:4px; border:1px solid red; color:red" . $error . "</div>";

You're missing a "'>" after "red" to close the div before you show the error message.

 

It should be like this:

 

echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>";

 

Of course, I should have seen that myself.

 

Thanks so much!

Posted

Happy to help. :) For future reference, I found the issue by running the code in the browser and submitting an empty form. When the page refreshed, I noticed that the form elements were missing, which seemed strange, leading me to check the rendered source code.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...