【发布时间】:2016-06-09 13:30:56
【问题描述】:
我在 php 中创建了一个程序。在这个程序中,我想在 php 中提交表单之前预览上传的图像。这是我的 html 表单代码。
<form id="form1" enctype="multipart/form-data">
<label>Upload your closeup photo</label>
<input onchange="readURL(this);" type="file" name="myfile"/>
</form>
<img alt="Image Display Here" id="test" src="assets/images/nopic.jpg" height="500px" width="450px" style="display:block;"/>
<form action = "matrimony_action.php" method="post" name = "matrimony" onsubmit="return validate()" enctype="multipart/form-data">
<input class="btn btn-action pull-left" type="submit" value="Submit" name="submitted8" id="submitted8">
</form>
这是我在“输入类型文件”上调用的 javascript 函数代码
function readURL(input)
{
if (input.files && input.files[0])
{
var reader = new FileReader();
reader.onload = function (e)
{
$('#test').attr('src', e.target.result);
//$("#test").css("display", "block");
$('#test').css({"display":"block"});
}
reader.readAsDataURL(input.files[0]);
}
$.session.set("img", "hello"); // i am not sure about this statement.
}
在 php 文件中。我写了以下几行。
<?php
$photo1 = $_SESSION["img"];
echo $photo1;
?>
在提交表单之前可以更好地预览图像。最后我收到错误“未定义的索引:img”。 请帮帮我。
【问题讨论】:
-
你不能在JS代码中调用
$.session.set("img", "hello");。 -
感谢您的回复。然后告诉我该怎么做?
-
很难准确说出你在问什么,
test输入上显示的图像是什么?您希望echo $photo1;显示什么(img 的 src 或名称 ...)? -
上传后,图像在测试输入中完美显示。在这之后。我想通过提交表单“婚姻”将其存储在 mysql 数据库中。
-
可以正常提交表单到php。
标签: javascript php jquery session