0

This is a project that works on another opensuse 13.1. On my laptop, also opensuse 13.1, i get an undefined index for the input "census". I'm afraid to copy back up to the server, as it works with the same code!

the last line i show for update.php is the first undefined index I get for 'census'. the var_dumps both return NULL. However, I can use console.log on proposal.php to dump the census variable to console, and it's correct. It will show the filename of the file. Why isn't it getting passed to the second php script?

I have enctype set in the ajax call and in the form tag.

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>

now update.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"]);
tktenn
  • 31
  • 5
  • Where are you echoing your results? You need to echo for the AJAX to return something. – Script47 Oct 10 '15 at 16:18
  • It isn't the whole file. It does echo the results, i stopped the code where it says the first undefined index is. It echos 1 when successful or otherwise a message from a set of conditionals when the input isn't correct. – tktenn Oct 10 '15 at 16:33
  • Here is the result shown on proposal.php array(0) { } array(0) { } Notice: Undefined index: census in /home/troy/public_html/proposal/assets/update.php on line 35 Notice: Undefined index: census in /home/troy/public_html/proposal/assets/update.php on line 38 Notice: Undefined index: census in /home/troy/public_html/proposal/assets/update.php on line 69 Error: File type not allowed. Accepted formats are .xls, .xlsx and .csv. – tktenn Oct 10 '15 at 16:36
  • Actually, this code is working for csv (text based file format) but not xlsx or xls files. Why is that? – tktenn Oct 10 '15 at 17:20
  • @Terminus it's added and also in the ajax. See my previous comment, the code will work but only for a text based file. if it's an excel file format, it will not work. – tktenn Oct 10 '15 at 17:28
  • my bad. .xlsx files are more like zip files. you need a library to parse them –  Oct 10 '15 at 17:33
  • @Terminus even if your form action script is just storing them somewhere? Thank you for the responses, really appreciate it btw! – tktenn Oct 10 '15 at 17:45
  • just storing the file should have no problems –  Oct 10 '15 at 17:59
  • That's all i'm doing with it, storing to disk. That's why it's odd it will work fine for a csv, but fails on anything binary. PHP 5.4.20, apache 2.4.6. I have read about 40 stack overflows, only 1 similar so far but it was a jpeg he was looking for and it's fix didn't fix mine. – tktenn Oct 10 '15 at 18:24
  • unrelated to question at hand: check out [this question](http://stackoverflow.com/a/2486343/5051310) and the links on it for some ways to verify you're getting the right file type –  Oct 10 '15 at 18:25
  • and `var_dump`ing the whole `$_FILES` array give you nothing? –  Oct 10 '15 at 18:25
  • @Terminus yup, var dump $_FILES and $_POST['census'] come back array(0) { } if add ['census']['name'] it comes back NULL. tmp folder is writable by all, 777. I will check those out. – tktenn Oct 10 '15 at 19:28
  • tried to simplify the problem to just focus on the files and made this http://codepad.org/XPpeEzAE. All update.php does is `` That seems to pass any filetypes along just fine. –  Oct 10 '15 at 19:38
  • getting ready to leave but i'm going to put all this up on a vps, running centos, see if i have the same issue. Now i'm just ticked because everything looks fine and it will work for a text based file type. – tktenn Oct 10 '15 at 20:06

0 Answers0