2015-02-08 121 views
1

我正在製作一個網站,並且無法將表單發送到PHP。這裏是我的表單代碼:HTML表單不能將數據發送到PHP

<form class="form-horizontal" method="POST" enctype="multipart/form-data"> 
<!-- 
<div class="form-group"> 
    <label for="id" class="col-sm-2 control-label">Text</label> 
    <div class="col-sm-10"> 
     <input type="text" class="form-control" name="name" id="id" placeholder="placeholder"> 
    </div> 
</div> 
--> 
<div class="form-group"> 
    <label for="name" class="col-sm-2 control-label">Name of the Program</label> 
    <div class="col-sm-10"> 
     <input type="text" class="form-control" name="name" id="name" placeholder="Name"> 
    </div> 
</div> 
<div class="form-group"> 
    <label for="lang" class="col-sm-2 control-label">What language is it in</label> 
    <div class="col-sm-10"> 
     <select multiple class="form-control" id="lang" name="lang[]"> 
      <option value="java">Java</option> 
      <option value="python">Python</option> 
      <option value="webiste">Website(HTML, PHP, CSS or Javascript)</option> 
     </select> 
    </div> 
    <span id="helpBlock" class="help-block">Hold down control to select multiple.</span> 
</div> 
<div class="form-group"> 
    <label for="desc" class="col-sm-2 control-label">Give a description about it</label> 
    <div class="col-sm-10"> 
     <textarea id="desc" name="desc" class="form-control" rows="4" placeholder="Description"></textarea> 
    </div> 
</div> 
<div class="form-group"> 
    <label for="file" class="col-sm-2 control-label">Upload the file</label> 
    <div class="col-sm-10"> 
     <input type="file" class="form-control" name="file" id="file" placeholder="File"> 
    </div> 
    <span id="helpBlock" class="help-block">If it is in multiple files put it in a ZIP file.</span> 
</div> 
<div class="form-group"> 
    <div class="col-sm-offset-2 col-sm-10"> 
     <button type="submit" name="submit" class="btn btn-default">Continue</button> 
    </div> 
</div> 

(我用系統啓動時進行形式) 這裏是我的PHP代碼:

<?php 
$error = array(); 
if(isset($_POST['submit'])){ 
    if(!isset($_POST['name']) || $_POST['name'] == ''){ 
     $error[1] = 'Please enter the name form.'; 
    } 
    if(!isset($_POST['lang']) || $_POST['lang'] == array()){ 
     $error[2] = 'Please enter the language form.'; 
    } 
    if(!isset($_POST['desc']) || $_POST['desc'] == ''){ 
     $error[3] = 'Please enter the description form.'; 
    } 
    if(!isset($_POST['file']) || $_POST['file'] == ''){ 
     $error[4] = 'Please select a file.'; 
    } 
    if(isset($error) && $error != array()){ 
     foreach($error as $e => $k){ 
      echo '<div class="alert alert-danger" role="alert"><strong>Error!</strong> ' . $k . '</div>'; 
     } 
    }else{ 
     $data = array(); 
     $data['name'] = $db->mySQLSafe($_POST['name']); 
     $data['file'] = $db->mySQLSafe(serialize($_POST['file'])); 
     $data['description'] = $db->mySQLSafe($_POST['desc']); 
     $data['author'] = $db->mySQLSafe($result[0]['full name']); 
     $data['page'] = $db->mySQLSafe('projects'); 

     $insert = $db->insert('projects', $data); 
     if($insert){ 
      $target_file = 'projects/' . basename($_FILES["file"]["name"]); 
      $fileType = pathinfo($target_file,PATHINFO_EXTENSION); 
      echo $target_file . '<br />'; 
      echo $fileType . '<br />'; 
      $uploadok = 1; 
      if (file_exists($target_file)) { 
       echo '<div class="alert alert-danger" role="alert"><strong>Error!</strong> That file already exists, try a different file name.</div>'; 
       $uploadok = 0; 
      } 
      if ($_FILES["file"]["size"] > 5000000) { 
       echo '<div class="alert alert-danger" role="alert"><strong>Error!</strong> That file is too large.</div>'; 
       $uploadok = 0; 
      } 
      if($fileType != 'zip' && $fileType != 'gz' && $fileType != 'java' && $fileType != 'py' && $fileType != 'html' && $fileType != 'css' && $fileType != 'js'){ 
       echo '<div class="alert alert-danger" role="alert"><strong>Error!</strong> That type of file is not acceptable.</div>'; 
       $uploadok = 0; 
      } 
      if($uploadok == 0){ 
       echo '<div class="alert alert-danger" role="alert"><strong>Error!</strong> The file was not uploaded.</div>'; 
      }else{ 
       if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)){ 
        echo '<div class="alert alert-success" role="alert"><strong>Yes!</strong> It was successfully submitted!</div>'; 
       }else{ 
        echo '<div class="alert alert-warning" role="alert"><strong>Warning!</strong> There was an error uploading your file with error code ' . $_FILES['file']['error'] . '.</div>'; 
       } 
      } 
     }else{ 
      echo '<div class="alert alert-danger" role="alert"><strong>Error!</strong> There was an error inserting the data.</div>'; 
     } 
    } 
} 
?> 

我想擁有它獲得文件從用戶發送到文件夾,但表格甚至不發送。在我添加了enctype="multipart/form-data"之後,它完全停止發送數據,我不確定是否需要這些數據,但它不起作用。

+0

是PHP和與html相同的頁面? – Mihai 2015-02-08 20:38:27

+0

是的,PHP和HTML在同一個文件。 – mttprvst13 2015-02-08 20:41:19

+0

你是說如果你刪除'enctype =「multipart/form-data」'數據被髮送?你可以用開發者工具(網絡標籤)檢查表單提交的響應嗎? – 2015-02-08 20:46:36

回答

-1

您無形式發送數據。

看到:

<form class="form-horizontal" method="POST" enctype="multipart/form-data">

在這裏,你應該有類似action="myprocessingpage.php",其中myprocessingpage.php是你正在處理後的數據。

它也出現,你錯過了一個關閉</form>標籤。

+0

action屬性是可選的,形式爲默認提交到當前URI。表單元素的結束標記是強制​​性的,但如果沒有後續表單,則不會破壞任何內容。 – Quentin 2015-02-08 20:37:41

+0

是的,我已經結束''標記,我只是忘記包含它。 – mttprvst13 2015-02-08 20:41:00

+0

如果OP說他將數據發送到同一個URI,那麼這會有所幫助,所以感謝那些我不知道他在做什麼的事情。 :) – Jguy 2015-02-08 20:41:14