Page 1 of 1

Multiple Actions

Posted: Sat Aug 31, 2002 11:47 am
by jamesm
I would like to have several buttons, each will post data to a different URL but I cant figure out how to code the "<form action=" to accept a different URL depending on which button is pressed. I have tried stuff like:

<form name='out' method='post' action="<? echo $page_name; ?>"> but it cant pass it back in retrospect.
I can see if you are prepared to reload the form it can be done but this is clearly too clumsy.
I dont want to embed javascript as I am trying to make the transition to PHP!
Will some one point me in the right direction please.

Thanks

Posted: Sat Aug 31, 2002 11:54 am
by twigletmac
Why not post everything to one page, detect which button was pressed there and using a switch statement include a file with the coding for dealing with the information based on the button pressed?

Otherwise you are going to have to reload the form or use javascript to create the action attribute's value each time.

Because javascript is client-side and PHP is server-side they do not really replace each other as each can do things the other can't.

Mac

Posted: Sat Aug 31, 2002 12:07 pm
by gotDNS

Code: Select all

<form name='out' method='post' action="<? echo $page_name; ?>">
to:

Code: Select all

<?php
echo "<form name='out' method='post' action='$page_name' />";
?>
Try to keep every page in one php tag. Be consistent. Be neat.

Posted: Sat Aug 31, 2002 12:24 pm
by volka
but it cant pass it back in retrospect.
of course you can't.
I suppose a little misunderstanding.

Code: Select all

&lt;form name='out' method='post' action="&lt;? echo $page_name; ?&gt;"&gt;
is parsed server-side; the browser will never see it. What reaches the client always look like

Code: Select all

&lt;form name='out' method='post' action="target.php"&gt;
a form has only one target at a time, but you may alter this with a client-side script, i.e.

Code: Select all

&lt;html&gt;&lt;head&gt;&lt;script language="JavaScript"&gt;
	function submit_form(oForm, sUrl)
	&#123;
		oForm.action = sUrl;
		oForm.submit();
	&#125;
&lt;/script&gt;&lt;/head&gt;&lt;body&gt;
   &lt;form action="" method="GET"&gt;
     &lt;input type="text" name="myContent"/&gt;&lt;br/&gt;
     &lt;button onClick="submit_form(this.parentElement, 'page1.php')"&gt;page 1&lt;/button&gt;&lt;br/&gt;
     &lt;button onClick="submit_form(this.parentElement, 'page2.php')"&gt;page 2&lt;/button&gt;&lt;br/&gt;
     &lt;button onClick="submit_form(this.parentElement, 'page3.php')"&gt;page 3&lt;/button&gt;&lt;br/&gt;
   &lt;/form&gt;
&lt;/body&gt;&lt;/html&gt;
But if the user turned javascript off, nothing ever happens :(

Posted: Sat Aug 31, 2002 12:41 pm
by jamesm
Thanks for the quick responses, appreciated.