为什么每次刷新时,php fom提交都会继续提交?

时间:2015-09-02 06:10:03

标签: javascript php jquery html forms

所以我提交了我的表单,并且每次单击ctrl-r它都刷新页面而不提醒我它将重新提交表单。每次我点击浏览器上的重新加载按钮它都会要求我重新加载,但我已经提交了表单,所以现在我每次都会收到一个新的提交并在我的sqlDB中创建一个新项目。我刷新,我不知道我要做什么来自动刷新页面,它不会保存表单重新提交,并再次像一个新的页面。

//结束其他所在的位置是我尝试添加标题(位置:)但我收到错误。我将错误更多地发布在问题的底部。

footer.php

<?php
if(isset($_POST['submit-story'])){

    $answer = $_POST['human-story'];

    if (!ctype_digit($answer) == 8) {
      echo "Cannot store event. wrong answer";
      die();      
    } else {
    //Get the uploaded file information
    $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);
    //get the file extension of the file
    $type_of_uploaded_file =
        substr($name_of_uploaded_file,
        strrpos($name_of_uploaded_file, '.') + 1);

    $size_of_uploaded_file =
    $_FILES["uploaded_file"]["size"]/1024;//size in KBs

    //Settings
    $max_allowed_file_size = 1000000; // size in KB
    $allowed_extensions = array("jpg","jpeg","gif","bmp","mp4", "mov");

    //Validations
    if($size_of_uploaded_file > $max_allowed_file_size )
    {
      $errors = "\n Size of file should be less than $max_allowed_file_size";
    }

    //------ Validate the file extension -----
    $allowed_ext = false;
    for($i=0; $i<sizeof($allowed_extensions); $i++)
    {
      if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
      {
        $allowed_ext = true;
      }
    }

    if(!$allowed_ext)
    {
      $errors = "\n The uploaded file is not supported file type. ".
      "Only the following file types are supported: ".implode(',',$allowed_extensions);
    }

    //copy the temp. uploaded file to uploads folder
      $upload_folder = 'uploads/';
      $to = "example@gmail.com"; // this is your Email address
      $first_name = filter_var($_POST['first_name']. $schoolOfficialShortName, FILTER_SANITIZE_STRING);
      $story = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
      $eventDate = filter_var($_POST['eventDate'],FILTER_SANITIZE_STRING);
      $eventLocation = filter_var($_POST['eventLocation'],FILTER_SANITIZE_STRING);
      $subject = "blah-" . $schoolOfficialShortName . "Event";
      $title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
      $message = $first_name . " wrote the following:" . "\n\n" . $title ."<br>". $story;
      $headers = "From: $first_name";
      // boundary
      $semi_rand = md5(time());
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
      // headers for attachment
      $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
      // multipart boundary
      $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
      $message .= "--{$mime_boundary}\n";
      $content = '';
      $tmp_path = $_FILES["uploaded_file"]["tmp_name"];
        if($tmp_path) {
          $filename = $_FILES["uploaded_file"]["name"];
          $path_of_uploaded_file = $upload_folder . $filename;
          if(!copy($tmp_path, $path_of_uploaded_file))
          {
            $errors = '\n error while copying the uploaded file';
          }
          $file_size = filesize($path_of_uploaded_file);
          $handle = fopen($path_of_uploaded_file, "rb");
          $content = fread($handle, $file_size);
          fclose($handle);
          $content = chunk_split(base64_encode($content));
        }
        // if attachment has successfully encoded
        if ($content) {
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"{$filename}\"\n" .
        "Content-Disposition: attachment;\n" . " filename=\"{$filename}\"\n" .
        "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
        $message .= "--{$mime_boundary}\n";
        }
        $headers2 = "From:" . $to;
        mail($to, $subject, $message, $headers);
        //send items to S3 bucket
        move_uploaded_file($upload_folder, $filename);

        //store image to s3 bucket
        try {
            $result = $s3->putObject([
              'Bucket' => $config['s3']['bucket'],
              'Key' => "uploads/{$name_of_uploaded_file}",
              'Body' => fopen($path_of_uploaded_file, 'rb'),
              'ACL' => 'public-read'
            ]);

            $results = $result['ObjectURL'];

            if (is_uploaded_file($_FILES['uploaded_file']['tmp_name'])){
                  //inserts in pending table
                  $sql = "INSERT INTO items (photo,title,eventDate,description,name,pLike,pDislike,address) VALUES ( :photo, :title, :eventDate, :description, :name, :pLike, :pDislike, :address)";
                  $stmt = $pdo->prepare($sql); 
                  $stmt->bindParam(':title', $title);
                  $stmt->bindParam(':photo', $results);
                  $stmt->bindParam(':description', $story);
                  $stmt->bindParam(':eventDate', $eventDate);
                  $stmt->bindParam(':address', $eventLocation);
                  $stmt->bindParam(':name', $first_name);
                  $stmt->bindValue(':pLike', 0, PDO::PARAM_INT);
                  $stmt->bindValue(':pDislike', 0, PDO::PARAM_INT);
                  $stmt->execute();       
                }else {
                  die("<h1>There was an error. Please go back and retry.</h1>");        
                } 
                //remove the pending file in uploads folder
                unlink($path_of_uploaded_file);

        } catch(PDOException $e){
            echo 'error in query:  '.$e->getMessage();
        }
      };// end else 
};// end isset final
?>

我试过添加一个标题(位置:index.php)但是我得到一个奇怪的错误说

Cannot modify header information - headers already sent by (output started at /Users/mine/Documents/www/website/schools/inc/header.php:67) in /Users/mine/Documents/www/website/schools/inc/footer.php on line 127

2 个答案:

答案 0 :(得分:1)

  1. 在footer.php中处理数据后取消设置$ _POST变量

  2. 您的标题已发送问题:这意味着已经输出了一些文字。将标题()放在脚本的顶部。或者查看ob_start和ob_end_clean()函数。

答案 1 :(得分:1)

一旦将文本输出到浏览器,就无法使用header()。由于您的头文件可能包含输出HTML,因此无法使用header()。

你可以用流动的方式解决这个问题:

  1. 将if语句移到头文件include上方。
  2. 脚本顶部的
  3. ob_start()缓冲输出。