Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-18-2012, 09:26 PM   PM User | #1
andrew55
New Coder

 
Join Date: Mar 2012
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
andrew55 is an unknown quantity at this point
Need help with using image as submit button on form

I have this simple form:

Code:
<form id="form1" name="form1" method="POST" action="do.signup.php">
<input type="text" size="22" name="first_name" id="first_name" value=""></td>
<input type="text" size="22" name="email" id="email" value=""><br />
<input name="Submit" type="button" onClick="all_fields();if (validate_form()) document.form1.submit();">
</form>
It works fine with this validation script:

Code:
<script type="text/JavaScript">
function validate_form(){
err = 'The following fields are not correct filled:\n';
if (document.form1.first_name.value == ''){
	err += 'No First Name.\n';
}
if (emailCheck(document.form1.email.value) == false){
	err += 'No Valid email.\n';
}
if (err != 'The following fields are not correct filled:\n'){
	alert (err);
	return false;
}
else{
		return true;
	}
}
var str_vars = '';
function all_fields(){
	str_vars = '';
	el = document.form1;
	for (var i = 0; i < el.elements.length; i++) {
		if (el.elements[i].value != '')
	  	str_vars += el.elements[i].name+'='+el.elements[i].value+'&';
	}
	str_vars = str_vars.substr(0,str_vars.length-15);;
}
</script>
Instead of the standard grey submit button, I am trying to get an image as the submit button in the form. Here is the modified version of the form, with the new submit image. Although this approach has worked in other forms for me, it is not working here and I can't figure out what to do to make it work properly. The form will submit fine, but if form is submitted without everything being entered properly, it redirects to the next page and gives an error message. Does anyone have a clue how to code the submit string so it will work properly? Thank you for any suggestions.

Code:
<form id="form1" name="form1" method="POST" action="do.signup.php">
<input type="text" size="22" name="first_name" id="first_name" value=""></td>
<input type="text" size="22" name="email" id="email" value=""><br />
<input type="image" src="/images/buttons/free-access.jpg" height="105" width="300" border="0" alt="Click for Free Sign Up!" onClick="all_fields();if (validate_form()) document.form1.submit();">
</form>
andrew55 is offline   Reply With Quote
Old 09-18-2012, 10:34 PM   PM User | #2
Sammy12
Registered User

 
Join Date: Jun 2011
Posts: 1,063
Thanks: 12
Thanked 241 Times in 240 Posts
Sammy12 is on a distinguished road
Why not just use a background to a standard input[type="submit"]?

Code:
.submit-button {
    background: url('/images/buttons/free-access.jpg');
}
Code:
<input type="submit" class="submit-button" value="Submit" />
There have been so many complaints, bugs, and browser inconsistencies with input[type="image"], I would advise you avoid it.

Last edited by Sammy12; 09-18-2012 at 10:39 PM..
Sammy12 is offline   Reply With Quote
Old 09-18-2012, 10:53 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You shouldn't be using onclick on the submit button either - you should use onsubmit on the form tag instead as there are ways to submit a form that do not involve the submit button at all.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 09-19-2012, 12:41 AM   PM User | #4
andrew55
New Coder

 
Join Date: Mar 2012
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
andrew55 is an unknown quantity at this point
Sammy12, thank you. This seems to work so far, a variation of what you suggested:

Code:
<input name="Submit" class="submit-button" onClick="all_fields();if (validate_form()) document.form1.submit();">
felgall: it's not my usually style of creating a form. It's a script which I didn't create and it is set up in this way. I will try to see if I can recode it though it may be over my head.
andrew55 is offline   Reply With Quote
Old 09-19-2012, 03:14 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by andrew55 View Post

felgall: it's not my usually style of creating a form. It's a script which I didn't create and it is set up in this way. I will try to see if I can recode it though it may be over my head.
Simply move the onclick code from the button to the form tag and then change onclick to onsubmit.

So:

Code:
onClick="all_fields();if (validate_form()) document.form1.submit();"
gets deleted from the button and

Code:
onsubmit="all_fields();if (validate_form()) document.form1.submit();return false;"
gets added to the form tag.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

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

Forum Jump


All times are GMT +1. The time now is 11:14 AM.


Advertisement
Log in to turn off these ads.