参数未输入xmlhttp.open(“POST”,“show.php”,true);

时间:2014-01-14 08:48:30

标签: javascript php html

我的代码正在上传我想要的任何内容。当我上传图片或文件时我应该做什么,它应该在选择上传后立即显示(即在html“输入名称=”上传“type”“提交”onChange =“pict()”class =“box” id =“upload”value =“Upload”>)。问题是它没有通过“xmlhttp.open(”POST“,”show.php',true)进入show.php页面;“。虽然我做了调试,但没能理解为什么这不会转到其他页面

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>";

        }
      ?>

1 个答案:

答案 0 :(得分:0)

onChange =“pict()”未被调用。 把ajax电话放在适当的地方......

如果您想要一个接一个地提交和显示图像..然后使用ajax提交图像而不是POST。