2013-12-08 38 views
0

这是我的html表单,它通过php验证了ajax请求。但提交后,它显示下面的错误我:Php文件没有上传ajax请求

Notice: Undefined index: img in D:\Software Installed\xampp\htdocs\wix\users\insert_galary.php on line 12 

如果我删除表格的文件部分,然后它的正常工作,也当我检查

if(isset($_POST['Submit']) && $_POST['Submit'] == "Upload galary") 

那么它无法验证我的数据。

HTML表单

<p><a href="javascript:void(null);" onclick="showDialog2();">Add more</a></p>     
      <div id="dialog-modal2" title="Upload your galary image" style="display: none;"> 

      <form method="post" action="insert_galary.php" id="myForm2" enctype="multipart/form-data" > 

      <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
      <tr> 
      <td>Image title</td> 
      <td><input type="text" name="g_title" placeholder="Image title" class="tr" /></td> 
      </tr>     
      <tr> 
      <td>Upload image</td> 
      <td><input type="file" name="file" class="tr"/></td> 
      </tr> 
      <tr> 
      <td>Image link</td> 
      <td><input type="text" name="g_link" placeholder="Image link" class="tr"/> <span style="color:#030;">* must have http://</span> 
      </td> 
      </tr> 
      <tr> 
       <td><input type="submit" id="sub2" value="Upload Galary" name="Submit" class="submit"/></td> 
       </tr> 
      </table> 
      </form> 

      <span id="result2"></span> 


      <script type="text/javascript"> 
      $("#sub2").click(function() { 
      $.post($("#myForm2").attr("action"), 
        $("#myForm2 :input").serializeArray(), 
        function(info){ $("#result2").html(info); 
       }); 
      clearInput(); 
      }); 

      $("#myForm2").submit(function() { 
       return false; 
      }); 

      </script> 
      </div> 


function showDialog2() 
{ 
$("#dialog-modal2").dialog(
{ 
    width: 610, 
    height: 350, 
    open: function(event, ui) 
    { 
     var textarea = $('<textarea style="height: 276px;">'); 
     $(textarea).redactor({ 
      focus: true, 
      autoresize: false, 
      initCallback: function() 
      { 
       this.set('<p>Lorem...</p>'); 
      } 
     }); 
    } 
}); 
} 

