提交时无法访问 php 文件中的 Html 表单元素,收到错误未指定的索引?



这是文件upload.php。当我尝试提交HTML表单并获取stateCode的值时,它会引发未指定的索引错误,请参阅附件屏幕快照

        <html>
        <body>
        <?php
        $target_dir = getcwd().DIRECTORY_SEPARATOR."/files/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]
        ["name"]);
        $dst = $target_file;
        $uploadOk = 1;
        echo $_POST['stateCode'] ;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        // Check if image file is a actual image or fake image
        if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
        }
        // Check if file already exists
        if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
        }
       // Check file size
       if ($_FILES["fileToUpload"]["size"] > 500000) {
           echo "Sorry, your file is too large.";
        $uploadOk = 0;
       }
    // Allow certain file formats
    if($imageFileType != "xls" && $imageFileType != "xlsx" ) {
        echo "Sorry, only Excel files are allowed.";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
    ?>
    </body>
    </html>

以下是welcome.html,每当我尝试提交此表格时,我都会收到以下错误。

<!DOCTYPE html>
<html>
<style>
#form {
    background-color: #FFF;
    height: 600px;
    width: 600px;
    margin-right: auto;
    margin-left: auto;
    margin-top: 0px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    padding: 0px;
    text-align:center;
}
label {
    font-family: Georgia, "Times New Roman", Times, serif;
    font-size: 18px;
    color: #333;
    height: 35px;
    width: 200px;
    margin-top: 18px;
    margin-left: 10px;
    text-align: right;
    margin-right:15px;
    float:left;
}
iframe {
    height:35px;
    width: 300px;
    margin-top:14px;
  }
 button{
 margin-top: 18px;
 }
</style>
<script>

 var dvalues='';
function myFunction(){
  var iframe = document.getElementById('frame');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var b =innerDoc.body;
 var c = b.childNodes;
 var dvalues = b.childNodes[0].value;
  document.getElementById("placeholder").innerHTML =dvalues;
 }
window.onload = function(){
document.getElementById("placeholder").innerHTML=dvalues;
};
</script>
<body>

<div id = "form">
<form action="upload.php" method="post" enctype="multipart/form-data">
 <fieldset>
<div class="form-group">
    <label for="States" name ="states">States:</label>
    <iframe id ="frame" name="myPHPScript" src="myScript.php" width="100%" frameborder="0"></iframe>
    <p  id = "placeholder" name="stateCode"></p>    
    <input type="file" name="fileToUpload" id="fileToUpload" /><input type="submit" name="action"onclick="myFunction()"/>
 </div>
 </fieldset>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
#form {
    background-color: #FFF;
    height: 600px;
    width: 600px;
    margin-right: auto;
    margin-left: auto;
    margin-top: 0px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    padding: 0px;
    text-align:center;
}
label {
    font-family: Georgia, "Times New Roman", Times, serif;
    font-size: 18px;
    color: #333;
    height: 35px;
    width: 200px;
    margin-top: 18px;
    margin-left: 10px;
    text-align: right;
    margin-right:15px;
    float:left;
}
iframe {
    height:35px;
    width: 300px;
    margin-top:14px;
  }
 button{
 margin-top: 18px;
 }
</style>
<script>

 var dvalues='';
function myFunction(){
  var iframe = document.getElementById('frame');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var b =innerDoc.body;
 var c = b.childNodes;
 var dvalues = b.childNodes[0].value;
  document.getElementById("placeholder").innerHTML =dvalues;
 }
window.onload = function(){
document.getElementById("placeholder").innerHTML=dvalues;
};
</script>
<body>

<div id = "form">
<form action="upload.php" method="post" enctype="multipart/form-data">
 <fieldset>
<div class="form-group">
    <label for="States" name ="states">States:</label>
    <iframe id ="frame" name="myPHPScript" src="myScript.php" width="100%" frameborder="0"></iframe>
    <p id = "placeholder"></p>
    <input type="text"   name="stateCode" />   

    <input type="file" name="fileToUpload" id="fileToUpload" /><input type="submit" name="action"onclick="myFunction()"/>
 </div>
 </fieldset>
</form>
</div>
</body>
</html>

将P修复到输入类型文本中。不确定您实际打算的类型是什么,但这应该使您运行。

最新更新