文件提交一直有效,直到我添加了jQuery,这是什么问题?

时间:2015-05-29 15:12:27

标签: php jquery html

我有一个奇怪的问题,我能够提交文件并成功上传它们,现在我已经在网页的head中添加了这一点jQuery,它似乎无法正常工作了:

在没有jQuery的情况下再次测试并且它工作得很好,我的jQuery在某个地方搞乱它。无论如何,我可以以不同的方式实现同​​样的目标吗?

Jquery的:

$(document).ready(function() {
    var options = $('select[name=itemType]');
    var optionVal = options.val();
    var filer = $('input[name=itemFile');
    var filerVal = $('input[name=itemFile]').val();

    options.change(function() {
        optionVal = $(this).val();

        if(optionVal == 1 || optionVal == 3) {
            $('input[name=itemContact]').removeAttr('disabled');
            $('input[name=itemContact]').attr('required','true');
        } else {
            $('input[name=itemContact]').attr('disabled','true');
            $('input[name=itemContact]').removeAttr('required');
        };
        if(optionVal != 5) {
            $('input[name=itemFile]').removeAttr('disabled');
            $('span.inputFile').css("display","block");
        } else {
            $('input[name=itemFile]').attr('disabled','true');
            $('span.inputFile').css("display","none");
        };
    });
    filer.change(function() {
        filerVal = $(this).val();
        $(this).parent().text(filerVal);
    });
});

HTML表单:

注意:$sectionName早先在整个网页中定义,并且确实有效。

<form class="col-12" method="post" action="./action.php?upload" enctype="multipart/form-data">
    <p class="head">Upload to <i><?php echo $sectionName; ?></i></p>
    <div class="con">
        <input name="sectionID" type="hidden" value="<?php echo $section; ?>" required />
        <select name="itemType" required>
            <option selected disabled>Select an Item Type</option>
            <option value="1">Downloadable, Contact</option>
            <option value="2">Downloadable</option>
            <option value="3">Installable, Contact</option>
            <option value="4">Installable</option>
            <option value="5">Browsable</option>
        </select>
        <span class="inputFile" style="display:none;">
            Select a File
            <input name="itemFile" type="file" required />
        </span>
        <input name="itemName" type="text" placeholder="Enter Item Name" required />
        <input name="itemContact" type="email" placeholder="Enter Contact Email Address" disabled />
        <button type="submit">Submit</button>
    </div>
</form>

action.php?upload

$fileDir = "./uploads/";
$fileName = preg_replace("/[^A-Z0-9._-]/i", "_", $_FILES['itemFile']["name"]);
$itemDir = $fileDir. $fileName;
$sectionID = $_POST['sectionID'];
$itemName = $_POST['itemName'];
$itemType = $_POST['itemType'];
$itemContact = $_POST['itemContact'];

if (file_exists($itemDir)) {
    echo '<p class="alert">Sorry, file already exists</p>';
} else {
    if (move_uploaded_file($_FILES["itemFile"]["tmp_name"], $itemDir)) {
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        if($createItem = $con->prepare("INSERT INTO items(sectionID,itemName,itemType,itemDir) VALUES(?,?,?,?)")) {
            $createItem->bind_param("isis", $sectionID,$itemName,$itemType,$itemDir);
            if($createItem->execute()) {
                $itemID = $createItem->insert_id;
                if($itemType == 1 || $itemType == 3) {
                    if($createContact = $con->prepare("INSERT INTO contacts(itemID,itemContact) VALUES(?,?)")) {
                        $createContact->bind_param("is", $itemID,$itemContact);
                        if($createContact->execute()) {
                            echo '<p class="alert">Item Created</p>';
                        } else {
                            echo "<p class=alert'>Execute failed: [createContact] (" . $createContact->errno . ") " . $createContact->error. '</p>';
                        };
                    } else {
                        echo '<p class="alert">Item Created</p>';
                    };
                    $createContact->close();
                };
            };
        };
        $createItem->close();
    };
};

不是上传文件,也不是将其存储在$_FILES中,而是传递给action.php。我已经检查了$_POST$_FILES,它没有出现在其中任何一个,而在此之前。

注意:这是我的某个网络服务器上的内部网站,因此如果它易受SQL注入攻击并不重要。

1 个答案:

答案 0 :(得分:1)

选择器在此行无效

private static class Environment {
    private String aps;
    private String osId;
    private String sample;
    private boolean forMigrate;

    public String getAps() {
        return aps;
    }
    public void setAps(String aps) {
        this.aps = aps;
    }
    public String getOsId() {
        return osId;
    }
    public void setOsId(String osId) {
        this.osId = osId;
    }
    public String getSample() {
        return sample;
    }
    public void setSample(String sample) {
        this.sample = sample;
    }

    private void forMigration(Environment e, Consumer<Environment> con) {
        if (!e.isForMigrate()) {
            con.accept(e);
        }
    }

    public boolean isForMigrate() {
        return forMigrate;
    }
    public void setForMigrate(boolean isForMigrate) {
        this.forMigrate = isForMigrate;
    }

    protected Environment create(Environment env) {
        Environment newEnv= new Environment();
        List<String> imh=new ArrayList<>();
        forMigration(env, e -> {newEnv.setAps(e.getAps());newEnv.setOsId(e.getOsId()); });
        forMigration(env, e -> {imh.add("test for generic call"); });
        return newEnv;
    }
}

var filer = $('input [name = itemFile ] ');