PHP代码:(insert_galary.php)

 if(isset($_POST['Submit']) && $_POST['Submit'] == "Upload Galary") 
     { 
      $current_date = date('Y-m-d H:i:s');  

      $g_title = inputvalid($_POST['g_title']); 
      $g_link = inputvalid($_POST['g_link']); 


      $gid = mt_rand(100000, 999999); 
      $file = $_FILES["file"]["name"]; 
      $type = $_FILES["file"]["type"]; 
      $size = ($_FILES["file"]["size"]/1024); 
      $temp = $_FILES["file"]["tmp_name"]; 
      $allowedExts = array("gif", "jpeg", "jpg", "png"); 
      $temp = explode(".", $_FILES["file"]["name"]); 
      $extension = end($temp);  
      $galary_pic = $gid.".".$extension; 
      $galary_directory = "../galary_images/"; 

      $err = array(); 

      if(isset($g_link) && isset($g_title) && isset($file)) 
      { 
       if(empty($g_link) && empty($g_title) && empty($file)) 
        $err[] = "All field require"; 
       else 
       { 
        if(empty($g_title)) 
         $err[] = "blog title require"; 
        elseif(strlen($g_title) > 250) 
         $err[] = "blog title is too long"; 
        elseif(strlen($g_title) < 2) 
         $err[] = "blog title is too small"; 

        if(strlen($g_link) > 250) 
         $err[] = "image link is too long"; 


        if(empty($file))      
         $err[] = "Upload your picture";    
        elseif(!in_array($extension, $allowedExts)) 
         $err[] = "Uploaded file must be gif, jpeg, jpg, png format"; 
        elseif($size > 500) 
         $err[] = "Uploaded file must be within 500kb";     


       } 
      } 

      if(!empty($err)) 
      { 
       echo "<div class='error'>"; 
       foreach($err as $er) 
       { 
        echo "<font color=red>$er.</font><br/>";     
       } 
       echo "</div>"; 
       echo "<br/>"; 
      } 
      else 
      { 


       if($insert = mysql_query("INSERT INTO galary VALUES('', '', '', '', '$username', '$g_title', '', '$galary_pic', '$g_link', '$current_date', '')"))    
       { 




       define('DESIRED_IMAGE_WIDTH', 338); 
       define('DESIRED_IMAGE_HEIGHT', 338);    


      $source_path = $_FILES['file']['tmp_name']; 
      list($source_width, $source_height, $source_type) = getimagesize($source_path); 

      switch ($source_type) { 
       case IMAGETYPE_GIF: 
        $source_gdim = imagecreatefromgif($source_path); 
        break; 
       case IMAGETYPE_JPEG: 
        $source_gdim = imagecreatefromjpeg($source_path); 
        break; 
       case IMAGETYPE_PNG: 
        $source_gdim = imagecreatefrompng($source_path); 
        break; 
      } 

      $source_aspect_ratio = $source_width/$source_height; 
      $desired_aspect_ratio = DESIRED_IMAGE_WIDTH/DESIRED_IMAGE_HEIGHT; 

      if ($source_aspect_ratio > $desired_aspect_ratio) { 
       $temp_height = DESIRED_IMAGE_HEIGHT; 
       $temp_width = (int) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio); 
      } else { 
       $temp_width = DESIRED_IMAGE_WIDTH; 
       $temp_height = (int) (DESIRED_IMAGE_WIDTH/$source_aspect_ratio); 
      } 

      $temp_gdim = imagecreatetruecolor($temp_width, $temp_height); 
      imagecopyresampled(
       $temp_gdim, 
       $source_gdim, 
       0, 0, 
       0, 0, 
       $temp_width, $temp_height, 
       $source_width, $source_height 
      ); 

      $x0 = ($temp_width - DESIRED_IMAGE_WIDTH)/2; 
      $y0 = ($temp_height - DESIRED_IMAGE_HEIGHT)/2; 
      $desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT); 
      imagecopy(
       $desired_gdim, 
       $temp_gdim, 
       0, 0, 
       $x0, $y0, 
       DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT 
      ); 
      $desired_gdim; 

      //$file = $_FILES["file"]["name"]; 
      //header('Content-type: image/jpeg'); 
      //imagejpeg($desired_gdim, $file); 

      //imagejpeg($desired_gdim, $source_path); 
      imagejpeg($desired_gdim, "$upload_directory/$galary_pic"); 
      //header("Location: img/"); 





       echo "<div class=success>"; 
       echo "successfully uploaded new image."; 
       echo "</div>"; 
       header("Refresh:3, url=all_galary.php"); 
       } 
       else 
       { 
        echo "Sorry, I can't upload your galary"; 
        echo mysql_error(); 
       } 

      } 
     } 
     ?> 

谁能告诉我,为什么它显示我的错误信息?

更新错误消息:

说明:未定义指数:文件d:\软件安装\ XAMPP \ htdocs中\威克斯\用户\上线insert_galary.php 16

说明:未定义指数: D:\ Software Installed \ xampp \ htdocs \ wix \ users \ insert_galary.php中的文件在线18

注意:未定义指数:文件中的d:未定义指数:文件在d:\软件安装\ XAMPP \ htdocs中\威克斯安装\ XAMPP \ htdocs中\威克斯\用户\在线insert_galary.php 19

通知\ SOFTWARE \用户\ insert_galary.php第21行 对不起,我不能上传galaryColumn数在1行

回答

0

不匹配值计数的原因undefined index错误消息,是因为你的输入名称不匹配。

你有这样的:

<input type="file" name="img" class="tr"/> 

和你使用$file = $_FILES["file"]["name"];

其重命名为:

<input type="file" name="file" class="tr"/> 
+0

我修改,但仍然我得到同样的错误消息。 – Alex

+0

您是否重新加载页面?请给我确切的错误信息。 @Alex –

+0

是的,我重新加载了页面。看到我的更新错误消息。 – Alex