【问题标题】:How to set img src to php session in jquery如何在 jquery 中将 img src 设置为 php 会话
【发布时间】: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


【解决方案1】:

您应该只使用一种形式,以便myfile 也将发送到matrimony_action.php,例如:

<form  action = "matrimony_action.php" method="post" name = "matrimony" onsubmit="return validate()" enctype="multipart/form-data">
    <label>Upload your closeup photo</label>
    <input onchange="readURL(this);" type="file" name="myfile"/>

    <input class="btn btn-action pull-left" type="submit" value="Submit" name="submitted8" id="submitted8">
</form>

<img alt="Image Display Here" id="test" src="assets/images/nopic.jpg" height="500px" width="450px" style="display:block;"/>

注意:您不需要 session,因为 img 文件将与表单一起提交。

希望这会有所帮助。

【讨论】:

  • 谢谢.. 我可以在表单标签之外使用这个语句吗? ''
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
相关资源
最近更新 更多