【问题标题】:pass a hidden input value to php将隐藏的输入值传递给 php
【发布时间】:2013-01-29 20:31:12
【问题描述】:

我找到了这个代码来上传个人资料图片,

http://www.phpdevblog.eu/2011-04/combined/jquery-ajax-and-php-based-profile-image-upload-without-reloading-the-page.html#comments

一切都很好。但我想设置一个名称 file = user_id

我如何将输入的隐藏 user_id 值传递给 php 上传文件进程?

var uploadURL = "processupload.php";
      $(document).ready(function(){
                $('a#uploadFile').file();
                $('a#delete').click(function(){
                      $('input#profileImageFile').val("");
                      $('img#profileImage').attr("src","/images/styles/profileBlank.jpg");
                      $('div#messageBox').html("Image deleted !");
                      $('div#messageBox').attr("class","success");
                      $('a#delete').hide();
                });
                $('input#uploadFile').file().choose(function(e, input) {

                    input.upload(uploadURL, function(res) {
                        if (res=="invalid"){
                            $('div#messageBox').attr("class","error");
                            $('div#messageBox').html("Invalid extension !");
                        }else{
                            $('div#messageBox').attr("class","success");
                            $('div#messageBox').html("Imagen cargada !");
                            $('img#profileImage').attr("src","/images/avatars/"+res);
                            $('input#profileImageFile').val(res);
                            $('a#delete').show();
                            $(this).remove();
                        }
                    }, '');                  
          });
           });

html

<div class="imageContainer">
            <img alt=""  src="/images/avatars/<?php echo $row_rs_user['user_image']; ?>" width="150" height="150" id="profileImage">
            <a href="#" id="uploadFile" title="Upload"><img alt=""  src="/images/styles/upload.jpg"></a>
            <a href="#" id="delete" title="Delete" style="display:none;position:relative;z-index:999999;"><img alt=""  src="/images/styles/delete.jpg"></a>
<input type="hidden" name="user_id" value="<?php echo $row_rs_user['user_id']; ?>">
            <div id="messageBox"></div>
    </div>

PHP上传过程

if(isset($_POST))
{
.
.
.
.

【问题讨论】:

  • 通过 POST 发送 user_id 不是一个好主意。只需使用会话中的用户 ID(我猜用户必须登录才能拥有 user_id?),这样更安全。
  • 好主意...问题解决了。谢谢

标签: php jquery image profile image-uploading


【解决方案1】:

我不熟悉您用于处理上传的插件,但如果表单元素按应有的方式访问 PHP,您只需调用

$fileId = $_POST['user_id'];

这将在您的 PHP 进程中设置一个等于隐藏值的变量。确保将其包含在 ISSET 条件中,以免导致任何错误。

【讨论】:

  • 只有一个元素进入PHP(输入上传文件)表单上的其他元素,不要来,因为没有提交表单就会发生这种情况
  • 明白了.. 好的。在这种情况下,我同意 John 关于会话使用的观点。
【解决方案2】:

只需通过uploadURL传递信息:

input.upload(uploadURL + "?user_id=" + $("input[name=user_id]").val(), function(res) {});

确保包含用户 ID 的隐藏输入的名称是“user_id”。 在 PHP 中的服务器端,您可以使用 isset($_REQUEST['user_id']) 读取变量的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2021-01-31
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多