|
-
Jun 30th, 2009, 08:48 AM
#1
Thread Starter
Lively Member
page redirect upon submission
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?
Last edited by sridharao; Jun 30th, 2009 at 09:04 AM.
Save trees, avoid plastics, say no to zoo, go veg, recycle as much, live holistic
-
Jun 30th, 2009, 09:38 AM
#2
Re: page redirect upon submission
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?
-
Jun 30th, 2009, 10:51 AM
#3
Thread Starter
Lively Member
Re: page redirect upon submission
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.
Save trees, avoid plastics, say no to zoo, go veg, recycle as much, live holistic
-
Jun 30th, 2009, 03:07 PM
#4
Re: page redirect upon submission
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 Code:
<?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 Code:
<?php session_start();
//is an action set? if(!isset($_SESSION['action'])){ //redirect to the original form header("Location: form.php"); }
print_r($_SESSION); ?>
-
Jun 30th, 2009, 10:32 PM
#5
Re: page redirect upon submission
 Originally Posted by sridharao
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:
Code:
<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.
-
Jul 1st, 2009, 03:09 AM
#6
Thread Starter
Lively Member
Re: page redirect upon submission
Thank you both for useful replies.
Save trees, avoid plastics, say no to zoo, go veg, recycle as much, live holistic
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|