在成功填写表单后,如何从php重定向到不同的页面

时间:2017-11-28 12:50:27

标签: php forms redirect

我有这个问题。我制作了一个包含php的bootstrap表单,如果表单已填写并成功发送,我需要将其重定向到另一个页面(.html),而不是显示消息。

这是表单的PHP页面:

<?php
/*
 *  CONFIGURE EVERYTHING HERE
 */

// an email address that will be in the From field of the email.
$from = 'email@email.com';

// an email address that will receive the email with the output of the form
$sendTo = 'email@mail.cz';

// subject of the email
$subject = 'Nová zpráva z Vašeho webu';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 
'Phone', 'email' => 'Email', 'message' => 'Message'); 

// message that will be displayed when everything is OK :)
$okMessage = 'Děkuji! Vaše zpráva byla úspěšně odeslána. ';

// If something goes wrong, we will display this message.
$errorMessage = 'Omlouvám se, ale došlo k nečekaně chybě. Zkuste to prosím 
později.';

/*
 *  LET'S DO THE SENDING
 */

// if you are not debugging and don't need error reporting, turn this off by 
error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{

if(count($_POST) == 0) throw new \Exception('Form is empty');

$emailText = "You have a new message from your contact form\n=============================\n";

foreach ($_POST as $key => $value) {
    // If the field exists in the $fields array, include it in the email 
    if (isset($fields[$key])) {
        $emailText .= "$fields[$key]: $value\n";
    }
}

// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
    'From: ' . $from,
    'Reply-To: ' . $from,
    'Return-Path: ' . $from,
);

// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));

$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);

header('Content-Type: application/json');

echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}

以下是表单的HTML:

      <form id="contact-form" method="post" action="contact.php">

      <div class="messages"></div>

      <div class="controls">

          <div class="row">
              <div class="col-md-6">
                  <div class="form-group">
                      <label for="form_name">Jméno *</label>
                      <input id="form_name" type="text" name="name" class="form-control" placeholder="Vaše křestní jméno *" required="required" data-error="Firstname is required.">
                      <div class="help-block with-errors"></div>
                  </div>
              </div>
              <div class="col-md-6">
                  <div class="form-group">
                      <label for="form_lastname">Příjmení *</label>
                      <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Vaše příjmení *" required="required" data-error="Lastname is required.">
                      <div class="help-block with-errors"></div>
                  </div>
              </div>
          </div>
          <div class="row">
              <div class="col-md-6">
                  <div class="form-group">
                      <label for="form_email">Email *</label>
                      <input id="form_email" type="email" name="email" class="form-control" placeholder="Váš platný email *" required="required" data-error="Valid email is required.">
                      <div class="help-block with-errors"></div>
                  </div>
              </div>
              <div class="col-md-6">
                  <div class="form-group">
                      <label for="form_phone">Telefon</label>
                      <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Váš telefon">
                      <div class="help-block with-errors"></div>
                  </div>
              </div>
          </div>
          <div class="row">
              <div class="col-md-12">
                  <div class="form-group">
                      <label for="form_message">Vaše zpráva *</label>
                      <textarea id="form_message" name="message" class="form-control" placeholder="Vaše zpráva *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
                      <div class="help-block with-errors"></div>
                  </div>
                  <br>
              </div>
              <div class="col-md-12">
                  <input type="submit" class="odeslat" value="Odeslat">
              </div>
          </div>
          <div class="row">
              <div class="col-md-12">
                  <br><p class="text-muted"><strong>*</strong> Tyto pole jsou povinná.</p>
              </div>
          </div>
      </div>

</form>     

可以帮助我解决这个问题,因此它不会只显示消息吗? 谢谢

1 个答案:

答案 0 :(得分:0)

你可以在最后写一个if句子,在真实情况下显示这个回声并且不会在虚假的情况下显示,有些像这样 - &gt;

  if(!$ejecutar)
  {
   echo "whatever you want";
  } 
  else 
  {
   echo "Whatever you want<br><a href='index.php'>Volver</a>";
  }