如何从HTML表单到PHP脚本获取选择选项值?

时间:2019-02-05 15:09:54

标签: php html

我正在为我的网站制作表格,希望用户能够在其中将DXF文件和有关该材料的一些信息上载到我的网站服务器。

现在,我可以将文件上传到服务器了。但是由于某种原因,我无法使PHP脚本从表单中的选择选项中读取值。

我认为问题在于PHP脚本不知道在哪里寻找值,因为当我提交表单时,我得到了Notice: Undefined index:

我尝试使用if(isset())来查看是否确实存在要保存的值。我还尝试启动要将值保存到的变量,但是没有任何效果。

我是HTML和PHP的超级新手,我只使用了两天,所以我可能会觉得超级简单。

这是我使用的HTML表单:

<form action = "../php/uploadDXF.php" method = "POST" enctype="multipart/form-data">

    <!-- Selector for differnet materials -->
    <select id = "material"> 
        <option value="0" >- Selecet material -</option>
        <option value="1" >Stainless steel</option>
        <option value="2" >Hot rolled</option>
        <option value="3" >Cold rolled</option>
        <option value="4" >Aluminium</option>
    </select>

    <!-- Selector for material thicknesses, see selector.js -->
    <select id = "thickness"> 
        <option value="0" >- Selecet thickness -</option>
    </select>
    <!-- Form for uploading a .DXF file, see uploadDXF.php -->
    <input type = "file" name = "inputDXF"/>
    <button type = 'submit' name = 'submit'>Upload</button>

</form>

这是我的PHP脚本:

<?php
// php script for uploading a file to the webserver via the website. 

//Variables
$maxFileSize = 10000; 
$selectedMaterial = 'test1';
$selectedThickness = 'test2';

if(isset($_POST['submit'])) { // Run this program when Upload is clicked


    // Material and thickness
    if(isset($_POST['material'])) { 

    }
    if(isset($_POST['thickness'])) {

    }
    $selectedMaterial = $_POST['material']['value'];
    $selectedThickness = $_POST['thickness']['value'];

$file = $_FILES['inputDXF']; // The file being uploded

    // Name, size, location, error and type of file being uploaded
    $fileName = $_FILES['inputDXF']['name']; 
    $fileSize = $_FILES['inputDXF']['size'];
    $fileTmpName = $_FILES['inputDXF']['tmp_name'];
    $fileError = $_FILES['inputDXF']['error'];
    $fileType = $_FILES['inputDXF']['type'];
    // ---------------------------------------------

    $fileExt = explode('.', $fileName); // Explode name in to array
    $fileActualExt = strtolower(end($fileExt)); // Make last entry in array lowercase 

    $allowedFileTypes = array('dxf'); // Files that are allowed to be uploaded

    if (in_array($fileActualExt, $allowedFileTypes)) { // Is the file-type allowed?

        if($fileError === 0) { // Is the file free of errors?

            if ($fileSize < $maxFileSize) { // Is the file too big?

                $fileNameNew = uniqid('', true).".".$fileActualExt; // Gives the uploaded file a uniqe name
                $fileDestionation = '../uploads/'.$fileNameNew; // Where the file should be uploaded
                move_uploaded_file($fileTmpName, $fileDestionation); // Function that uploads the file


            } else {
                echo "Your file is too big. Maximum size allowed: 10mb.";
            }
        } else {
            echo "There was an error uploading your file.";
        }
    } else {
        echo "The file must ba a .DXF";
    }

}
echo $selectedMaterial;
//header("Location: http://localhost/Version%203/pages/test.html");
?>

我知道您不应该包含完整的PHP脚本,但是我不确定什么是重要的。

我想做的是在PHP脚本的末尾回显已保存的变量$selectedMaterial$selectedThickness的值,以查看其是否有效。但是它们永远不会更改为我在选择选项中选择的内容。

请帮助!

1 个答案:

答案 0 :(得分:3)

表单元素需要@date DATE = GETDATE();属性才能显示在select REPLACE(LEFT(CONVERT(DATE,@date,112),8),'-','') -- 1st approach select FORMAT(@date,'yyyyMM') --2nd approach 数组中:

name