954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Javascript Button Function

Hi All,

Just a quick one,

My code (Below) works in Internet Explorer, but not in Chrome, Safari or Mozilla Firefox. Not sure why, but would it be possible for someone to point me in the right direction?

Thanks!

Code attached;

<script language="javascript">

function formAction(flag) {
	switch (flag){
		case "create": 
			document.forms.useraction.action = "/apps/portal_admin/create_user1.php";
			break;
		case "modify":
			document.forms.useraction.action = "/apps/portal_admin/modify_user1.php";
			break;
		case "copy":
			document.forms.useraction.action = "/apps/portal_admin/copy_user1.php#user2";
			break;
	}
	document.forms.useraction.submit();
}
function helloworld() {
	alert("Hello World");
}
</script>
<?php
	if ($authlib->auth_authobj($posting_userid,$_COOKIE['shost_app_id'],'002')>=2) {
		echo "<input type=\"button\" value=\"Create User\" class=\"formButton\" name=\"create\" id=\"create\" onClick=\"formAction('create')\">\n";
		echo "<input type=\"button\" value=\"Copy User\" class=\"formButton\" name=\"copy\" onClick=\"formAction('copy')\">\n";
	}
	if ($authlib->auth_authobj($posting_userid,$_COOKIE['shost_app_id'],'002')>=3) {
		echo "<input type=\"button\" value=\"Edit User\" class=\"formButton\" name=\"modify\" onClick=\"formAction('modify')\">\n";
		echo "<input type=\"button\" value=\"This button works\" class=\"formButton\" name=\"Working\" onClick=\"helloworld()\">\n";
	}
?>


Everything else in my file works just fine, including my testing button - but I don't know whether my Javascript function for "formAction(flag)" works properly. (Even though it does on IE7/8/9.

Anybody have any suggestions?

Thanks!

Coyx
Newbie Poster
23 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
 

I assume that you have only one form on page,
so to solve your problem, change all

document.forms.useraction.action

to

document.useraction.action
urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

Thanks for your prompt reply Urtrivedi!

Unfortunately, that didn't work either. It seems that (After placing an Alert right after the function call before the "switch(flag)") - and testing again, it didn't even pop the alert.

function formAction(flag) {
	alert("Testing");
	switch (flag){
		
		case "create": 
			document.useraction.action = "/apps/portal_admin/create_user1.php";
			break;
		case "modify":
			document.useraction.action = "/apps/portal_admin/modify_user1.php";
			break;
		case "copy":
			document.useraction.action = "/apps/portal_admin/copy_user1.php#user2";
			break;
	}
	document.useraction.submit();
}
Coyx
Newbie Poster
23 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
 

I don't know if this will help, but the Chrome Javascript Error Console states;

Uncaught TypeError: string is not a function (anonymous function)list_user1.php:469 onclick
Coyx
Newbie Poster
23 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
 

HAH! Found it. Right on the mark to go home too. :P

Okay, it turns out that "formAction" is a reserved handler... and therefore, back in the day when this was just the name of my function and it worked... it's been fine, but since HTML5 made it a reserved handler... it wouldn't work.

I simply added a "z" to the end of each place that calls the function, then added a "z" to the end of the function name, and hey presto, it works in every browser.

WOOOO.

Coyx
Newbie Poster
23 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: