duaiwu8385 2015-04-29 09:34
浏览 469

如何使用FormData将输入值传递给ajax?

I am trying to upload images using ajax and php and I have managed to make it work to a certain level successfully, but I can't make my input's value (ID) pass to my php file.

Here's my scenario.

FORM

<form enctype="multipart/form-data" id="myform">
        <input type="file" name="images[]" multiple id="image"/>
</form>

Button

<button type="button" id="uploadimages" name="thing_id" 
value="<?php echo $row['thing_id']; ?>" class="btn btn-primary">Save changes
</button>

AJAX

    $("#uploadimages").click(function () {
        var form = new FormData($('#myform')[0]);

        // Make the ajax call
        $.ajax({
            url: 'uploadimages.php',
            type: 'POST',
            xhr: function () {
                var myXhr = $.ajaxSettings.xhr();
                if (myXhr.upload) {
                    myXhr.upload.addEventListener('progress', progress, false);
                }
                return myXhr;
            },
            //add beforesend handler to validate or something
            //beforeSend: functionname,
            success: function (res) {
                $('#content_here_please').html(res);
                /**/
            },
            //add error handler for when a error occurs if you want!
            //error: errorfunction,
            data: form,
            cache: false,
            contentType: false,
            processData: false
        });
    });

PHP

$get_id = $_POST['']; // This is where I stuck.

How can I pass $row['thing_id'] to $get_id variable? I can pass it using another GET ajax call on success, but then I lose the image_id value (I use foreach since I may upload multiple files), so I want to handle it in the same php file.

I didn't include my upload script because it works unless I try to make something using thing_id.

  • 写回答

3条回答 默认 最新

  • drbi19093 2015-04-29 09:40
    关注

    in your ajax code pass the data attribute

    $.ajax({
            url: 'uploadimages.php',
            type: 'POST',
            data:{upId:$("#uploadimages").val(),images:$('#uploadFieldId').val()},
            xhr: function () {
                var myXhr = $.ajaxSettings.xhr();
                if (myXhr.upload) {
                    myXhr.upload.addEventListener('progress', progress, false);
                }
                return myXhr;
            },
            //add beforesend handler to validate or something
            //beforeSend: functionname,
            success: function (res) {
                $('#content_here_please').html(res);
                /**/
            },
            //add error handler for when a error occurs if you want!
            //error: errorfunction,
            cache: false,
            contentType: false,
            processData: false
        });
    

    then you can access the upId in your php file as,i have made an updation so that you can send the upload file field value also to the php file. Change the id so that it matches the field and you can retrieve this field using

    $get_id = $_POST['upId'];

    $image = $_POST['images'];

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。