Jump to content

How do you redirect to another php script.


daydreamer

Recommended Posts

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.

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 :)

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?>
<?
}
?>

thanks just what i need  ;D 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!

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 :)

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.