473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1514
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: r0adhog | last post by:
I have a very simple form: <html> <head> </head> <body> <% function ValForm() if len(document.form.newapp.all("AccessCode").Value) = 4 then document.form.newapp.submit()
7
by: Houston | last post by:
I have a form that after being filled out has its contents written to a database and then goes to confirmation .asp page. I want the confirmation page to be personalized but I am not sure how to...
2
by: SU_Oran | last post by:
I found this when searching. I need to have a simple script that will upload a single file. It is giving me an error on Set upl = Server.CreateObject("ASPSimpleUpload.Upload") It is saying : ...
6
by: francisco lopez | last post by:
ok , first of all sorry if my english is not so good, I do my best. here is my problem: I donīt know much javascript so I wrote a very simple one to validate a form I have on my webpage. ...
0
by: Tal Sharfi | last post by:
Hi everyone I recently had the need for StringGrid object same as the one that Delphi has. An object that helps show lists of other objects in a simple grid. I searched the news groups and...
1
by: James Alba | last post by:
Hi again all, I was wondering if there was a simple built in dialog box that contained only a textbox and an ok button? Just like the MessageBox.Show(...) class/method. Or do I have to make...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
4
by: Duncan | last post by:
Hi I'm learning c# 2.0 as I feel I need to be able to switch between vb & c#, I'm just starting with a few simple examples and I've come across a problem. I've got two forms ones an MDI parent &...
5
by: Byron | last post by:
I need to create an application that uses primarily a single form rather than an SDI that creates a new form for everythting. However, I don't want an MDI style application since the users I'm...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.