提交引导程序表单不起作用

时间:2015-07-07 18:11:25

标签: php html forms twitter-bootstrap email

我尝试使用php提交我的Bootstrap表单,以便输入表单的数据通过电子邮件发送给我。某些地方的东西不起作用,不幸的是我的PHP知识缺乏(到目前为止,网络搜索没有任何用处)。

当您单击提交时,表单所在的模式会立即关闭(默认的HTML内容?)。如果您将表单留空,则重新打开模式会显示表单错误消息。如果您提交包含所有内容的表单,则不会发生任何事情。

所以我的问题是:我做错了什么,如何修复它以收发邮件?另外,在提交内容但未完成时,我可以停止关闭模式吗?

编辑:我已实施"更正"用于上传图像的php,但即使它们有效,它也会给我一个无效的文件类型。 echo $target_file仅返回file_dir,表示无法确定文件名称。我做错了什么?

这是php:

<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$location = $_POST['location'];
$desc = $_POST['desc'];

//Image upload
$target_dir = "/img/gallery/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["file"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}

// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists. Please rename your image.";
    $uploadOk = 0;
}

// Check file size
if ($_FILES["file"]["size"] > 500000) {
    echo "Sorry, your file is too large. Google how to reduce an image size or contact us if you need help with this.";
    $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

$from = 'Gallery Upload Form';
$to = 'email@example.com'; //I've only changed this for stackoverflow
$subject = 'New Gallery Image';

$body = "Name: $name\n Location: $location\n Description: $desc\n \n $file";

if (!$_POST['name']) {
    $errName = 'Please enter your name';
}

if (!$_POST['location']) {
    $errLocation = 'Please enter where your image was taken';
}

if (!$_POST['desc']) {
    $errDesc = 'Please enter a description of your image';
}

if (!$_POST['file']) {
    $errFile = 'Please upload your image file';
}

// If there are no errors, send the email
if (!$errName && !$errName && !$errLocation && !$errDesc && $errFile) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! Your image has been submitted.</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later or report this to us.</div>';
    }
}
}
?>

这是HTML:

<div class="box">
        <h2>Upload your picture</h2>
        <p>Upload your picture and we'll upload it to the gallery. The best picture each month will be featured as the page background. You can also upload your pictures to our Facebook page with
          <font color="darkcyan">#gallery</font>.</p>
        <br>
        <!-- model content -->
        <!-- Button trigger modal -->
        <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModalNorm">Upload a Picture</button>

        <!-- Modal -->
        <div class="modal fade" id="myModalNorm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <!-- Modal Header -->
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                  <span aria-hidden="true">&times;</span>
                  <span class="sr-only">Close</span>
                </button>
                <h3 class="modal-title" id="myModalLabel">Upload Image</h3>
              </div>

              <!-- Modal Body -->
              <div class="modal-body">
                <form role="form" method="post" action="index.php">
                  <div class="form-group">
                    <label for="name">Your Name</label>
                    <input class="form-control" placeholder="John Smith" type="text" name="name" required value="<?php echo htmlspecialchars($_POST['name']); ?>">
                    <?php echo "<p class='text-danger'>$errName</p>";?>
                  </div>
                  <div class="form-group">
                    <label for="location">Location</label>
                    <input class="form-control" placeholder="South-west bank of Trout Pool." type="text" name="location" value="<?php echo htmlspecialchars($_POST['location']); ?>">
                    <?php echo "<p class='text-danger'>$errLocation</p>";?>
                  </div>
                  <div class="form-group">
                    <label for="desc">Picture Description</label>
                    <textarea class="form-control" name="desc" rows="3" placeholder="A picture of a 6lbs trout, my biggest all season."><?php echo htmlspecialchars($_POST['desc']); ?></textarea>
                    <?php echo "<p class='text-danger'>$errDesc</p>";?>
                  </div>
                  <div class="form-group">
                    <label for="btn-upload">Upload Image File</label>
                    <input type="file" id="exampleInputFile" name="file" value="<?php echo htmlspecialchars($_POST['file']); ?>">
                    <?php echo "<p class='text-danger'>$errFile</p>";?>
                  </div>
                  <input id="submit" name="submit" type="submit" value="Upload" class="btn btn-primary">
                </form>
              </div>

              <!-- Modal Footer -->
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              </div>
            </div>
          </div>
        </div>
        <?php echo $result; ?>
      </div>

编辑:愚蠢的问题 - 您是否必须为服务器端配置任何内容才能发送电子邮件?

1 个答案:

答案 0 :(得分:2)

您上传文件的html表单必须包含enctype="multipart/form-data" attribute.Use $ _FILES变量用于访问有关上传文件的信息。

查看这些链接,了解有关$ _FILES

的更多信息

http://php.net/manual/en/reserved.variables.files.php

http://www.tutorialsscripts.com/php-tutorials/php-files-variable.php

相关问题