PDA

View Full Version : form troubles


iLife
Nov 20, 2003, 11:27 AM
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
Nov 20, 2003, 12:53 PM
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
Nov 20, 2003, 01:15 PM
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

Rower_CPU
Nov 20, 2003, 01:18 PM
Did you try it and it didn't work?

Also, have you validated the page to make sure the HTML is correct?

http://validator.w3.org/

sonofslim
Nov 20, 2003, 01:24 PM
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
Nov 20, 2003, 01:30 PM
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
Nov 20, 2003, 01:40 PM
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
Nov 20, 2003, 01:47 PM
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
Nov 20, 2003, 01:51 PM
It works on PHP files just fine. The page needs a valid doctype declaration and charset specified for it to begin parsing the HTML.

iLife
Nov 20, 2003, 01:54 PM
danka

sonofslim
Nov 20, 2003, 02:03 PM
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:
<input type="hidden" name="issubmitted" value="issubmitted" />

and check for that instead of $submit.

sonofslim
Nov 20, 2003, 02:11 PM
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:


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.

iLife
Nov 20, 2003, 02:26 PM
thanks slim, very helpful, i'm going to try it now i'll let you knwo if any of that worked :cool:

iLife
Nov 20, 2003, 02:34 PM
slim, you freaking rock! it was the browser issue. the issubmit worked perfectly. thanks.!!!!!!!!!

:D

::wipes worried brow::

sonofslim
Nov 20, 2003, 02:50 PM
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!