我正在制作一个网站,并且无法将表单发送到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"
之后,它完全停止发送数据,我不确定是否需要这些数据,但它不起作用。
是PHP和与html相同的页面? – Mihai 2015-02-08 20:38:27
是的,PHP和HTML在同一个文件。 – mttprvst13 2015-02-08 20:41:19
你是说如果你删除'enctype =“multipart/form-data”'数据被发送?你可以用开发者工具(网络标签)检查表单提交的响应吗? – 2015-02-08 20:46:36