繁体   English   中英

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

[英]How do I get the select option value from my HTML form to my PHP script?

我正在为我的网站制作表格,希望用户能够在其中将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的值,以查看其是否有效。 但是它们永远不会更改为我在选择选项中选择的内容。

请帮忙!

表单元素要求name属性显示在$_POST数组中:

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

    <!-- Selector for differnet materials -->
    <select id = "material" name="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" name="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>

暂无
暂无

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

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