Ok, I know this has to be simple. My Javascript skills are very weak (will become obvious in the code.) What I'm trying to do is use two Submit buttons in a form. The called function I want to send one named variable if one Submit is clicked and another named variable if the other button is clicked. The examples I've seen make it simple -- just use two destination PHP files. But isn't it possible to send just post ONE (the name connected to the button clicked) to the PHP file to use?
I know the PHP has to be set up, ready to Post whatever comes it's way (using variable would be really neat but can't transfer a JS variable to PHP file I guess).
What happens below is that no matter WHICH button is pushed, both seem to be sent.

<form method="post" name="frm1" onSubmit="javascript: decide_action();" action="">
	<input type="text" name="firstname"><br>
	<input type="text" name="lastname"><br>	

	<INPUT TYPE="SUBMIT" name="submit" onClick="document.pressed=this.value" VALUE="First">
	<INPUT TYPE="SUBMIT" name="submit" onClick="document.pressed=this.value" VALUE="Last">
</form>

<SCRIPT language="JavaScript">
function decide_action() {
x = document.pressed
	if (x == "First") 
		{
		myname =document.forms['frm1'].elements['firstname'].value;
		document.frm1.action ="two.php";		
		}
	else if (x == "Last") 
		{
		myname=document.forms['frm1'].elements['lastname'].value;	
		document.frm1.action ="two.php";
		}
}  	 
</SCRIPT>

CAN this work?
Thanks for any advice (aside from "take up piano") will be appreciated.
-- Mike

Recommended Answers

All 2 Replies

Why not decide what value to choose after you submit the page ? Check this for example.

<?php
if(isset($_POST['submit1'])) {
	print $_REQUEST['firstname'];
} 
if(isset($_POST['submit2'])) {
	print $_REQUEST['lastname'];
}
?>
<html>
<head>
<SCRIPT language="JavaScript">
function decide_action() {
x = document.pressed
	if (x == "First") 
		{
		myname =document.forms['frm1'].elements['firstname'].value;
		document.frm1.action ="test.php";		
		}
	else if (x == "Last") 
		{
		myname=document.forms['frm1'].elements['lastname'].value;	
		document.frm1.action ="test.php";
		}
}  	 
</SCRIPT>
</head>
<body>
<form method="post" name="frm1" onSubmit="javascript: decide_action();">
<input type="text" name="firstname"><br>
<input type="text" name="lastname"><br>
<INPUT TYPE="SUBMIT" name="submit1" onClick="document.pressed=this.value" VALUE="First">
<INPUT TYPE="SUBMIT" name="submit2" onClick="document.pressed=this.value" VALUE="Last">
</form>
</body>
</html>

If this isn't what you are looking for, then I am sorry ! I didn't get your question right.

Wow! It worked. Duh! So simple (but then things are always simple if you understand them). Yes, you were right -- make the choice at the php server end. I put your php code in the destination php file, set the part I wanted to extract as a variable, $sentname, and just picked that up afterward. Now I have the data in the $sentname variable and can use it as needed. I was trying to do it all from JS and going nuts! Thanks for the quick reply and easy solution!!
-- Mike

<?php
	if(isset($_POST['submit1'])) {	
		[B]$sentname [/B]= $_REQUEST['firstname'];
	}		 
	if(isset($_POST['submit2'])) {
		[B]$sentname [/B]= $_REQUEST['lastname'];
	}
?>
<?php
echo ("Hello! ") . [B]$sentname[/B];
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.