Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iLife

macrumors member
Original poster
Sep 4, 2003
51
0
SF Bay Area / Miami FL
ok, so my problem is that when typing the email address in for an email list and simply hittin enter instead of clicking on submit the page is refreshing and not posting any data at all, but its not giving an error message either: so the user has no clue that nothing has gone wrong. i have error handling that works as long as you click submit, but i need it to work when you hit enter too.

here is the code:
----------------------------------------------------------
<?php //script 1.3 index.php email

//checks if submit button has been used
if (isset($_POST['submit'])) {
//handle the form
//validates email and stripslashes
if (!eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$",stripslashes(trim($_POST['email'])))) {
$problem = TRUE;
$errorMessage .= '<p>Please Enter A Valid Email Address.</p>';
}
//check if anything went wrong
if (!$problem) { //nothign went wrong
$message=$email . ',';//concatenates string for text file
echo "Your email address, $message has been added.";
$fp=fopen("./emailList/emailLog.log","a+");// read and write capability for emailLog
fwrite($fp,$message);//writes file
fclose($fp);
} else {

echo "<center><h4><font color=#FF0000>\n The email address,$email, is not valid.\n Please go back to re-enter your information</ font></ h4></ center>";

} else {
//displays form
?>
<form name="signup" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<legend>Enter Your Email Address Below</legend>
<p><b>Email Address:</b>
<input type="text" name="email"
value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>" />

<input type="submit" name="submit" value="Submit" class="fbutton" />
</form><!-- end of form -->

<?php
} //closes conditional statement
?>
---------------------------------------------------------------------------------

any help or suggestions would be great, i've been hitting my head against this, and i'm sure its something stupid i've absent mindedly forgotten. thanks
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
Why don't you switch it so that it looks for $_POST['email'] instead of $_POST['submit']? Since that's the variable that you're going to be checking it makes more sense to me to use that.

I'm configured on an older version of PHP here that doesn't use the GPC variables, so I can't say for sure if this will work for you.
 

iLife

macrumors member
Original poster
Sep 4, 2003
51
0
SF Bay Area / Miami FL
thanks, i seriously think that the problem lies in the form somehow, because the code works if you "click" on the submit button as opposed to merely hitting "enter" or "return" while in the text box. very frustrating. anyways thanks though
 

sonofslim

macrumors 6502a
Jun 6, 2003
742
0
you might consider javascript validation. that way, if there is an error, it's detected before the form is submitted. it's faster if the user doesn't have to wait for a server hit before they're notified of an error; they won't be waiting for your error page to load AND the original form to re-load when they backtrack.

also, maybe this isn't a realistic problem in this case, but i find it's safest to check for $_POST['submit'] when processing forms, and not some other variable. what if at some point you wanted to pass $email from another location? if your conditional is looking for $email and not $submit, your script would bypass your form, jumping straight to your error handler.

(that said, you should listen to rower 99.99% of the time, because he knows what he's talking about.)
 

iLife

macrumors member
Original poster
Sep 4, 2003
51
0
SF Bay Area / Miami FL
the thing is i'm using php because i can more precisley isolate emails that aren't valid, and the way i have it set up, $email isn't getting sent to the text file if the form finds an error.

my problem is the form isn't even submitting wheen you hit "enter" or "return" and is refreshing the page, when you click submit the script works perfectly.

anyways thanks, i guess i may just just have to validate with both php and javascript. i just don't want to have to weed through needless emails when i go to send out updates [this is a massive email list already, i work for a pretty well known company]

thanks again though
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
Originally posted by sonofslim
you might consider javascript validation. that way, if there is an error, it's detected before the form is submitted. it's faster if the user doesn't have to wait for a server hit before they're notified of an error; they won't be waiting for your error page to load AND the original form to re-load when they backtrack.

also, maybe this isn't a realistic problem in this case, but i find it's safest to check for $_POST['submit'] when processing forms, and not some other variable. what if at some point you wanted to pass $email from another location? if your conditional is looking for $email and not $submit, your script would bypass your form, jumping straight to your error handler.

(that said, you should listen to rower 99.99% of the time, because he knows what he's talking about.)

Thanks for the kudos, but I still consider myself a novice when it comes to PHP. :)

Checking for $_POST['submit'] is a technique I've never come across, but it makes sense when you explain it in that context.

iLife-
What're the results of the HTML validation?
 

iLife

macrumors member
Original poster
Sep 4, 2003
51
0
SF Bay Area / Miami FL
it wouldn't work (i'm guessing because it has a php extension)

what i'm tyring is giving the form an id="akjhgdskj" and maybe having its own identifier will help

the form is acting like there are two forms on the page, and that when hitting enter the page refreshes and does nothing...

i've just started using php also: i'm much the novice but learning fast... :D

thanks for all your help
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
It works on PHP files just fine. The page needs a valid doctype declaration and charset specified for it to begin parsing the HTML.
 

sonofslim

macrumors 6502a
Jun 6, 2003
742
0
Originally posted by Rower_CPU
Thanks for the kudos, but I still consider myself a novice when it comes to PHP. :)

really? because i'm always picking up tips from your posts, and i'd like to think i'm at least an "intermediate" user.

anyway, back iLife's problem:
i did some digging, and it appears that some browsers don't send a name/value pair when the enter key is used to submit a form -- only when the button is pressed.

try out your page on some other browsers and see if the problem persists. if you narrow it down to a browser problem, here's your solution: add a hidden input with a value you won't be using elsewhere. like:
PHP:
<input type="hidden" name="issubmitted" value="issubmitted" />

and check for that instead of $submit.
 

sonofslim

macrumors 6502a
Jun 6, 2003
742
0
solution, part II

if you find that it's NOT a browser problem (ie, hitting 'enter' submits your form no matter what) then you could create a regular (non-submitting) button and call submit() via that:

PHP:
onClick='this.form.submit()'

if you do this, don't name your button, or any other element, "submit". having an element named "submit" precedes the submit() method and it can confuse things, make it so your form is never submitted, cause balding, &c.
 

sonofslim

macrumors 6502a
Jun 6, 2003
742
0
one last though: it occurs to me that if you had gone with solution 2, you MIGHT need a further step. i realize you've solved it already, but i don't want to leave anyone with incomplete infomation.

if you're sending via the submit() method because hitting 'enter' screws you up, you'll need to "turn off" the regular submit method by adding this inside your form tag: onSubmit="return false;" and remember that no other element can be named "submit." some browsers will send on 'enter' only if there is a submit button; some won't. this method covers all your bases.

anyway, glad we could help!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.