Forum Moderators: coopster
// Check for a title
if (isset ($_POST['title'])) {
$title= ($_POST['title']);
}
else {
$title = '';
}
// Check for a name.
if (isset ($_POST['name'])) {
$name= ($_POST['name']);
}
else {
$name = '';
}
// Check for a year.
if (isset ($_POST['year'])) {
$year= ($_POST['year']);
}
else {
$year = '';
}
//Assign value of bib_type.
$mdbookclub = $_POST['bib_type'];
$teen_mysteries = $_POST['bib_type'];
$kids_aloud = $_POST['bib_type'];
if ($_POST['bib_type']=='mdbookclub')
{
$query = "INSERT INTO mdbookclub (title, name, year)
VALUES ('$title','$name','$year')";
// Run the query.
$result = @mysql_query ($query);
_________________________________________________
But when I use the strlen function to validate which I believe is the correct way and add the "Handle form" part I get a number 1 on the output screen.
I'm trying to understand why this is. Also would it be ok to use the isset function to validate a string as I do in the first part? Thankful for any clarification.
// Handle the form.
if (isset($_POST['submit'])) {
// Create an empty new variable.
$message = NULL;
// Check for a title.
if (strlen($_POST['title']) > 0) {
$title= TRUE;
} else {
$title = FALSE;
$message .= '<p>You forgot to enter a title!</p>';
}
I would imagine that the reason you are getting 1 output is because you are setting the $title variable to true. This is the same as doing $title = 1; So, this will overwrite the original string.
You might also want to use trim() as this will prevent anyone just hitting the space bar to execute the script.
Try something like:
if (strlen(trim($_POST['title']))==0) {
$message .= '<p>You forgot to enter a title!</p>';
}
Then check to see if the $message variable has any value and echo the error.
if (!empty($message))
{
echo $message;
}
Hope that helps.
dc