如何在同一个div中填充响应,该div被填充为ajax

时间:2021-06-09 14:29:33

I have a html page

我有一个HTML页面

say main.php, which populates "upload.php" in div using ajax.

比较main.php,它使用ajax在div中填充“upload.php”。

Now in this upload.php, I upload an image, and I want to somehow, make this div in main.php to show me the response of image load success or not.

现在在这个upload.php中,我上传了一个图像,我想以某种方式,在main.php中创建这个div,向我展示图像加载成功与否的响应。

my code is shown below:

我的代码如下所示:

The main.php

main.php

<script type="text/javascript">
function showHint()
{
    var str = document.form1.filenumber.value;
    if (str.length==0)
      { 
      document.getElementById("txtHint").innerHTML="";
      return;
      }
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","upload.php?filenumber="+str,true);
    xmlhttp.send();
}
</script>
</head>
<body>

<form name="form1" >    
    <tr>
        <td>File Number </td><td><input type="text" id="11" name="filenumber" /></td>

</form>
        <td colspan="2"><input type="submit" onclick='showHint()'></td>
    </tr>   

</table>
<div id="txtHint"></div>

And the upload.php

和upload.php

<?php   
$filenumber = clean($_GET['filenumber']);

if($filenumber != '') {
    $qry = "SELECT * FROM profile WHERE filenum='$filenumber'";
    $result = mysql_query($qry)
    or die(mysql_error());  
    $row = mysql_fetch_array( $result );



    if($result) {
        if(mysql_num_rows($result) == 0) {
            $errmsg = '<div style="width:300px; height:100px;color:red;margin:0px auto;position:relative;top: 30%">No such record found. Redirecting back to the status page. </p>';
            $errflag = true;
        }   
        else
        {
        ?>      

            <form enctype="multipart/form-data" action="uploader.php" method="POST">
             Please choose a file: <input name="uploaded" type="file" /><br />
             <input type="submit" value="Upload" />
             </form> 

        <?php
        }
        //@mysql_free_result($result);
    }
    else {
        die("Query111 failed");
    }
}
?>

And the upload.php calls for uploader.php, whose response I want to show in the same div

upload.php调用uploader.php,我想在同一个div中显示其响应

    <?php 
    $target = "images/"; 
    $target = $target . basename( $_FILES['uploaded']['name']) ; 
    $ok=1; 
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
    {
        echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    } 
    else {
        echo "Sorry, there was a problem uploading your file.";
 }
 ?> 

You suggestion are most welcome and your help is appreciated.

您的建议非常受欢迎,我们非常感谢您的帮助。

Thanks Zee

谢谢Zee

1 个解决方案

#1


1  

As GolezTrol mentioned, you should consider using JQuery, it makes working with AJAX a lot easier.

正如GolezTrol所提到的,您应该考虑使用JQuery,这使得使用AJAX变得更加容易。

To answer your question, you could use a JSON encoded response to be able to seperate information about the result of the uploading process and the real result you want to fill your div with.

要回答您的问题,您可以使用JSON编码的响应,以便能够分离有关上传过程结果的信息以及您想要填充div的真实结果。

#1


1  

As GolezTrol mentioned, you should consider using JQuery, it makes working with AJAX a lot easier.

正如GolezTrol所提到的,您应该考虑使用JQuery,这使得使用AJAX变得更加容易。

To answer your question, you could use a JSON encoded response to be able to seperate information about the result of the uploading process and the real result you want to fill your div with.

要回答您的问题,您可以使用JSON编码的响应,以便能够分离有关上传过程结果的信息以及您想要填充div的真实结果。