-1

I have set up my code to pass formdata via Ajax to my php file. My code works until I actually select an image and then I get a parsing error in my php file. I get an undefined index error. I have a form named "AddItemForm". My image input line looks like this:

 <input type="file" id="_ni_image_in" onchange="readURL(this);" name="ni_image" accept="image/png, image/jpeg">

My javascript/ajax code looks like this which gets called when submit button is selected on form:

function AddItem(){
var form = document.forms["AddItemForm"];
var name = document.forms["AddItemForm"]["ni_name"].value;
var tag = document.forms["AddItemForm"]["ni_tag"].value;
var description = document.forms["AddItemForm"]["ni_description"].value;
var weight = document.forms["AddItemForm"]["ni_weight"].value;
var date = document.forms["AddItemForm"]["ni__date"].value;
var color = document.forms["AddItemForm"]["ni_color"].value;
var itag2 = document.forms["AddItemForm"]["ni_tag2"].value;
var itag3 = document.forms["addcowform"]["ni_tag3"].value;
var useremail= "<?php echo $_SESSION['UserEmail']; ?>";

var itemdata = new FormData();

    var filedata = document.getElementById("_nc_image_in").files[0];
    itemdata.append('Item_Image', filedata);

itemdata.append('Name', name);
itemdata.append('Tag_Num', tag);
itemdata.append('Description', description);
itemdata.append('Weight', weight);
itemdata.append('Date', date);
itemdata.append('Color', color);
itemdata.append('Tag_Num2', itag2);
itemdata.append('Tag_Num3', itag3);
itemdata.append('User_Email', useremail);

  var isValid = false;
  $.ajax({           
      type: "POST",  
      url: "/AddNewItem.php",  
      data: itemdata,
      processData: false,
          contentType: false,
      success: function(resp){
        console.log(resp);
        if(resp.reply == "Success")
        {
          isValid = true;
          form.submit();
        }
        else
        {
       isValid = false;
        }
      },
      error: function(data, status){
        console.log(data, status);
        alert("error")
      }

    }); //end Ajax
    console.log(isValid);
     return isValid;
};

In my php file, I am retrieving image like this:

$itemmage = $_REQUEST["Item_Image"];

It is working if I don't select an image, so something is wrong with how I am getting my image or retrieving it. Please help.

cfnerd
  • 3,658
  • 12
  • 32
  • 44
BEP
  • 39
  • 6
  • PHP has never provided the data of an actual file upload in $_REQUEST … http://php.net/manual/en/features.file-upload.php – 04FS Jan 29 '19 at 14:03
  • Possible duplicate of ["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) – Nico Haase Jan 29 '19 at 14:33

1 Answers1

0

Use $_FILES instead of $_REQUEST

Edit

if(isset($_FILES['image']) && $_FILES['image']['name']!=""){
    $image_name= $_FILES['image']['name'];
    move_uploaded_file( $_FILES['image']['tmp_name'], "images/$image_name");
}

$_FILES has an array for each image:

[name] => MyFile.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174

See http://php.net/manual/en/reserved.variables.files.php

halfer
  • 19,824
  • 17
  • 99
  • 186
Paddy Hallihan
  • 1,624
  • 3
  • 27
  • 76
  • so I changed it to this $_FILES["Item_Image"]; still getting parse error. is that syntax right? – BEP Jan 29 '19 at 15:01
  • PaddyHallihan - you are being very helpful. Thank you. So I don't want to move my file but I want to use a $sql = "INSERT INTO. to insert into my database table. So to what is proper syntax to move it into my database as a jpeg. Right now it is upload and it is a .bin type. – BEP Jan 29 '19 at 15:37
  • Yeah so what I do is just get the file name and store that in my DB but if I want to display it on my site I'll need to upload/move it to the server or local environment so that I can reference the file location, for example I'd upload it to a `media/product_images/` directory, then store the name in my DB then I can call the name again and display it like `` – Paddy Hallihan Jan 29 '19 at 15:43
  • If you don't upload/move it somewhere all you have is an array of info referencing an image that is still in the `tmp` folder of the computer that filled in the form and no actual image on the server/local environment – Paddy Hallihan Jan 29 '19 at 15:45
  • Paddy - Roger that. I guess I have been going about it wrong. I thought I could directly upload a image to my database. But I need to move the images to a directory in my site directory and just push the path to my database. Is that right? – BEP Jan 29 '19 at 16:07
  • so mine are usually all in the same directory so i just store the name but you can upload them to different folders in some sort of conditional statement and then you could store the full file path of wherever you moved it to. – Paddy Hallihan Jan 29 '19 at 16:12
  • Paddy - I got it to work!! Thank you so much. Last question. I will be having many different users so my image folder will get filled with a lot of different images. Do you have any recommendations on keeping filenames unique. Even the same user could upload an image with same name I would imagine. I can only think to rename each image myself and keep some sort of global counter to add to the end of the image name. – BEP Jan 29 '19 at 22:02
  • So instead of saving the file name to the db you could get the last inserted id and use that as the var for the file name when you're uploading the file – Paddy Hallihan Jan 29 '19 at 22:21