簡體   English   中英

將多個值(文件)從javascript發送到php

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

我有這個代碼

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

}

我可以在此行中將“ action”和“ 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對象確實可以幫助您...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM