HighDots Forums  

Simple Form

Javascript JavaScript language (comp.lang.javascript)


Discuss Simple Form in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
bdog4@hotmail.com
 
Posts: n/a

Default Simple Form - 02-01-2006 , 07:01 PM






I know this is beginner stuff but why is it that when I run this
validation my form still gets submitted. Can someone explain what I'm
doing wrong. I don't quite understand how the return true/false works
with the forms. Here is what I have

<?php
if ((isset($_POST["MM_Upload"])) && ($_POST["MM_Upload"] == "form1")) {
//do this
header("Location: Page.php");
}
?>

<script>
function UploadImage( f ){

if(f.userfile.value.length < 1)
{
f.bUpload.disabled = false;
f.bCancel.disabled = false;
f.bUpload.value = 'Upload';
alert("Please select a file to upload!");
f.userfile.focus();
return false;
}
else {
f.bUpload.disabled = true;
f.bCancel.disabled = true;
f.bUpload.value = 'Please Wait...';
//f.submit();
return true;
}
}
</script>

<form action="<?php echo $editFormAction; ?>" method="POST"
enctype="multipart/form-data" name="form1" id="form1" onSubmit="return
UploadImage(this);">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="25">
<input name="userfile" type="file" class="control" id="userfile"
size="40">
<input name="Submit" type="submit" class="control" id="bUpload"
value="Upload" />
<input name="bCancel" type="button" class="control" id="bCancel"
onClick="MM_goToURL('parent','MyRecipes.php');retu rn
document.MM_returnValue" value="Cancel">
<input name="MM_Upload" type="hidden" id="MM_Upload" value="form1">
</form>


Reply With Quote
  #2  
Old   
RobG
 
Posts: n/a

Default Re: Simple Form - 02-01-2006 , 08:06 PM






bdog4 (AT) hotmail (DOT) com wrote:
Quote:
I know this is beginner stuff but why is it that when I run this
validation my form still gets submitted. Can someone explain what I'm
doing wrong. I don't quite understand how the return true/false works
with the forms. Here is what I have
What errors do you get? Your code as posted works fine for me in
Firefox and IE - but that seems to be based on server code.


Quote:
?php
if ((isset($_POST["MM_Upload"])) && ($_POST["MM_Upload"] == "form1")) {
//do this
header("Location: Page.php");
}
?
Not much point in posting PHP code here, post what is received at the
client (use view source).


Quote:
script
The type attribute is required (but has nothing to do with the problem):

<script type="text/javascript">


Quote:
function UploadImage( f ){
Variable names starting with capitals are normally used for constructors
- but there is no law about that. :-)


Quote:
if(f.userfile.value.length < 1)
{
f.bUpload.disabled = false;
f.bCancel.disabled = false;
f.bUpload.value = 'Upload';
alert("Please select a file to upload!");
f.userfile.focus();
return false;
}
else {
f.bUpload.disabled = true;
f.bCancel.disabled = true;
f.bUpload.value = 'Please Wait...';
//f.submit();
return true;
}
}
You could also code that as:

if (f.userfile.value.length) { // Evaluates as true if length is not 0
// do true stuff
} else { // length is zero, nothing entered
// do false stuff
}


Quote:
/script

form action="<?php echo $editFormAction; ?>" method="POST"
enctype="multipart/form-data" name="form1" id="form1" onSubmit="return
UploadImage(this);"
If the onsubmit handler returns false, the form will not be submitted.
Your script will prevent the form being submitted if nothing has been
entered in the userfile text input (provided of course that scripting
has been enabled).


Quote:
INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="25"
input name="userfile" type="file" class="control" id="userfile"
size="40"
input name="Submit" type="submit" class="control" id="bUpload"
It's not a good idea to give a form control a name of 'Submit' as it may
conflict with the form's submit method if you'd used a lower case 's'
(but that's not a problem here).

If you must give the submit button a name, make it the same as the ID,
i.e. name="bUpload".


[...]



--
Rob


Reply With Quote
  #3  
Old   
bdog4@hotmail.com
 
Posts: n/a

Default Re: Simple Form - 02-01-2006 , 08:50 PM



Thanks for the great explanation. What happens is when I submit with
nothing in the file field it prompts me but it must be still submitting
because my php code is seeing that it is submitted and I'm getting
redirected. Thats why I posted the php. I'm thinking it is still
being submitted even when the scripts is saying not to


Reply With Quote
  #4  
Old   
RobG
 
Posts: n/a

Default Re: Simple Form - 02-01-2006 , 09:29 PM



bdog4 (AT) hotmail (DOT) com wrote:
Quote:
Thanks for the great explanation. What happens is when I submit with
nothing in the file field it prompts me but it must be still submitting
because my php code is seeing that it is submitted and I'm getting
redirected. Thats why I posted the php. I'm thinking it is still
being submitted even when the scripts is saying not to
If the error is happening on the client, you have to start with what the
client is getting, not the server code.

Show the page source (trimmed to just the relevant bits) - once the
errors there are identified, you can fix the PHP that is generating the
page. As it is now, we have no idea exactly what the client is acting
upon, only that a part what you are sending works when tested in isolation.


--
Rob


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.