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

send multiple value (file) from javascript to php

本文关键字:javascript php 文件      更新时间:2023-09-26

我有这个代码

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);
    }) ;                                         

}

我可以发送"操作"answers"Z_product"。。。在这行到php页面

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

但我不知道该怎么做才能将文件值发送到php页面
这可能是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> 

实际上我无法访问php中的文件值。
我想访问php whit javascript.post中的多个文件值?
我该怎么做?

最简单的方法是用表单包装表并将其发送到php页面。但是,如果您不想这样做,那么您需要编写js代码来从HTML元素中获取文件值。

试试这个

<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>

并用这个替换您的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();
        });

在php中,像往常一样检索的值

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

简而言之,使用formData对象将真正帮助您。。。