的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] =>**))
,如何檢查所有數組是否有值。在此先感謝
在所有文本框中添加'required'屬性 – Tushar