Php form redirect

Hi, I have a standard form that a user fills out. I want the results to be sent to the email as normal but then i want to redirect to another page depending on the amount.

For example in the amount box if the amount is up to 20 then email and redirect to pay1.php

if the amount is from 20 to 40 email and redirect to pay2.php

if the amount is from 40 to 60 email and redirect to pay3.php

if the amount is from 60 to 80 email and redirect to pay4.php

if the amount is from 80 to 100 email and redirect to pay5.php

if the amount is over 100 email and redirect to pay6.php.

In my html form i have an input box that says amount. Here is my php code at the moment.

<?php
if (session_id() == "")
{
   session_start();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['formid'] == 'form1')
{
   if (isset($_POST['captcha_code'],$_SESSION['random_txt']) && md5($_POST['captcha_code']) == $_SESSION['random_txt'])
   {
      unset($_POST['captcha_code'],$_SESSION['random_txt']);
   }
   else
   {
      echo '<b>The entered code was wrong.</b><br>';
      echo '<a href="javascript:history.back()">Go Back</a>';
      exit;
   }
}
?>
<?php
function ValidateEmail($email)
{
   $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
   return preg_match($pattern, $email);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['formid']) && $_POST['formid'] == 'form1')
{
   $mailto = '[email protected]';
   $mailfrom = isset($_POST['email']) ? $_POST['email'] : $mailto;
   $subject = 'Invoice Man Form Option 1';
   $message = 'Invoice Man Form Option 1';
   $success_url = '';
   $error_url = '';
   $error = '';
   $eol = "\n";
   $boundary = md5(uniqid(time()));

   $header  = 'From: '.$mailfrom.$eol;
   $header .= 'Reply-To: '.$mailfrom.$eol;
   $header .= 'MIME-Version: 1.0'.$eol;
   $header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol;
   $header .= 'X-Mailer: PHP v'.phpversion().$eol;
   if (!ValidateEmail($mailfrom))
   {
      $error .= "The specified email address is invalid!\n<br>";
   }

   if (!empty($error))
   {
      $errorcode = file_get_contents($error_url);
      $replace = "##error##";
      $errorcode = str_replace($replace, $error, $errorcode);
      echo $errorcode;
      exit;
   }

   $internalfields = array ("submit", "reset", "send", "filesize", "formid", "captcha_code", "recaptcha_challenge_field", "recaptcha_response_field", "g-recaptcha-response");
   $message .= $eol;
   $logdata = '';
   foreach ($_POST as $key => $value)
   {
      if (!in_array(strtolower($key), $internalfields))
      {
         if (!is_array($value))
         {
            $message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;
         }
         else
         {
            $message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;
         }
      }
   }
   $body  = 'This is a multi-part message in MIME format.'.$eol.$eol;
   $body .= '--'.$boundary.$eol;
   $body .= 'Content-Type: text/plain; charset=ISO-8859-1'.$eol;
   $body .= 'Content-Transfer-Encoding: 8bit'.$eol;
   $body .= $eol.stripslashes($message).$eol;
   if (!empty($_FILES))
   {
       foreach ($_FILES as $key => $value)
       {
          if ($_FILES[$key]['error'] == 0)
          {
             $body .= '--'.$boundary.$eol;
             $body .= 'Content-Type: '.$_FILES[$key]['type'].'; name='.$_FILES[$key]['name'].$eol;
             $body .= 'Content-Transfer-Encoding: base64'.$eol;
             $body .= 'Content-Disposition: attachment; filename='.$_FILES[$key]['name'].$eol;
             $body .= $eol.chunk_split(base64_encode(file_get_contents($_FILES[$key]['tmp_name']))).$eol;
          }
      }
   }
   $body .= '--'.$boundary.'--'.$eol;
   if ($mailto != '')
   {
      mail($mailto, $subject, $body, $header);
   }
   header('Location: '.$success_url);
   exit;
}
?>

Your consecutive and multiple page redirects points to a design flaw. Are all those pages really that different or is just the data displayed different?

hi sorry i am new to all this i will try to make it simpler. If i have a form input called amount and i have a success page called say thanks.php once the form has been submitted to my email how do i pass the amount to my thanks.php page and then depending on the amount redirect to another page ?

For example if someone entered 20 in the amount how do i show this in my thanks page and then redirect to pay1.php ?

Or if someone entered 40 in the amount it shows 40 in the thanks page and redirects to pay2.php and so on ?

I get what you want to do. What I am saying is, there seems to be a flaw in your logic.

What is different on the redirect pages? If your thanks page is redirecting nobody is going to see anything on your thanks page.

1 Like

Well, you have this set up in your code. Just change it depending on the value.
If ($amount=>20 AND $amount<40) {
$success_url = “pay2.php”;
} elseif ($amount=>40 AND $amount<60) {
$succcess_url=“pay3.php”;
etc…
Or, you could set up an array of pages and handle it in some sort of calculation.

But, as the others have mentioned, it is a bad design. Normally you would go to the same page and in that page, sort out the details of what is displayed using the amount there. So, you would pass the amount in a SESSION variable and in the pay.php file, you would load it and adjust the output of the pay.php page depending on the value of the amount. You would not duplicate many pages. It is not useful to waste resources on a site for multiple pages which are similar in use. Does that make sense?

1 Like

if there is an easier way to do it the difference on the redirect pages is a price. For example if its up to 20 then it would say for example be ÂŁ1 if it was 20 to 40 then it would say for example be ÂŁ2 etc

// thanks.php?amount=20

$amount = (int)$_GET['amount'] ?? 0;
if($amount > 40) $price = 1;
elseif($amount > 20) $price = 2;
else $price = 3;

I sure hope that is a school assignment, because that discount is definitely backwards!

Sponsor our Newsletter | Privacy Policy | Terms of Service