Jump to content

video php crud


judeur

Recommended Posts

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();

}

 

}

 

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...