0

So I have this strip of code working for one page and not working for another page... Same code, same form name attributes, same php functionality on the server side. I'm calling with jquery ajax.. Calling from one page, it works completely fine... When I call from another page, it gives me "Unidentified index"

I've tried changing the names around (to remove duplicates and all).. I seem to be sure that the names are correct though.

<script type="text/javascript">
    $('#uploadcsv').submit(function(e){
        e.preventDefault();
        $.ajax({
            url: "functions/grocerystore.php?item=fetchcontacts",
            method: "post",
            data: new FormData(this),
            dataType: 'json',
            contentType: false,
            cache: false,
            processData: false,
            success: function(data){
                console.log(data);
                var accumulator = "";
                var filetype = $('#uptype').val();
                if(filetype == "csv"){
                    for(trav=0; trav<data.length; trav++){
                        accumulator += data[trav];
                        if(trav < data.length-1)
                            accumulator += "\r\n";
                    }
                }else{
                    for(trav=0; trav<data.length; trav++){
                        accumulator += data[trav];
                    }
                }
                var mobilenumbers = $('#mobilenumbers');
                mobilenumbers.html(accumulator);
            },
            error: function(data){
                console.log(data);
            }
        });
    });
</script>

php

case 'fetchcontacts':
                if(!empty($_FILES['customFilex']['name'])){
                    $name = explode('.',$_FILES['customFilex']['name']);
                    $extension = end($name);
                    $file_data = fopen($_FILES['customFilex']['tmp_name'],"r");
                    if($extension == 'csv'){
                        fgetcsv($file_data);
                        $mobile = array();
                        $init = 0;
                        while($row = fgetcsv($file_data)){
                            $mobile[$init] = $row[0];
                            $init++;
                        }
                        fclose($file_data);
                    }else{
                        if ($file_data = fopen($_FILES['customFilex']['tmp_name'], 'r')) {
                            $mobile = array();
                            $init = 0;
                            while (!feof($file_data)) {
                                $row = fgets($file_data);
                                $mobile[$init] = $row;
                                $init++;
                            }
                            fclose($file_data);
                        }
                    }
                    echo json_encode($mobile);
                }else{
                    $report["status"] = "failed";
                    echo json_encode($report);
                }
                break;

I just need it to accept my file...

Ferdinando
  • 964
  • 1
  • 12
  • 23
josh
  • 1
  • Welcome. Are there any other scripts running on either HTML page? Can you provide links to the HTML pages? – Tedinoz Dec 22 '18 at 07:03
  • Suggest you read [“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) for some ideas on what might be causing the error, and how you can troubleshoot it. For example, what does the console show – Tedinoz Dec 22 '18 at 07:06
  • I don't know what this is `php case 'fetchcontacts':` but it's not syntactically correct PHP. – ArtisticPhoenix Dec 22 '18 at 08:16
  • Can you please copy the full error message so that we can know on which lines of code we should focus? – asiby Dec 22 '18 at 17:32
  • Thanks guys... I already found a way around it.... I'm using case 'fetchcontacts': because I'm using a switch statement... – josh Dec 23 '18 at 20:46

1 Answers1

1

You are using $_FILES['customFilex']['tmp_name']without checking if the keys exists in the $_FILES array.

asiby
  • 3,229
  • 29
  • 32