daydreamer Posted March 7, 2008 Share Posted March 7, 2008 I want to redirect to another script on my server so that can run. Ive looked at header('Location: http://www.example.com/'); , but in the php manual it says you need to have it before any html tags. The page I want to use it on is a html form, with php parts in it. I have a two part user registration. They go to first php page, enter stuff, then I want to redirect them to the second php file after they click Continue and ive processed some input. Also is there any way I can pass a variable to the next script? Or, how can I use a switch statement, but keep the case variable when the php file reloads? (because I am using action="<?php echo $_SERVER['PHP_SELF']; ?>"> to process and output a html form in the same html file). Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/ Share on other sites More sharing options...
revraz Posted March 7, 2008 Share Posted March 7, 2008 If you have output, use HTML redirect. If you want to pass a variable to another script, use either POST, GET or Sessions. Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-486158 Share on other sites More sharing options...
laffin Posted March 7, 2008 Share Posted March 7, 2008 Form Page (Submit Button) -> Processing Page (use header redirection) -> Display Page To Do this all in one page, u will check $_POST statements of $_SERVER['REQUEST_METHOD'] <?php if($_SERVER['REQUEST_METHOD']=='POST') { $username=$_POST['name']; // process your variables here } ?> <FORM METHOD='POST'> Username: <INPUT TYPE="TEXT" name='name' <?= (!empty($username)?"value=\"$username\"":'')?>><BR> <INPUT TYPE='SUBMIT'> </FORM> Very simple example of an all in one script Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-486172 Share on other sites More sharing options...
daydreamer Posted March 7, 2008 Author Share Posted March 7, 2008 <?php if($_SERVER['REQUEST_METHOD']=='POST') ?> ok thanks.. so what does this do exactly? ??? does it return true if anything has been posted, and false if nothing has yet been posted? Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-486221 Share on other sites More sharing options...
daydreamer Posted March 7, 2008 Author Share Posted March 7, 2008 I just looked it up, but doesn't it perform the same function as <?php if(!isset($_POST['submit']) ?> which im using in a similar/same way as your example? But my problem is I have two forms to show in one php file. Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-486248 Share on other sites More sharing options...
laffin Posted March 7, 2008 Share Posted March 7, 2008 just different ways accomplishing the same thing. It's not a big issue really 1 or 10 forms, ya need an identifier of which form was sent In this sample using a hidden field to progress thru the forms <?php if($_SERVER['REQUEST_METHOD']=='POST') // Form was posted { $formid=0+$_POST['formid']; switch($formid) { case '2': // Process 2nd form $email=$_POST['email']; if(empty($email)) $showform=2 else $showform=3; case '1': // Process 1st form $username=$_POST['name']; if(empty($username)) $showform=1; elseif($showform!=3) $showform=2; // Did we process form 2 already? break; default: // Unknown form info Show 1st Form $showform=1; } } else $showform=1; // Display different forms if($showform==1) { ?> <FORM METHOD='POST'> <INPUT TYPE='HIDDEN' name='formid' value='1'> Username: <INPUT TYPE="TEXT" name='name' <?= (!empty($username)?"value=\"$username\"":'')?>><BR> <INPUT TYPE='SUBMIT'> </FORM> <? } elseif($showform=='2') { ?> <FORM METHOD='POST'> <INPUT TYPE='HIDDEN' name='formid' value='2'> <INPUT TYPE='HIDDEN' name='username' value='<?=$username?>'> Email: <INPUT TYPE="TEXT" name='email' <?= (!empty($email)?"value=\"$email\"":'')?>><BR> <INPUT TYPE='SUBMIT'> </FORM> <? } else { // Form 1 & 2 Processed already ?> <H1>Hello <?=$username?>,</H1><br> I see yer email is <?=$email?> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-486323 Share on other sites More sharing options...
daydreamer Posted March 7, 2008 Author Share Posted March 7, 2008 thanks just what i need works perfect. why didnt i think of that?! One other thing.. I have this code to collect my form variables: <?php foreach($_POST as $name => $value){ $vars .= "&" . $name . "=" . $value; } ?> how would i exclude the "formid=2&" from the string, or prevent it from being put into the $vars string all together? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-486475 Share on other sites More sharing options...
BlueSkyIS Posted March 7, 2008 Share Posted March 7, 2008 <?php foreach($_POST as $name => $value){ if ($name == "formid") { continue; } $vars .= "&" . $name . "=" . $value; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-486479 Share on other sites More sharing options...
daydreamer Posted March 7, 2008 Author Share Posted March 7, 2008 Hey thanks alot. going to look up in the php manual how exactly it works Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-486500 Share on other sites More sharing options...
laffin Posted March 8, 2008 Share Posted March 8, 2008 I prefer sumthing like <?php $reqfields=array('username','email','password'); // Required fields $urqfields=array('formid'); // Unecessary fields foreach($_POST as $key => $val) // Grab all fields except Unecessary fields { if(array_search($urqfields,$key)===false) $args[$key]="$key=". urlencode($val); } $errors=0; foreach($reqfields as $val) // Check Required Fields { if(!isset(args[$key])) { echo "Required field ($val) missing"; $errors++; } } if($errors) exit; $arg=implode('&',$args); // Build our args line ?> using something like this avoids the possible malformed url of http://my.site.com/script.php?&arg1=x&arg2=y when building it Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-486678 Share on other sites More sharing options...
daydreamer Posted March 8, 2008 Author Share Posted March 8, 2008 hmm, a bit more code than i need to simply exclude &formid=2 ! but i might use that sort of method to validate my form, because I got alot of fields to check! looks easier than writing out each array key and then checking each value, may as well use a foreach loop like u have. thanks 4 code Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-487084 Share on other sites More sharing options...
laffin Posted March 8, 2008 Share Posted March 8, 2008 Exactly Might be a bit more code for small stuff, but when ya got forms with 10+ inputs and half are required, it just makes validating so much easier Quote Link to comment https://forums.phpfreaks.com/topic/94910-how-do-you-redirect-to-another-php-script/#findComment-487099 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.