简体   繁体   English

将多个值(文件)从javascript发送到php

[英]send multiple value (file) from javascript to php

I have this code 我有这个代码

var action = $("#action").val();
var z_product_id = $("#z_product_id").val(); 
var product_id = $("#product_id").val(); 
$.post('ajaxGetOperations.php',
    {action: action, z_product_id: z_product_id, product_id: product_id},
    function(data)
    {
        $('#respons').html(data);
        $('#hand_product_id').val(product_id);
    }) ;                                         

} }

I can send the "action" and "Z_product" and ... in this line to php page 我可以在此行中将“ action”和“ Z_product”和...发送到php页面

{action: action, z_product_id: z_product_id, product_id: product_id}

but I don't know what do I do for send file value to php page. 但我不知道该如何将文件值发送到php页面。
this is may html code 这可能是HTML代码

<table class="table table-striped table-hover">  
    <tr>
        <td><input type='text' size="4" value='$z_product[id]' id='z_product_id' /></td>
        <td>$z_product[name]</td>                          
        <td><input type='text' size="4" value='$product[id]' id='product_id' /></td>
        <td>$product[name]</td>                                                              
    </tr> 
    <tr>      
        <td colspan='4'>
            <div class="col-lg-6 col-sm-6 col-md-6 -col-xs-12">
                <label class="" for="filebutton">ارسال عکس</label>
                <input id="image" name="image" class="input-file" type="file">
            </div>
        </td>                                                               
    </tr>                                                               
    </tr>         
    <tr>      
        <td></td>
        <td></td>     
        <td></td>     
        <td>
            <button onclick='setDBInfo(this.value)' value='1' id='action'>ارسال </button>
            <button onclick='setDBInfo(this.value)' value='0' id='action'>رد </button>
        </td>                                                               
    </tr>
</table> 

actually I couldn't access the file value in php. 实际上我无法访问php中的文件值。
I want to access multiple file value in php whit javascript .post? 我想在php whit javascript .post中访问多个文件值?
how can I do this? 我怎样才能做到这一点?

The easiest way is to wrap your table in a form and send it to the php page. 最简单的方法是将表格包装成表格并将其发送到php页面。 But if you don't want to do it that way, then you'll need to write js code to fetch the file value from the HTML element. 但是,如果您不想那样做,则需要编写js代码以从HTML元素中获取文件值。

Try This 尝试这个

<form name="my_form">
<table class="table table-striped table-hover">  
    <tr>
        <td><input type='text' size="4" name="z_product_id" value='$z_product[id]' id='z_product_id' /></td>
        <td>$z_product[name]</td>                          
        <td><input type='text' size="4" name="product_id" value='$product[id]' id='product_id' /></td>
        <td>$product[name]</td>                                                              
    </tr> 
    <tr>      
        <td colspan='4'>
            <div class="col-lg-6 col-sm-6 col-md-6 -col-xs-12">
                <label class="" for="filebutton">ارسال عکس</label>
                <input id="image" name="image" class="input-file" type="file">
            </div>
        </td>                                                               
    </tr>                                                               
    </tr>         
    <tr>      
        <td></td>
        <td></td>     
        <td></td>     
        <td>
            <button type="submit" value='1' id='action'>ارسال </button>
            <button onclick='setDBInfo(this.value)' value='0' id='action'>رد </button>
        </td>                                                               
    </tr>
</table> 
</form>

and replace your ajax request with this 并以此替换您的ajax请求

$("form[name='my_form']").submit(function(e) {
    var formData = new FormData($(this)[0]); /* this is your data */

        $.ajax({
            url: "ajaxGetOperations.php",
            type: "POST",
            data: formData,
            async: false,
            success: function (data) {
                $('#respons').html(data);
                $('#hand_product_id').val(product_id);
            },
            cache: false,
            contentType: false,
            processData: false
        });

           e.preventDefault();
        });

in your php retrieve the values as usual 在您的PHP中照常检索值

$p_id = $_POST['z_product_id'];
$p_id = $_FILES['image'];

In short using the formData object would really help you... 简而言之,使用formData对象确实可以帮助您...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM