提交表单csv文件保存后,我想重定向另一个网站

时间:2019-07-31 06:21:49

标签: php redirect export-to-csv

我有一个表单设置和一个php文件(如下所示),我已将数据保存在csv文件中以验证输入,然后重定向到其他网站(index.html)。验证和csv导出可以完美地工作,但是我无法弄清楚如何使表单重定向到所需的页面,而不仅仅是显示回邮。

<?php
//index.php
$error = '';
$name = '';
$email = '';
$phone = '';
$message = '';

function clean_text($string)
{
 $string = trim($string);
 $string = stripslashes($string);
 $string = htmlspecialchars($string);
 return $string;
}

if(isset($_POST["submit"]))
{
 if(empty($_POST["name"]))
 {
  $error .= '<p><label class="text-danger">Please Enter your Name</label></p>';
 }
 else
 {
  $name = clean_text($_POST["name"]);
  if(!preg_match("/^[a-zA-Z ]*$/",$name))
  {
   $error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>';
  }
 }
 if(empty($_POST["email"]))
 {
  $error .= '<p><label class="text-danger">Please Enter your Email</label></p>';
 }
 else
 {
  $email = clean_text($_POST["email"]);
  if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
   $error .= '<p><label class="text-danger">Invalid email format</label></p>';
  }
 }
 if(empty($_POST["phone"]))
 {
  $error .= '<p><label class="text-danger">phone is required</label></p>';
 }
 else
 {
  $phone = clean_text($_POST["phone"]);
 } 

  if(empty($_POST["message"]))
 {
  $error .= '<p><label class="text-danger">Message is required</label></p>';
 }
 else
 {
  $message = clean_text($_POST["message"]);
 }
 if($error == '')
 {
  $file_open = fopen("enquiry_form_data.csv", "a");
  $no_rows = count(file("enquiry_form_data.csv"));
  if($no_rows > 1)
  {
   $no_rows = ($no_rows - 1) + 1;
  }
  $form_data = array(
   'sr_no'  => $no_rows,
   'name'  => $name,
   'email'  => $email,
   'phone'  => $phone,
   'message' => $message
  );
  fputcsv($file_open, $form_data);
  $error = '<label class="text-success">Thank you for contacting us</label>';
  $name = '';
  $email = '';
  $phone = '';
  $message = '';
 }
}

?>
<!DOCTYPE html>
<html>
<head>
<title>How to Store Form data in CSV File using PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container"> <br />
  <div class="col-md-6" style="margin:0 auto; float:none;">
    <form method="post">
      <h3 align="center">Find your dream Holiday today!</h3>
      <br />
      <?php echo $error; ?>
      <div class="form-group">
        <label>Enter Name</label>
        <input type="text" name="name" placeholder="Enter Name" class="form-control" value="<?php echo $name; ?>" />
      </div>
      <div class="form-group">
        <label>Enter Email</label>
        <input type="text" name="email" class="form-control" placeholder="Enter Email" value="<?php echo $email; ?>" />
      </div>
      <div class="form-group">
        <label>Enter phone</label>
        <input type="text" name="phone" class="form-control" placeholder="Enter phone" value="<?php echo $phone; ?>" />
      </div>
      <div class="form-group">
        <label>Enter Message</label>
        <textarea name="message" class="form-control" placeholder="Enter Message"><?php echo $message; ?></textarea>
      </div>
      <div class="form-group" align="center">
        <input type="submit" name="submit" class="btn btn-info" value="Submit" />
      </div>
    </form>
  </div>
</div>
</body>
</html>

我有一个表单设置和一个php文件(如下所示),我已将数据保存在csv文件中以验证输入,然后重定向到其他网站(index.html)。验证和csv导出可以完美地工作,但是我无法弄清楚如何使表单重定向到所需的页面,而不仅仅是显示回邮。

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

<?php 
header('Location: http://www.example.com/');
?>