FormData ajax到php

时间:2017-12-26 06:37:18

标签: javascript php ajax

大家好我的系统有问题,我现在正在工作。
我正在通过ajax发送数据。问题是,当我试图回显我在php文件中发送的数据时,有undefying个变量的错误,但当我在php中进行var_dumping时它向我展示了这个array

  

array(20){[“------ WebKitFormBoundaryaw2Fqe4TLPU8WGtM   Content-Disposition:_form-data; name“] => string(948)”“pimage”;   filename =“C:\ fakepath \ 2772.png”Content-Type:image / png?PNG   IHDRæ$?SBIT 30 O?   pHYsp P | {?? tEXtSoftwarewww.inkscape.org ?? string(0)“”[“'()* _, - _ / 0123456789:;<”] =>串(415)   “> @ ABCDEFGHIJKLMNOPQRSTUVWXYZ [\] ^ _`ABCDEFGHIJKLMNOPQRSTUVWXYZ {|}〜????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????? 6?“?? IDATx ?? ?【T ????   !?DV ZT ?? * Y ?? [PQQü牛逼`?? - ???$ @ ????   ???(?? @(QH ???>有3 O 6-8 IBF {WY ????? = ??? {5ZΔ V ??????   H 1'I =?w ^ $ ??? ???#NFT?C??问GV ?????? string(5)“?)?%”“[”M?b?C?ry ?? z?`????? h?gVxA ?? 2 ??   ?? ?? $ * ?? _ 6EfyA?一?eI` ?? “NZ,正??????????? ^?LE ?? Xh6_pbT?HJ?#rZ ?? VDN?)'”? ???   ·K? ?y?_rZl?“] =>字符串(161)   “DN ??。??? '??????。·K?9A`52' ??? P 450 S'” 的tJ?   △:%Z ^△]△d“sR≈9s sR]]△dN A A7△B'ŽA?32'E?0 ?? o ?? B ???   ·K?81ZZ?B $ N + ^ ?? 6?!???? N + 9A`4?”   [ “G ?? _ ydtp ??” 米?t9T? “H?” ?? *?1|I ?? _ ?? XD?_h?4?我?01 20 FΔ 3])   ?Z`sk $ i5 |?JB ?? n?h ???? h?“] => string(0)”“[”?? ??> t?   ??? 9 ????? Ws的????   string(0)“”[“?????????????????????????????????????????????????????????????????????????   字符串(946)“7?,?V?FPh?MPHO ???? j?X   ^^#的ΔΣ4 @ M +;一个Δ P·X·K?M5?(?@ KH ???? d,?V 10 2,3 O 10

     
    

... @ DDX ??????? - ????2 B ????一个????一个δλ0 A 380%一????的ΔΣ P 25 @ķ     h ?? @ [αa≤0≤0≤a≤C≤9S≤O≤N≤2,ΔV     P {^%d ????? JHKT ??? j ??? 4Rh ?? 4 @ EhY -     PQ?1?,(14f ???? @ [x?a ????? de?|| Gv ??? @ [8 ?? @ k     HEdX ?? tDdX D?XF ?? / j> ?? k?u?J [?〜?] k}?85?     ????| - “L)〜-MF ????小号0Q ??? CH 8 ?? - @ $öL - @ ??&GT; ???ķ@h R(?????? @h·R·(?3 H·R·H 2 @h·R 20 F ????? 3 H·R     string(193)“?u ??”?SX?x?SIgz?Xh @ 8 ?? Id ??? P:CB9 ??     ???? {????; 5%???'?? $;?T $ ???? 8 ??? Ch @?X ?? /     ?è?? 4`Gk5 $ ??我沃顿??? 5“'?;     ?? 0 ???? 4`6?G?R?N?Ep?5`“[”???? n     (Σ(ΔN≤tAp≤5`4≤N≤Ep≤5≤α≤N≤3)n ????ª“〕     string(0)“”[“???? a ?? 4?/ ??”] =&gt; string(0)“”[“?? ^?     ?c?\??i?M?CX1?t`U"]=> string(0) "" ["?7?_??g#?/??~_C??_c??s?)yl???"?s????s??qb_??_?????#??1ue??1_???9?e?4?p9wLMJ4?M?C??4E?_?Q?4??D!?^?~4;?e?jY???n?U ?6?]?a??a??/?a????'??~????????6O9??{?wb?,fj?????9?_??6??vA??????,vPz??o?{fn?H???,??Cki ??f??7?z??????y5Y?;?r?? "]=> string(175) "??c???2????7?-j)?O???t~?>j??n???k/?????'? ? ?[????U?S?~UZ?????qn???????>?a?0? ?s#??w?=?Y?;:?R?W??v???Q??????a4???? t???}\???b?u1???x??\?z??^?1?" ["?q????f?<)'??; @?E3]"]=> string(32) "I?>!???:c9?????)?gX?a?~?" ["?L????Dx%???t??Dx??_???'????-?p??O?WL?T?K??#^??'G"{"]=> string(82) "]G?ry?ifP?r???;n??D?R??? ?p??@??m???@Sw???B??m"?7?y?????_?Zn?." [">?b??>??"]=> string(409) "!? 7:M?7?y???????$~?0????;?e(???0?E????g.?P??t}"?>?????q]??Y? 0RD?Bg???0n/wh"?w??D?;?m?????~G??g?? 0DH?pG?<?c?:??.?? ??N??????i???I C???r?Ÿ?? r?%?#??}???????w??]G??wE9?BV???'??F??}???<@?,'?}#V?8???n?a????????? .;~??iq??)p?M?-?@??xN?G????H*??d ?j?????????1IU????)??F,????????W[?,?y##???`?o8???!?????ulg?\?{??1?[:9b?S'??????w\?}?n???????q?z" ["?q?"]=> string(0) "" ["U1:???4?????{Y4?M??R]??J*??????m_?~??]T???????J???????_????I7????1n??q??$?0??Ow?x¬x??y?v?(x?????"]=> array(1) { ["_z??%?u05?:????q?????"]=> string(452) ""pname" test ------WebKitFormBoundaryaw2Fqe4TLPU8WGtM Content-Disposition: form-data; name="psupplier" 1 ------WebKitFormBoundaryaw2Fqe4TLPU8WGtM Content-Disposition: form-data; name="pquant" 123 ------WebKitFormBoundaryaw2Fqe4TLPU8WGtM Content-Disposition: form-data; name="pprice" 123 ------WebKitFormBoundaryaw2Fqe4TLPU8WGtM Content-Disposition: form-data; name="pdate" 2017-12-26 ------WebKitFormBoundaryaw2Fqe4TLPU8WGtM-- " } }

??@Sw???B??m"?7?y?????_?Zn?." [">?b??>??"]=> string(409) "!? 7:M?7?y???????$~?0????;?e(???0?E????g.?P??t}"?>?????q]??Y? 0RD?Bg???0n/wh"?w??D?;?m?????~G??g?? 0DH?pG?<?c?:??.?? ??N??????i???I C???r?Ÿ?? r?%?#??}???????w??]G??wE9?BV???'??F??}???<@?,'?}#V?8???n?a????????? .;~??iq??)p?M?-?@??xN?G????H*??d ?j?????????1

here's my ajax code.

function getName(key)
{
    key = document.getElementsByName(key)[0];
    return key;
}

function getId(key)
{
    key = document.getElementById(key);
    return key;
}

function message(id, message, color = 'black')
{
    getId(id).style.color = color;
    getId(id).innerHTML = message;
}

function loadImage(handler)
{
    if(handler.files && handler.files[0])
    {
        var reader = new FileReader();

        reader.onload = function(e)
        {
            getId('productImagepreview').src = e.target.result;
        }

        reader.readAsDataURL(handler.files[0]);
    }
}

function xhrCreateProduct()
{
    var url = getId('formcProduct').getAttribute('action');

    var image = getName('prodimage');
    var name = getName('prodname');
    var supplier = getName('prodsupplier');
    var quantity = getName('prodquant');
    var price = getName('prodprice');
    var date = getName('proddate');

    var imagevalue = image.value.trim();
    var namevalue = name.value.trim();
    var suppliervalue = supplier.value.trim();
    var quantityvalue = quantity.value.trim();
    var pricevalue = price.value.trim();
    var datevalue = date.value.trim();

    var valid_image = imagevalue.substring(imagevalue.lastIndexOf('.')+1).toLowerCase();


    if(imagevalue == null || imagevalue == '')
    {
        message('msg_pimage', '* Provide product image', 'red');
        image.focus();
        return false;
    }
    else
    {
        message('msg_pimage', '');
    }

    if(valid_image != 'jpg' && valid_image != 'png' && valid_image != 'gif')
    {
        message('msg_pimage', '* File is not an image type', 'red');
        image.focus();
        return false;
    }
    else
    {
        message('msg_pimage', '');
    }

    if(namevalue == null || namevalue == '')
    {
        message('msg_pname', '* Provide product name', 'red');
        name.focus();
        return false;
    }
    else
    {
        message('msg_pname', '');
    }

    if(suppliervalue == null || suppliervalue == '')
    {
        message('msg_psupplier', '* Provide product supplier', 'red');
        supplier.focus();
        return false;
    }
    else
    {
        message('msg_psupplier', '');
    }

    if(quantityvalue == null || quantityvalue == '')
    {
        message('msg_pquant', '* Provide product quantity', 'red');
        quantity.focus();
        return false;
    }
    else
    {
        message('msg_pquant', '');
    }

    if(pricevalue == null || pricevalue == '')
    {
        message('msg_price', '* Provide product price', 'red');
        price.focus();
        return false;
    }
    else
    {
        message('msg_price', '');
    }

    if(datevalue == null || datevalue == '')
    {
        message('msg_pdate', '* Provide the date product come', 'red');
        date.focus();
        return false;
    }
    else
    {
        message('msg_pdate', '');
    }

    var xhr = null;

    if(window.XMLHttpRequest)
    {
        xhr = new XMLHttpRequest();
    }
    else
    {
        xhr = new ActiveXObject('Microsoft.XMLHTTP');
    }

    var sendData = new FormData();

    sendData.append('pimage', image.files[0], imagevalue);
    sendData.append('pname', namevalue);
    sendData.append('psupplier', suppliervalue);
    sendData.append('pquant', quantityvalue);
    sendData.append('pprice', pricevalue);
    sendData.append('pdate', datevalue);


    xhr.open('POST', url, true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send(sendData);

    xhr.onreadystatechange = function()
    {
        var OK = 4;
        var DONE = 200;

        if(xhr.readyState == OK && xhr.status == DONE)
        {
            //var response = JSON.parse(xhr.responseText);
            document.write(xhr.responseText);
        }
    }
}

here's my html code

<form id="formcProduct" action="<?= URL; ?>product/create" method="post" enctype="multipart/form-data" onsubmit="return false;">
          <div class="form-group">
            <div class="form-row">
              <div class="col-md-6">
                <img id="productImagepreview" class="form-control customcss_image_size" src="<?= Setting::imageLocation('public/product_image/no_photo_1x.jpg'); ?>" />
                <label for="exampleInputImage">Select an image</label>
                <input class="form-control" id="exampleInputImage" name="prodimage" type="file" onchange="loadImage(this);" accept="image/*">
                <span id="msg_pimage"></span>
              </div>
              <div class="col-md-6">
                <label for="exampleInputPname">Product Name</label>
                <input class="form-control" id="exampleInputPname" name="prodname" type="text" aria-describedby="nameHelp" placeholder="Enter Product name">
                <span id="msg_pname"></span><br/>

                <label for="exampleInputSupplier">Select Supplier</label>
                <select class="form-control" id="exampleInputSupplier" name="prodsupplier">
                    <?php
                        if(sizeof($this->productSupplier) > 0)
                        {
                            foreach($this->productSupplier as $ps)
                            {
                                echo '<option value="'.$ps['col_id'].'">'.$ps['col_supplier_name'].'</option>';
                            }
                        }
                    ?>
                </select>
                <span id="msg_psupplier"></span><br/>

                <label for="exampleInputQuantity">Quantity</label>
                <input class="form-control" id="exampleInputQuantity" name="prodquant" type="number" aria-describedby="nameHelp" placeholder="0">
                <span id="msg_pquant"></span><br/>

                <label for="exampleInputPrice">Price</label>
                <input class="form-control" id="exampleInputPrice" name="prodprice" type="text" aria-describedby="nameHelp" placeholder="&#8369; 0.00">
                <span id="msg_price"></span><br/>

                <label for="exampleInputLastName">Date</label>
                <input class="form-control" id="exampleInputLastName" name="proddate" type="date">
                <span id="msg_pdate"></span>
              </div>
            </div>
          </div>
          <input type="submit" class="btn btn-primary btn-block" value="Create Product" onclick="xhrCreateProduct();" />
        </form>

here's my php file where fetching all the datas that i'm sending

var_dump($_POST);
var_dump($_FILES);

and by the way i have another problem the image file it gives me no result when i'm var_dumping it. but it shows in post var_dump value.

。或其他第三方。

1 个答案:

答案 0 :(得分:0)

而不是在ajax中放置请求标头。为什么不删除它?