Forum Moderators: coopster

Message Too Old, No Replies

Validate using strlen function or isset function

         

Ann_G

6:35 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



I have a script that works when I do this:

// 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>';
}

dreamcatcher

8:56 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Ann_G,

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

Ann_G

3:10 am on Mar 22, 2005 (gmt 0)

10+ Year Member



Yes, it helped a lot. I did have $title set to TRUE so after reading your message things are making sense to me.
Thanks so much!

dreamcatcher

10:04 am on Mar 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome. :)