We can't upload file using normal Ajax. For that we should use advanced javascript frameworks or JSON for passing file content. Also this javascript will not work if it is disabled from the browser.
But we can get this Ajax effect without using ajax or javascript by simply depending on target attribute of the form tag.
Below is the sample script to achive this.
File name: form.html
<iframe src="http://www.genengnews.com/app_themes/genconnect/images/default_profile.jpg" id="targetFrame" name="targetFrame" style="border: 0; height:230px; width: 230px;"></iframe>
<form action="upload.php" target="targetFrame" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
File name: upload.php
<?php
if(isset($_FILES["file"])) {
// You can add file uploading script here
echo "Temporary file location is: " . $_FILES["file"]["tmp_name"] . "<br />";
if(substr($_FILES["file"]["type"], 0, 5) == "image") {
echo '<img src="' . $_FILES["file"]["tmp_name"] . '" alt="" />';
}
?>
<?php
}
?>
No comments:
Post a Comment