简体   繁体   中英

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

I am working on a form for my website where I want the user to be able to upload a DXF-file and some information about the material to my website server.

Right now I am able to upload the file to the server. But for some reason I cannot get the PHP-script to read the values from the select options that I also have in the form.

I think the problem is that the PHP-script dosn't know where to look for the values because when I submit the form I get Notice: Undefined index: .

I have tried using if(isset()) to see if there actually is a value to save. I have also tried initiating the variables that I want to save the value to, but nothing works.

I am super new to HTML and PHP, I have only been working with it for two days, so it could be somthing super simple I am missing.

This is the HTML form I use:

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

And this is my PHP-script:

<?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");
?>

I know you are not supposed to include full PHP-scripts but I am unsure of what is important.

What I am trying to do is echo the value of the saved variables $selectedMaterial and $selectedThickness at the end of the PHP-script to see if it works. But they never change to what I chose in the select option.

Please help!

Form elements require the name attribute to show up in the $_POST array:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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