2015-02-08 119 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