2014-01-14 31 views
0

我的代碼正在上傳任何我想要的東西。當我上傳圖片或文件時,它應該立即顯示在選擇上傳後(即在HTML「input name =」upload「type =」submit「onChange =」pict()「class =」box「 id =「upload」value =「Upload」>)。問題在於它沒有通過「xmlhttp.open(」POST「,'show.php',true)」進入頁面show.php;「。調試和不明白這是爲什麼不將其他頁面參數未輸入xmlhttp.open(「POST」,'show.php',true);

show.php

<?php 
echo"heloo00"; 
$con=mysql_connect("localhost","root",''); 
mysql_select_db("project",$con) or die("error db"); 

$sql="select * from upload"; 
$query=mysql_query($sql); 
while($row=mysql_fetch_array($query)) 
{ 
$image=$row ['name']; 

echo '<img src="data:image/png;base64,' . base64_encode($row['content']) . '" />'; 
} 
?> 

t.html

<html> 
<head> 
<style> 
#boox{ 
    overflow:auto; 
    width:600px; 
    height:400px; 
    } 
</style> 

<script> 
alert("helo"); 
</script> 
<script> 

    function pict() 
    { alert("hel"); 
     xmlhttp = new XMLHttpRequest(); 
     var picInput = document.getElementById('userfile').value; 
     var uploadpic = document.getElementById('upload').value; 
     document.getElementById('usf').innerHTML = picInput; 
     document.getElementById('upic').innerHTML = uploadpic; 
     xmlhttp.onreadystatechange = function() { 

     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
     var resp = xmlhttp.responseText; 
     alert(resp); 
     } alert("a"); 
     xmlhttp.open("POST", 'show.php', true); //problem 
xmlhttp.send();// not happening 
    } 

    }  
    </script> 
    </head> 
    <body> 
    <form method="post" enctype="multipart/form-data" action="take.php"> 
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> 
<tr> 
<td width="246"> 
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"> 
<input name="userfile" type="file" id="userfile"> 
</td> 
<input name="upload" type="submit" onChange ="pict()" class="box" id="upload" value=" Upload "> 
</tr> 
</table> 
</form> 
    </body> 
    </html> 

take.php

<?php 
    if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) 
     { 
     $fileName = $_FILES['userfile']['name']; 
     $tmpName = $_FILES['userfile']['tmp_name']; 
     $fileSize = $_FILES['userfile']['size']; 
     $fileType = $_FILES['userfile']['type']; 
     $fp  = fopen($tmpName, 'r'); 
     $content = fread($fp, filesize($tmpName)); 
     $content = addslashes($content); 
     fclose($fp); 
     if(!get_magic_quotes_gpc()) 
     { 
      $fileName = addslashes($fileName); 
     } 
     $con=mysql_connect("localhost","root",''); 
     mysql_select_db("project",$con) or die("error db"); 

     $query = "INSERT INTO upload (name, size, type, content) ". 
     "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; 
     mysql_query($query) or die('Error, query failed'); 
     mysql_close($con); 
     echo "<br>File $fileName uploaded<br>"; 

     } 
     ?> 

回答

0

的onChange = 「PICT()」是沒有得到調用。 把阿賈克斯電話放在適當的地方...

如果你想提交併顯示圖像在一個接一個..然後使用ajax提交圖像也不是POST。

+0

我是如何放置合適的。我把它放在哪裏? – user3187733

+0

我很困惑,它以這種方式工作: - 上傳圖片 - >立即顯示圖像,正如我們上傳圖片時一樣。 – user3187733

+0

當您發佈表單上傳圖片時,您可以使用相同的圖片,而上傳後再次加載頁面。 沒有必要在您的代碼示例 中使用ajax,一旦您在t.html中按下提交,圖像將被上傳到您的服務器。 ,會顯示一條消息,圖片上傳自take.php 現在轉到瀏覽器中的show.php,您將在從數據庫中獲取圖像時獲取圖像 – Learner