Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic post in form

Status
Not open for further replies.

work4free

Technical User
Jul 22, 2004
21
US
i was wondering how to create a dynamic post action in a form using php. i have several radio buttons on this form with values from A to H. if the user selects A, they should be directed to page2.php, however any other option should take them to more.php. i have done an "if/else" like this:
Code:
<?PHP if ($Q1 == "A")
	{
	$answer="page2.php";
	}
	else
	{
	$answer="more.php";
	}
?>
and then in the form i did this:
Code:
<form name="form1" method="post" action="<?PHP echo $answer;  ?>">

the problem is, since the php is processed before the page loads, the action in the form tag is always more.php and i am never able to get to page2.php. (because Q1 is never equal to A when the page loads.)

how do i dynamically set the action on the form tag?
 
Use JavaScript:

Code:
<script type="text/JavaScript">
<!--
var answer="";

if (document.form1.Q1.value == "A"){
    answer="page2.php";
} else {
    answer="more.php";
}

document.form1.action=answer;

?>

then the html form

Code:
<form name="form1" method="post" action="more.php" onsubmit="setAction();">

Should work.

Rick

 
i was using php, and not javascript, because i do not want them to be able to "decipher", or see, what the url's are before they make their selection. by using javascript they can view the source code and view the url's. they need to be hidden.

any other thoughts?
 
Take all the values into the pages and them redirect to which ever page you need based on those values

Code:
if ($_POST['Q1']=="A"){
  header ("location:page2.php");
}else{
  header("location:more.php");
}

Not my preferred method though, depending on what the page does, I would do any db processing on this page and then pass the record id to the next page(s) for further processing. Another option is to place the code from both pages into functions and call the function based on what happens as above...

Bastien

Cat, the other other white meat
 
Would this work?

<?php
if ($_POST["Q1"] == "A") {
include "page2.php";
} else {
include "more.php";
}

?>

 
Here's an all PHP solution:

Leave your action alone. When the form processing script starts, do the following:
Code:
<?php
 session_start();
 if ($_POST['Q1'] == 'A') {
    $_SESSION['my_post'] = serialize[$_POST];
    location('header: [URL unfurl="true"]http://www.yoursite.com/page2.php');[/URL] }
/* drop in to your other processing */

In page2.php:
Code:
<?php
 session_start();
 $my_post = unserialize($_SESSION['my_post']);
/* continue with your processing, using $my_post instead of $_POST to get your information */

I haven't checked the code for errors, so use this as a guideline.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top