9, July 2008

Calling Two Different Functions onSubmit - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  JavaScript
Topic: Calling Two Different Functions onSubmit
« previous next »
Pages: [1] 2 Print

Author Topic: Calling Two Different Functions onSubmit  (Read 1135 times)
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« on: Sep 19, 2007, 05:26:46 PM »

guys, here i go again:

i have two functions i want to call when my user clicks the submit button in my form. how should i do it? here is the code i found on the web:

Function 1
Code:
<script>

/*
Check required form elements script-
By JavaScript Kit (http://javascriptkit.com)
Over 200+ free scripts here!
*/

function checkrequired(which){
var pass=true
if (document.images){
for (i=0;i<which.length;i++){
var tempobj=which.elements[i]
if (tempobj.name.substring(0,8)=="required"){
if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==-1)){
pass=false
break
}
}
}
}
if (!pass){
alert("One or more of the required fields are not completed. Please complete them, then submit again!")
return false
}
else
return true
}
</script>

onSubmit="return checkrequired(this)"

Function 2
Code:
function onSubmitForm( f, ext ){
//(C)2006 Stephen Chalmers

var badName="", allBlank=true, rx;

rx=new RegExp("[^\.]\."+ext+"\s*$", "i");

for( var i=0,e=f.elements; i<e.length; i++ )
{
if( e[i].type=='file' )
{
e[i].value=e[i].value.replace(/^\s+/,'').replace(/\s+$/,'');

if( e[i].value.length )
{
allBlank=false;
if( !rx.test(e[i].value) )
badName+='\n\n' + e[i].value;
}
}
}

if( allBlank )
alert("Please click on 'Browse' and select a file to upload.")
else
if( badName )
alert( 'A required .'+ext+' extension was not found in:' + badName );

return (badName || allBlank) ? false : true;
}

onSubmit="return onSubmitForm(this, [\'gif\', \'jpg\', \'jpeg\'])"

i wish to validate some required fields and the file name of the photo they will upload.


Last blog : Alona Beach, Bohol, Philippines
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6274
38470 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Sep 19, 2007, 05:48:34 PM »

create a third function that calls the other two Smiley


Last blog : 4th of July Lottery from TemplateMonster.com
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #2 on: Sep 19, 2007, 05:58:46 PM »

Code:
function Third {
var Rtn = true;
Rtn = checkrequired(this);
if (Rtn) {
Rtn = onSubmitForm(this, ['gif', 'jpg', 'jpeg']);
}
return Rtn;
}

onSubmit="return Third()"

i tried this but it didnt work. please correct it.


Last blog : Alona Beach, Bohol, Philippines
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6274
38470 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Sep 19, 2007, 06:23:10 PM »

just read your full post Smiley

I think you can't validate the filename before upload.

but why not validating the whole thing on the server site (using php)?

check this demo:
http://www.finalwebsites.com/demos/php_file_upload.php Visit through proxy


Last blog : 4th of July Lottery from TemplateMonster.com
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #4 on: Sep 19, 2007, 06:33:26 PM »

the two functions work separately but when i call the two, it doesnt anymore. is there anything wrong with the code?


Last blog : Alona Beach, Bohol, Philippines
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6274
38470 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Sep 19, 2007, 07:30:07 PM »

the two functions work separately but when i call the two, it doesnt anymore. is there anything wrong with the code?

i think the problem is this, btw you need these functions together like:
if all fields are valid then validate the file name...


Last blog : 4th of July Lottery from TemplateMonster.com
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #6 on: Sep 19, 2007, 07:59:03 PM »

tried it on my own but i wasnt able to make it work. any help with the code for combining or calling the two functions at the same time will be very much appreciated!
« Last Edit: Sep 19, 2007, 08:57:19 PM by vbignacio »


Last blog : Alona Beach, Bohol, Philippines
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7945
40623 credits
Members referred : 3



