Forum Moderators: coopster

Message Too Old, No Replies

isset not working

or so i think....

         

kumarsena

1:48 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



hey guys,

this is a simple script i wrote to sign up to a mailing list. as far as i can see, this thing should work, but it is working very funny. if i submit with a blank textbox, ie 'add' not set, it doesnt catch that, but prints 'thanks for signing up...'

any idea as to why the script does not stop after executing the first if statement...?

thanks
k

<?php

if (!isset($_POST['add']))
{
echo 'HA! HA! very funny, now ENTER an address befdore you submit, moron!';
}

else
{
$add = $_POST['add'];

if ($add == 'test')
{
echo 'Is it so hard to proviude a proper address....?';
}

else
{
$fh = fopen('adds.php3', 'a');
fwrite($fh, $add);
fclose($fh);
echo 'Thank You for signing up to the list. you will get regular updates.';
//header("Location: index.php3?art=$art");
}
}
?>

mincklerstraat

1:58 pm on Oct 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to be using if(empty($_POST['add'])){ here. Empty checks to see if it's either not set, or if it's 'blank' - having the values null or false.

When you have an html form that's submitted, there will be elements in the $_POST or $_GET array for each field, whether it's filled in or not. But when it's not filled in, it will be 'empty.' So for this kind of thing use empty.

ergophobe

4:00 pm on Oct 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Actually, use both. If someone gets to the page from a direct link (i.e. not submitting a form), you want to catch with isset() or get a notice for an undefined array index. Use empty() to figure out if the person submitting the form actually entered any data.

Tom

jatar_k

5:14 pm on Oct 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could also write it something like so

<?php 
$emailpattern = '^([._a-zA-Z0-9-]){2,255}@([._a-zA-Z0-9-]){2,255}\.([.a-zA-Z]){2,7}$';
if (!isset($_POST['add']) ¦¦ empty($_POST['add'])) {
echo 'HA! HA! very funny, now ENTER an address befdore you submit, moron!';
} else if (!ereg($pattern, $_POST['add'])) {
echo 'Is it so hard to proviude a proper address....?';
} else {
$add = $_POST['add'];
$fh = fopen('adds.php3', 'a');
fwrite($fh, $add);
fclose($fh);
echo 'Thank You for signing up to the list. you will get regular updates.';
//header("Location: index.php3?art=$art");
}
?>

also gives you a proper pattern test for email addresses.

always remember to replace pipe chars with real pipe characters as this forum software breaks pipes

kumarsena

6:48 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



tnx guys,

that helped, i was wondering though, cause ive worked with forms bfore but never had an issue like this....

tnx again
k

kumarsena

12:44 pm on Oct 10, 2004 (gmt 0)

10+ Year Member



hey again,

teh sugesstions worked,,,but while were at it...
can anyone explain how teh emailpatter thing jatar came up with works...

tnx

Bonusbana

1:04 pm on Oct 10, 2004 (gmt 0)

10+ Year Member



it uses a regular expression. Do a google for the term or check your php.net for more info.

[edited by: Bonusbana at 1:43 pm (utc) on Oct. 10, 2004]

kumarsena

1:19 pm on Oct 10, 2004 (gmt 0)

10+ Year Member



tnx
will do that

jatar_k

3:21 pm on Oct 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



take a look at this

Regular Expressions [webmasterworld.com]