将隐藏的输入值传递给php

时间:2013-01-29 20:31:13

标签: php jquery image profile image-uploading

我发现此代码上传了个人资料图片,

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))
{
.
.
.
.

2 个答案:

答案 0 :(得分:0)

我不熟悉您用来处理上传的插件,但如果表单元素按照他们应该的那样点击PHP,您只需调用

$ fileId = $ _POST ['user_id'];

这将在PHP Process中设置一个等于隐藏值的变量。确保将其包含在ISSET条件内,以免引起任何错误。

答案 1 :(得分:0)

只需通过uploadURL传递信息:

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

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