2013-01-17 22 views
0
<div class="hideAfterSuccess"> 
        <label for="avatar">Upload an Avatar :</label> 
        <input type="file" name="avatar" id="avatar" /> 
       </div> 

和下方是具有对图像的位置传递到另一个文件(parsePortal.php)一个PHP函数jquery的。我能够通过文件的其他类型的数据,但图像失败。我认为,因为它发布图像的位置字符串,我可以使用$ _FILES [$ _ POST ['image']] ['name'],然后上传图像。我尝试过网络,当然,没有找到我需要的东西。如何使用jquery的回调函数将图像位置传递给php函数?

 $(function(){ 
      var imageLoc = $('avatar').val(); 
      var url = "parsePortal.php"; 
       $.post(url, { 
        status : "getimage", 
        image : "imageLoc" 
       }, function(result){ 

       }); 
      }); 
+0

你尝试了一个HTTP POST的PHP? – insomiac

+0

是的,我做过。但它仍然失败。它只会传递一个空字符串。 –

回答

0

我没有成功使用jQuery插件的形式它。这意味着它发送了我从中获得文件名并打印出来的整个路径。感谢你,没有这种帮助,我没有办法做到这一点。

这里是javascript。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script src="http://malsup.github.com/jquery.form.js"></script> 

<script> 
     // prepare the form when the DOM is ready 
     $(document).ready(function() { 
      // bind form using ajaxForm 
      $('#htmlForm').ajaxForm({ 
       // target identifies the element(s) to update with the server response 
       target: '#htmlExampleTarget', 

       // success identifies the function to invoke when the server response 
       // has been received; here we apply a fade-in effect to the new content 
       success: function() { 
        $('#htmlExampleTarget').fadeIn('slow'); 
       } 
      }); 
     }); 
</script> 

这里是HTML

<form id="htmlForm" action="file.php" method="post"> 
Message: <input type="text" name="message" value="Hello HTML" /> <p></p> 
Image : <input type="file" name="image" id="image" /><p></p> 
<input type="submit" value="Echo as HTML" /> 

,这里是

$image = $_FILES['image']['tmp_name']; 
$name = $_FILES['image']['name']; 
move_uploaded_file($image, "uploads/".$name); 
echo '<div style="background-color:#ffa; padding:20px">' . $_POST['message'] . '</div>'; 
echo '<img src="uploads/'.$name.'"/>'; 
0

不幸的是,通过ajax传递文件到php并不是那么简单。然而,有一个很好的图书馆,让你轻松做到这一点:

http://malsup.com/jquery/form/