2014-01-30 45 views
1

在我的代碼中,當我們點擊確定按鈕。這將回聲3如何計算有價值的投入?

我想申請來算只有那些價值只有

怎麼做輸入?

<?php 
    if(isset($_POST["submit"])) 
    { 
    echo count($_POST["to_more"]); 
    } 
?> 
<form name="f1" method="post"> 
<input type="text" name="to_more[]"> 
<input type="text" name="to_more[]"> 
<input type="text" name="to_more[]"> 
<input type="submit" name="submit" value="OK"> 
</form> 
+1

循環比只有你會得到計數。 –

+0

循環$ _POST ['to_more'],如果它包含值,則計數它們 –

回答

2

試試這個

<?php 
    if(isset($_POST["submit"])) 
    { 
    echo count(array_filter($_POST["to_more"])); 
    } 
?> 
+0

不能爲空。 – user3215821

1

如何

if(isset($_POST["submit"])){ 
    $count = 0 ; 
    foreach($_POST["to_more"] as $data){ 
       if($data != '') $count++; 

    } 
     if($count > 0) 
       echo $count; 
    } 
0

array_filter應該幫助

<?php 
    $ar = isset($_POST["to_more"]) ? $_POST["to_more"] : array(); 
    $ar = array_filter($ar, function($el){ return !empty($el);}); 
    echo count($ar); 
?> 

應該比的foreach快,順便說一句。

UPD:哦,看來,NLSaini發佈了精確的解決方案,檢查它。