2015-10-24 60 views
1

的index.html檢查所有數組有值,而不是空

<form id="players_form" > 
    <input type=" text" name="main_name[]" value="1"> 
    <input type=" text" name="main_name[]" value="2"> 
    <input type=" text" name="main_name[]" value="3"> 
    <input type=" text" name="main_name[]" value="4"> 
    <input type=" text" name="main_name[]" value="5"> 

    <input type=" text" name="sub_name[]" value="6"> 
    <input type=" text" name="sub_name[]" value="7"> 
    <input type=" text" name="sub_name[]" value="8"> 
    <input type=" text" name="sub_name[]" value="9"> 
    <input type=" text" name="sub_name[]" value=""> 
    <input type="button" value="Submit" id="submit"> 

</form> 

腳本

$('#submit').click(function() { 
var data = JSON.stringify($("#players_form").serializeArray()); 
// alert(data); 
$.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod 
type: 'POST', 
data: { 
    list: data 
}, 
url: 'process.php', 
success: function(responseText) { 
    if (responseText == 1) { 
    alert("Sucess"); 
    } 

process.php

$decoded = json_decode($_REQUEST['list'], true); 
print_r(array_filter($decoded)); 

結果

Array([0] => Array ([name] => main_name[] [value] => 1) 
[1] => Array ([name] => main_name[] [value] => 2) 
[2] => Array ([name] => main_name[] [value] => 3) 
[3] => Array ([name] => main_name[] [value] => 4) 
[4] => Array ([name] => main_name[] [value] => 5) 
[5] => Array ([name] => sub_name[] [value] => 6) 
[6] => Array ([name] => sub_name[] [value] => 7) 
[7] => Array ([name] => sub_name[] [value] => 8) 
[8] => Array ([name] => sub_name[] [value] => 9) 
[9] => Array ([name] => sub_name[] [value] =>)) 

預計代碼

if(all the arrays has value) 
{ 
// True 
} 
else 
{ 
Any one of the values in array is empty 
} 

喜的朋友在上面的代碼,我發送的形式通過數值來AJAX function process.php,在process.PHP我應該檢查wheather的所有文本框應包含值,那麼就應該進行下一步否則,它應該回聲對不起,你有空值。在我的代碼中,我的最後一個框的值爲空<input type=" text" name="sub_name[]" value="">結果也是returns [9] => Array ([name] => sub_name[] **[value] =>**)),如何檢查所有數組是否有值。在此先感謝

+0

在所有文本框中添加'required'屬性 – Tushar

回答

0

首先,你需要把它發送到服務器之前驗證數據。例如:

var dataValid = true; 
$("#players_form input").each(function(){ 
    if ($(this).val() == ''){ 
     dataValid = false; 
    } 
}); 

if (dataValid){ 
    // send data 
} else { 
    alert('Fill in all the data!'); 
} 

然後在服務器,同樣的事情:

$valid = true; 
foreach($decoded as $input){ 
    if ($input['value'] == ''){ 
     $valid = false; 
    } 
} 

if ($valid){ 
    // done good 
} else { 
    echo "Fill in all the data!"; 
} 
0

的最佳方式使用empty()功能

if(!empty($decoded)) 
{ 
// True 
} 
else 
{ 
Any one of the values in array is empty 
} 
+1

我認爲最好的方法是不發送ajax,如果有空輸入,那麼js部分的驗證也是如此。正如Tushar評論的那樣 – guradio

0

這是張貼數據之前,最好檢查,[].every可以做到這一點。 every允許您檢查數組的所有元素是否通過了某個測試。

var ok = data.every(function(element) { 
    return element.value; // if value is empty, it's false 
}); 

if (ok) { 
    // POST request 
} else { 

} 
0

在PHP5(> = 5.5.0),您可以通過多維數組

$user=Array 
(
(0) => Array 
    (
     (name) => 'Susan', 
     (age) => 21 
    ), 

(1) => Array 
    (
     (name) => 'Mark', 
     (age) => 33 
    ), 

(2) => Array 
    (
     (name) => 'Michael', 
     (age) => 25 
    )); 
$key = array_search(21, array_column($user, 'age')); 

您可以搜索數組中''值做搜索。

相關問題