Multiple Actions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamesm
Forum Newbie
Posts: 6
Joined: Sun Aug 18, 2002 1:47 am
Location: UK

Multiple Actions

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 :(
jamesm
Forum Newbie
Posts: 6
Joined: Sun Aug 18, 2002 1:47 am
Location: UK

Post by jamesm »

Thanks for the quick responses, appreciated.
Post Reply