php ajax提交为$ _FILES

时间:2015-10-10 16:14:33

标签: javascript php jquery ajax post

这是一个适用于另一个opensuse 13.1的项目。在我的笔记本电脑上,也打开了13.1,我得到输入“人口普查”的未定义索引。我害怕复制回服务器,因为它使用相同的代码!

我为update.php显示的最后一行是我为'census'获得的第一个未定义的索引。 var_dumps都返回NULL。但是,我可以使用proposal.php上的console.log将census变量转储到控制台,这是正确的。它将显示文件的文件名。为什么不将它传递给第二个PHP脚本?

我在ajax调用和form标签中设置了enctype。

initial proposal.php:

    <script>
$(document).ready(function(){
    $('#subreq').click(function(){
        $("#mainform").hide();
        $("#prevreqs").show();
    });
    $('#newreq').click(function(){
        $("#mainform").show();
        $("#prevreqs").hide();
    });     

    $('input[type="radio"]').click(function(){

        if($(this).attr("value")=="1099"){
            $("#mecprod").hide();
            $("#tprod").show();
            $("#industry").show();
        }
        if($(this).attr("value")=="W2"){
            $("#mecprod").show();
            $("#tprod").show();
            $("#industry").hide();
            $('[name=indtype]').prop('checked',false);
        }
        if($(this).attr("value")=="trucking"){
            $("#trucktype").show();
        }
        if($(this).attr("value")=="realtor" || ($(this).attr("value")=="other")){
            $("#trucktype").hide();
            $('[name=transtype]').prop('checked',false);
        }
        if($(this).attr("value")=='other'){
            $("#otherinfo").show();
        }

    });


    $('form#proposal-request').submit( function(e) {
        e.preventDefault();
        var cname = $('#cname').val();
        var caddress = $('#caddress').val();
        var ctype = $('#ctype').val();
        var ein = $('#ein').val();
        var wellmec = $('#wellmec').val();
        var tmec = $('#tmec').val();
        var dmec = $('#dmec').val();
        var census = $('#census').val();
       var std_comp = $('#std_comp');
        var notes = $('#notes');
        var emptype = $('input:radio[name=emptype]:checked').val();
        var transtype = $('input:radio[name=transtype]:checked').val();

        if ($('input:radio[name=indtype]:checked').val() == 'other'){
            var indtype = $('#otherind').val();
        } else {    
            var indtype = $('input:radio[name=indtype]:checked').val();
        }

        if (cname == '' || caddress == '' || ein == '') {
            $('#error').show().children('span').html('Compnay Name, Address and EIN are required.');
            return false;
        } else if ($('input:radio[name=emptype]:checked').val() == 'W2' && $('#wellmec').prop('checked') == false && $('#tmec').prop('checked') == false && $('#dmec').prop('checked') == false) {
            $('#error').show().children('span').html('At least one MEC product must be selected.');
            return false;
        } else if (census == '') {
            $('#error').show().children('span').html('A Census Must Be Uploaded To Continue.');
            return false;
        } else if (emptype == '') {
            $('#error').show().children('span').html('Please select 1099 or W2.');
            return false;
        } else {
            $.ajax({
                url: 'assets/update.php',
                type: 'POST',
                enctype: 'multipart/form-data',
                data: new FormData(this),
                cache: false,
                contentType: false,
                processData: false,
                success: function(response) {
                    if (response == '1') {
                        $('#error').hide();
                        $('#success').show();
                        $('#cname').val('');
                        $('#caddress').val('');
                        $('#ctype').val('');
                        $('#ein').val('');
                        $('#fname').val('');
                        $('#lname').val('');
                        $('#bemail').val('');
                        $('[name=emptype').prop('checked',false);
                        $('#wellmec').prop('checked', false);
                        $('#tmec').prop('checked', false);
                        $('#dmec').prop('checked', false);
                        $('#census').replaceWith($('#census').val('').clone(true));
                        $('#std_comp').val('');
                        $('#notes').val('');
                        $('[name=truck]').prop('checked',false);
                        $('[name=indtype]').prop('checked',false);
                        $('#otherind').val('');
                        $moveon = window.setTimeout(function() {
                            $('#success').hide();
                        }, 5000);
                    } else {
                        $('#error').show().children('span').html(response);
                    }
                }
            });
        }
    });

    $('.close').click(function() {
        $(this).parent().hide();
    })

});

<form method="POST" id="proposal-request" >
    <div class="form-group">
    <h4><b>Upload Census File:</b></h4>
		<input type="file" id="census" name="census" />
		<p class="help-block">A census file is required to continue.</p>
	</div>
    </form>

现在更新.php

    var_dump($_POST);
var_dump($_FILES['census']);
try {   
$cname = filter_input(INPUT_POST, 'cname', FILTER_SANITIZE_STRING);
$caddress = filter_input(INPUT_POST, 'caddress', FILTER_SANITIZE_STRING);
$ctype = filter_input(INPUT_POST, 'ctype', FILTER_SANITIZE_STRING);
$ein = filter_input(INPUT_POST, 'ein', FILTER_SANITIZE_STRING);
$wellmec = (!empty($_POST['wellmec']) &&$_POST['wellmec'] == '1' ? 1 : 0);
$tmec = (!empty($_POST['tmec']) && $_POST['tmec'] == '1' ? 1 : 0);
$dmec = (!empty($_POST['dmec']) &&$_POST['dmec'] == '1' ? 1 : 0);
$broker_id = $_SESSION['id'];
$std_comp = filter_input(INPUT_POST, 'std_comp', FILTER_SANITIZE_STRING);
$notes = filter_input(INPUT_POST, 'notes', FILTER_SANITIZE_STRING);
$emptype = filter_input(INPUT_POST, 'emptype', FILTER_SANITIZE_STRING);
$indtype = filter_input(INPUT_POST, 'indtype', FILTER_SANITIZE_STRING);
$transtype = filter_input(INPUT_POST, 'transtype', FILTER_SANITIZE_STRING);

$Database->sqlQuery('SELECT email, fname, lname FROM brokers WHERE id = :broker_id', array(':broker_id' => $broker_id));
$broker = $Database->sth->fetch();

$allowedExts = array('csv', 'xls', 'xlsx');
$temp = explode(".", $_FILES["census"]["name"]);

0 个答案:

没有答案