简体   繁体   English

HTML表单未将数据发送到PHP

[英]HTML form not sending data to PHP

I am making a website and am having trouble having a form send data to PHP. 我正在制作一个网站,无法将表格发送数据到PHP。 Here is my code for the form: 这是我的表格代码:

<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>

(I use BootStrap for the form) Here is my code for php: (我将BootStrap用于表单)这是我的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>';
        }
    }
}
?>

I am trying to have it get a file from the user and send the file to a folder, but the form is not even sending. 我试图让它从用户那里获取一个文件并将其发送到文件夹,但是表单甚至没有发送。 After I added the enctype="multipart/form-data" it just completely stop sending the data, I am not sure if that is needed or not, but it is not working. 添加完enctype="multipart/form-data"它完全停止发送数据,我不确定是否需要它,但是它不起作用。

You have no place for the form to send the data. 您没有放置表格发送数据的地方。

see: 看到:

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

In here, you should have something like action="myprocessingpage.php" , where myprocessingpage.php is where you're processing the post data. 在这里,您应该有类似action="myprocessingpage.php" ,其中myprocessingpage.php是您在其中处理帖子数据的地方。

It also appears that you're missing a closing </form> tag as well. 同样,您似乎也缺少</form>标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM