如何在JQuery Post PHP&之后删除_POST中的空行jQuery的

时间:2015-10-19 13:21:11

标签: javascript php jquery ajax

我用ajax Jquery发了一篇文章但效果很好,但结果是这样的:

    Array
    (
    [facdatas] => Array
    (
        [0] => Array
            (
                [mbr_id] => 26
            )

        [1] => Array
            (
                [nom] => Gautier Albert
            )

        [2] => Array
            (
                [adresse] => Avenue du Devin du Village 51
            )

        [3] => Array
            (
                [ville] => 1406 Cronay
            )

        [4] => Array
            (
                [concerne] => TEST
            )

        [5] => Array
            (
                [totalFac] => 118.00
            )

    )

[ligneFac] => Array
    (
        [0] => Array
            (
                [designation] => Veste de training CPNS
            )

        [1] => Array
            (
                [unite] => pièces
            )

        [2] => Array
            (
                [quantite] => 1
            )

        [3] => Array
            (
                [prixUnite] => 49
            )

        [4] => Array
            (
                [taxe] => 0.00
            )

        [5] => Array
            (
                [totLine] => 49.00
            )

        [6] => Array
            (
                [designation] => Pantalon de training CPNS
            )

        [7] => Array
            (
                [unite] => pièces
            )

        [8] => Array
            (
                [quantite] => 1
            )

        [9] => Array
            (
                [prixUnite] => 69
            )

        [10] => Array
            (
                [taxe] => 0.00
            )

        [11] => Array
            (
                [totLine] => 69.00
            )

        [12] => Array
            (
                [designation] => 
            )

        [13] => Array
            (
                [unite] => 
            )

        [14] => Array
            (
                [quantite] => 
            )

        [15] => Array
            (
                [prixUnite] => 
            )

        [16] => Array
            (
                [taxe] => 0.00
            )

        [17] => Array
            (
                [totLine] => 0.00
            )
    ... ... ...

就像你可以从行[12]看到的行是空的或者金额为0.00,我试图用array_filter删除这些行($ _ POST [' ligneFac']) 但结果是一样的。 有一种方法可以删除第一列为空的所有行吗?

感谢您的帮助

这是向表单添加行的代码:

    $("#insLines").click(function()
{
    $("#matable tbody").append('<tr>'+
                            '<td width="10px">'+
                                '<img id="insArticle" src="../../images/icon_add.png" width="16" height="16">'+
                            '</td>'+
                            '<td width="250px">'+
                                '<input name="ligneFac[][designation]" type="text" class="facBig" />'+
                            '</td>'+
                           ' <td width="30">'+
                                '<input name="ligneFac[][unite]" type="text" class="facSmall" />'+
                            '</td>'+
                           ' <td width="30">'+
                                '<input name="ligneFac[][quantite]" type="text" class="facSmall" />'+
                            '</td>'+
                            '<td width="30">'+
                                '<input name="ligneFac[][prixUnite]" type="text" class="facSmall" value="" />'+
                            '</td>'+
                            '<td width="30">'+
                                '<input name="ligneFac[][taxe]" type="text" class="facSmall" value="" />'+
                            '</td>'+
                           ' <td width="30">'+
                                '<input name="ligneFac[][totLine]" type="text" class="facSmall" value="" />'+
                            '</td>'+
                       '</tr>');

                        iconeClicable();
});

4 个答案:

答案 0 :(得分:0)

请尝试此演示代码: -

<?php    
    $arr=array();
    $arr['ligneFac'] = array("designation"=>"", "unite"=>"pièces", "quantite"=>"","taxe"=>"0.00");
     foreach($arr['ligneFac'] as $key =>$value)
     {
     if(!empty($value) && $value != "0.00"){
     $arr2['ligneFac'][$key]=$value;
    }
  }
  echo "<pre>";
  print_r($arr2);
  echo "</pre>";
 ?>

答案 1 :(得分:0)

从您发布的print_r()的输出中,我认为您的值都是字符串。 在这种情况下,以下应该可以做到这一点:

$ligneFac = array_filter($_POST['ligneFac'], function($champs) {
    $valeur = reset($champs);
    return ( ('' !== $valeur) && ('0.00' !== $valeur) );
});

(需要PHP&gt; = 5.3.0才能使用闭包。)

<强>更新

foreach ($_POST['ligneFac'] as $i => $champs) {
    list($cle, $valeur) = each($champs);
    if ( ('taxe' !== $cle) && ( ('' === $valeur) || ('0.00' === $valeur) ) ) {
        unset($_POST['ligneFac'][$i]);
    }
}

答案 2 :(得分:0)

试试这个,这将删除没有值的条目

$data = array(array('key1' => 10),array('key2' => 20,),array('key3' => ''));
$result = array_filter(array_map('array_filter', $data));

答案 3 :(得分:0)

谢谢大家的帮助,此问题现已解决。

首先,我的表单命名错误:

    <input name="ligneFac[][designation]" type="text" class="facBig" />

我改变了:

    <input name="ligneFac[1][designation]" type="text" class="facBig" />

这给我一个数组ligneFac第1行,指定为值等......

接下来我不得不更改代码以添加一些带有代码的新行:

var nbr_tr = $('#fac_table tr').length;
var nbr_rows = nbr_tr -1; //to remove the header from calc.
newNum = nbr_rows + 1;

$("#matable tbody").append('<tr>'+
                        '<td width="10px">'+
                            '<img id="insArticle" src="../../images/icon_add.png" width="16" height="16">'+
                        '</td>'+
                        '<td width="250px">'+
                            '<input name="ligneFac['+newNum+'][designation]" type="text" class="facBig" />'+
                        '</td>'+
                       ' <td width="30">'+
                            '<input name="ligneFac['+newNum+'][unite]" type="text" class="facSmall" />'+
                        '</td>'+
                       ' <td width="30">'+
                            '<input name="ligneFac['+newNum+'][quantite]" type="text" class="facSmall" />'+
                        '</td>'+
                        '<td width="30">'+
                            '<input name="ligneFac['+newNum+'][prixUnite]" type="text" class="facSmall" value="" />'+
                        '</td>'+
                        '<td width="30">'+
                            '<input name="ligneFac['+newNum+'][taxe]" type="text" class="facSmall" value="" />'+
                        '</td>'+
                       ' <td width="30">'+
                            '<input name="ligneFac['+newNum+'][totLine]" type="text" class="facSmall" value="" />'+
                        '</td>'+
                   '</tr>');

iconeClicable();
});

现在结果数组是代码友好的......

    Array
(
[mbrDatas] => Array
(
    [1] => Array
        (
            [mbr_id] => 26
            [nom] => Gautier Albert
            [adresse] => Avenue du Devin du Village 51
            [ville] => 1406 Cronay
        )

)

[facdatas] => Array
(
    [1] => Array
        (
            [concerne] => TEST
            [totalFac] => 147.00
        )

)

[ligneFac] => Array
(
    [1] => Array
        (
            [designation] => Veste
            [unite] => pièces
            [quantite] => 1
            [prixUnite] => 98
            [taxe] => 0.00
            [totLine] => 98.00
        )

    [2] => Array
        (
            [designation] => Veste de training
            [unite] => pièces
            [quantite] => 1
            [prixUnite] => 49
            [taxe] => 0.00
            [totLine] => 49.00
        )

)

)

并且没有空行使用&#34; Francis Eytan Dortort&#34;的更新代码。

    foreach ($_POST['ligneFac'] as $i => $champs)
    {
    list($cle, $valeur) = each($champs);
    if ( ('taxe' !== $cle) && ( ('' === $valeur) || ('0.00' === $valeur)         ) )
    {
    unset($_POST['ligneFac'][$i]);
    }
    }

希望这对任何人都有帮助......