« Reply #7 on: Sep 20, 2007, 10:32:48 AM »

First of all, can you explain what exactly do you want to do?

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6274
38470 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Sep 20, 2007, 10:38:55 AM »

First of all, can you explain what exactly do you want to do?

he is trying something similar to my demo link Smiley


Last blog : 4th of July Lottery from TemplateMonster.com
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #9 on: Sep 20, 2007, 10:46:55 AM »

thanks. i just want to validate a form for the required fields and the file name of the photos to be uploaded just using javascript. i cant do it server side because i already have php functions implemented and i dont want to complicate it further, me not being a programmer...


Last blog : Alona Beach, Bohol, Philippines
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7945
40623 credits
Members referred : 3



« Reply #10 on: Sep 20, 2007, 10:52:32 AM »

Validate the file name against what? If the file is not empty, then it is correct, right?

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #11 on: Sep 20, 2007, 11:19:33 AM »

if they are submitting the right file format: jpeg, jpg, gif, png... and they are filling the required fields before it gets sent to my server.


Last blog : Alona Beach, Bohol, Philippines
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6274
38470 credits
Members referred : 374


It's time to use PHP5!


« Reply #12 on: Sep 20, 2007, 12:56:15 PM »

if they are submitting the right file format: jpeg, jpg, gif, png... and they are filling the required fields before it gets sent to my server.

maybe you should post the existing php code, adding the two rows for validation is much easier then writing some new JS function


Last blog : 4th of July Lottery from TemplateMonster.com
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #13 on: Sep 20, 2007, 01:48:25 PM »

thanks man. i decided to just convert whatever file they uploaded into a jpeg file.


Last blog : Alona Beach, Bohol, Philippines
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6274
38470 credits
Members referred : 374


It's time to use PHP5!


« Reply #14 on: Sep 20, 2007, 02:42:17 PM »

thanks man. i decided to just convert whatever file they uploaded into a jpeg file.

yeah use imagemagicks (but you need to prevent the upload of dangerous files (scripts))


Last blog : 4th of July Lottery from TemplateMonster.com
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #15 on: Sep 20, 2007, 04:00:54 PM »

even if i convert those dangerous file or scripts into .jpeg file, are they still dangerous?


Last blog : Alona Beach, Bohol, Philippines
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6274
38470 credits
Members referred : 374


It's time to use PHP5!


« Reply #16 on: Sep 20, 2007, 04:08:55 PM »

even if i convert those dangerous file or scripts into .jpeg file, are they still dangerous?

I guess this should be answered by a Linux guru Smiley


Last blog : 4th of July Lottery from TemplateMonster.com
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7945
40623 credits
Members referred : 3



« Reply #17 on: Sep 20, 2007, 08:49:31 PM »

As far as I know nothing is dangerous unless you do it Smiley

If you are moving the files to a standard directory, and you are not using those files to run code (which would be impossible with php anyway) you wont have any problems.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #18 on: Sep 20, 2007, 10:21:47 PM »

thanks for the help!


Last blog : Alona Beach, Bohol, Philippines
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6274
38470 credits
Members referred : 374


It's time to use PHP5!


« Reply #19 on: Sep 20, 2007, 10:25:00 PM »

As far as I know nothing is dangerous unless you do it Smiley

If you are moving the files to a standard directory, and you are not using those files to run code (which would be impossible with php anyway) you wont have any problems.
so I cant run a (risky) script via the browser like http://www.domain.com/dir/somescript.jpg? Visit through proxy


Last blog : 4th of July Lottery from TemplateMonster.com
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7134
Tags : javascript form submit Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] 2 Print 
Webdigity Webmaster Forums  >  Web Development  >  JavaScript
Topic: Calling Two Different Functions onSubmit
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Jul 09, 2008, 01:30:11 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 35.590
Total Topics: 7.356
Total Members: 3.661
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: komrad

27 Guests, 4 Users online :

17 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.