I keep getting an undefined index notice

661 views Asked by At

I have a form which contains a file input:

 var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startImageUpload(this);' class='imageuploadform' ><label>" + 
    "Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label class='imagelbl'>" + 
    "<input type='submit' name='submitImageBtn' class='sbtn' value='Upload' /></label>" + +
    "</p> <iframe class='upload_target' name='upload_target' src='#' style='wclassth:0;height:0;border:0px;solclass #fff;'></iframe></form>");

This links to a php script where it uploads the file:

<?php

   $destination_path = str_replace("//", "/", $_SERVER['DOCUMENT_ROOT']."/")."ImageFiles";

   $result = 0;

   $target_path = $destination_path . basename( $_FILES['fileImage']['name']);

   if(move_uploaded_file($_FILES['fileImage']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);
?>

<script language="javascript" type="text/javascript">window.top.window.stopImageUpload(<?php echo $result; ?>);</script>   

Problem is that in the php script it is stating I am having an undefined index wherever it states 'fileImage' in the php script. But I don't know why it is saying I am having undefined index when I have mentioned in the name attribute in the form 'fileImage'. Why is it stating I am having an undefined index for 'fileImage' in the php script?

Here is a link to an application where you can append rows which include file inputs, you can test this and see for yourself if you wish but at moment it keeps saying there is an error during uploading. application

2

There are 2 answers

8
hakre On

There can be many reasons that are the root-cause of this error, but the actual reason is, that the $_FILES array has no string index 'fileImage'.

To get a better view which indexes are available, you can do a:

var_dump(array_keys($_FILES));

See as well array_keys and var_dump.

Probably the following (in this question undocumented javascript code) is destroying something in your form or it's data:

onsubmit='startImageUpload(this);'
0
Sthe On

I have encountered the same problem before and it was because of my ajax/javascript code. As a test, try sending your form without javascript. In that way, you will know where exactly the problem is. If the error goes away, your problem might be with javascript.

I never really got the right solution to upload files using ajax. I solved the problem by sending the form into an iframe and then collecting feedback once it is done loading.

I'd do something like this on the form tag: <form action="..." target="myframe">. create a frame with name="myframe" and onload="getResults()" This is vague, but I hope it does give you an idea.

Just be careful when you use onload because it will fire twice viz. when the page is done loading for the first time, and when the form is done submitting.

I hope I haven't completely misunderstood your point here.