469,271 Members | 1,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Simple Form

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>

Feb 2 '06 #1
3 1439
bd***@hotmail.com wrote:
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.

<?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).

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

<script type="text/javascript">

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

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
}

</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).

<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
Feb 2 '06 #2
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

Feb 2 '06 #3
bd***@hotmail.com wrote:
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
Feb 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by r0adhog | last post: by
7 posts views Thread by Houston | last post: by
2 posts views Thread by SU_Oran | last post: by
reply views Thread by Tal Sharfi | last post: by
1 post views Thread by James Alba | last post: by
reply views Thread by 42 | last post: by
4 posts views Thread by Duncan | last post: by
5 posts views Thread by Byron | last post: by
24 posts views Thread by firstcustomer | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.