无页面刷新上传图像

时间:2013-12-19 21:05:05

标签: javascript php

大家好我正在使用下面的代码上传图像而不刷新页面而且效果很好..但是我想使用php并回显javascript,但双引号或单引号出错...查看我的代码吼叫

<script>
function ajaxFileUpload(upload_field)
{
var re_text = /\.jpg|\.gif|\.jpeg/i;
var filename = upload_field.value;
if (filename.search(re_text) == -1) {
alert("File should be either jpg or gif or jpeg");
upload_field.form.reset();
return false;
}
document.getElementById("picture_preview").innerHTML = "<div><img src="images/ajax-loader.gif" border="0" /></div>";
upload_field.form.action = "upload-picture.php";
upload_field.form.target = "upload_iframe";
upload_field.form.submit();
upload_field.form.action = "";
upload_field.form.target = "";
return true;
}
</script>


<style>
#picture_preview img {
height:150px;
border:2px dotted #666666;
}
</style>


<div id="picture_preview" class="myclass"></div>
<!-- iframe used for ajax file upload-->
<!-- debug: change it to style="display:block" -->
<iframe name="upload_iframe" id="upload_iframe" style="display:none;"></iframe>
<!-- iframe used for ajax file upload-->

<form name="pictureForm" method="post" autocomplete="off" enctype="multipart/form-data">
<div>
<span>Upload Picture :</span>
<input type="file" name="picture" id="picture" onchange="return ajaxFileUpload(this);" />
<span id="picture_error"></span>

</div>
</form>

我试图像这样回复javascript代码:

<?php

echo '
 <script>
    function ajaxFileUpload(upload_field)
    {
    var re_text = /\.jpg|\.gif|\.jpeg/i;
    var filename = upload_field.value;
    if (filename.search(re_text) == -1) {
    alert("File should be either jpg or gif or jpeg");
    upload_field.form.reset();
    return false;
    }
    document.getElementById("picture_preview").innerHTML = "<div><img src="images/ajax-loader.gif" border="0" /></div>";
    upload_field.form.action = "upload-picture.php";
    upload_field.form.target = "upload_iframe";
    upload_field.form.submit();
    upload_field.form.action = "";
    upload_field.form.target = "";
    return true;
    }
    </script>';
?>

我在这一行得到了错误:

document.getElementById("picture_preview").innerHTML = "<div><img src="images/ajax-loader.gif" border="0" /></div>";

1 个答案:

答案 0 :(得分:2)

  

我在这一行得到了错误:`

document.getElementById("picture_preview").innerHTML = "<div><img src="images/ajax-> loader.gif" border="0" /></div>";

首先,你没有逃避双引号......

document.getElementById("picture_preview").innerHTML = "<div><img src=\"images/ajax-loader.gif\" border=\"0\" /></div>";
  

参考

在大多数/所有编程语言中,你不能做这样的事情:

var = "John said "hello" and I said "hello to you too!"";

您需要使用double和single的组合替换引号,如下所示:

var = 'John said "hello" and I said "hello to you too!"';

...或逃避您的报价如下:

var = "John said \"hello\" and I said \"hello to you too!\"";