judeur Posted October 1, 2010 Report Share Posted October 1, 2010 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(); } } ?> Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 1, 2010 Report Share Posted October 1, 2010 Judith, I'll take a look at this issue later today. Cheers, Ben Quote Link to comment Share on other sites More sharing options...
jstern Posted October 1, 2010 Report Share Posted October 1, 2010 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. Quote Link to comment Share on other sites More sharing options...
judeur Posted October 1, 2010 Author Report Share Posted October 1, 2010 if ($firstname == '' || $lastname == '') try changing this to: if ((!isset($firstname)) || (!isset($lastname))) { Thanks for suggestion; didn't work, though -no error, just empty page.... Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 1, 2010 Report Share Posted October 1, 2010 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>"; Quote Link to comment Share on other sites More sharing options...
judeur Posted October 1, 2010 Author Report Share Posted October 1, 2010 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! Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 1, 2010 Report Share Posted October 1, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.