PDA

Click to See Complete Forum and Search --> : page redirect upon submission


sridharao
Jun 30th, 2009, 08:48 AM
The action component of a form can be used to redirect to a page of choice when a form is submitted.

I have a form that contains several form elements. Depending on the choices that the user makes, the page has to be directed accordingly.

For example, if "a" condition is met, the page should load abc.php and if if "b" condition is met, the page should load def.php.

How can this be achieved in php. I don't want to use javascript because that will expose the variables (abc.php?name=so&id=23).

I understand that PHP is server side script and it can't do much about this, but is there a way where out?

What about this following method?
If the condition met is "b", I direct it to abc.php because that is explicitly mentioned in action="abc.php". In the abc.php, I validate if the condition was "a" or "b". If it is "b", I use header('Location: def.php'). Would this be ideal? Even if I were to redirect the page this way, would the submitted data also be passed to def.php?

SambaNeko
Jun 30th, 2009, 09:38 AM
Is your form dealing with post or get data? Using header() will not pass along post data - though you could probably append vars to the URL for get data, if that's appropriate for you (header("Location: def.php?a=1&b=2")).

There's an inelegant solution (for post data) by which you submit the form (to "abc.php"), and if the logic determines it should go to "def.php," you write out a form (on "abc.php") with hidden fields for each of your $_POST values, and auto-submit that form to "def.php".

You could also use AJAX to call some unseen logic to determine where to send the form, but how important is it to you to have a solution that'll work sans-Javascript?

sridharao
Jun 30th, 2009, 10:51 AM
There's an inelegant solution (for post data) by which you submit the form (to "abc.php"), and if the logic determines it should go to "def.php," you write out a form (on "abc.php") with hidden fields for each of your $_POST values, and auto-submit that form to "def.php".

This appears interesting, but how do I get that form to auto-submit?

The other alternative I have is to use two separate forms, each when submitted directs to the desired page.

kows
Jun 30th, 2009, 03:07 PM
I'd suggest doing what you mentioned in the original post and just storing the data in a session while you figure out where the user needs to be directed. if there was a radio selection on your form named "action," (with available options A or B) the following might work. the switch() in this case is the condition check that defines $url, and you can modify it however you'd like to produce the correct $url value.

<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == "POST"){

//store data in a session
foreach($_POST as $key => $value){
$_SESSION[$key] = $value;
}

//which action did they choose?
switch($_POST['action']):
case "A": $url = "abc.php"; break;
case "B": $url = "def.php"; break;
endswitch;

//redirect
header("Location: $url");
}
?>
<!-- print your form -->

then, in both abc.php and def.php, you just read your session variables. you can also verify that the user SHOULD be there by checking if $_SESSION['action'] exists (and also checking its value if you'd like).

<?php
session_start();

//is an action set?
if(!isset($_SESSION['action'])){
//redirect to the original form
header("Location: form.php");
}

print_r($_SESSION);
?>

SambaNeko
Jun 30th, 2009, 10:32 PM
This appears interesting, but how do I get that form to auto-submit?

Frankly what kows suggested is a better method, but since you asked... You would echo out the form with all your variables, then use a single line of Javascript to submit the form as soon as the page loads. Like so:


<form name="myForm" action="def.php" method="post">
<input type="hidden" name="myVar1" value="myVal1"/>
<input type="hidden" name="myVar2" value="myVal2"/>
</form>

<script type="text/javascript">
<!--
window.onload = function(){document.myForm.submit();}
//-->
</script>


This of course requires Javascript to work, and outputs your form to the browser if only for a second, so the savvy of your users could see the vars if they were so inclined.

sridharao
Jul 1st, 2009, 03:09 AM
Thank you both for useful replies.