Reputation: 663
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>
script
$('#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));
Result
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] => ))
Expected code
if(all the arrays has value)
{
// True
}
else
{
Any one of the values in array is empty
}
Hi friends in the above code I'm sending the form values to process.php through AJAX function
, in process.PHP
I should check wheather all the text box should contain values then it should proceed to next step else , it should echo sorry you have empty values . In my code I have last box value as empty <input type=" text" name="sub_name[]" value="">
in result also it returns [9] => Array ( [name] => sub_name[] **[value] =>** ))
, How to check whether all the array has value or not . Thanks in advance
Upvotes: 2
Views: 2641
Reputation: 26
In PHP5 (>=5.5.0) you can do search through a multi dimensional array
$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'));
You can search ''
value in your array.
Upvotes: 0
Reputation: 13692
It's better to check before POSTing the data, [].every
can do that. every
allows you to check if all elements of an array pass a certain test.
var ok = data.every(function(element) {
return element.value; // if value is empty, it's false
});
if (ok) {
// POST request
} else {
}
Upvotes: 1
Reputation: 328
First, you need to validate data before sending it to server. For example:
var dataValid = true;
$("#players_form input").each(function(){
if ($(this).val() == ''){
dataValid = false;
}
});
if (dataValid){
// send data
} else {
alert('Fill in all the data!');
}
Then on server, same thing:
$valid = true;
foreach($decoded as $input){
if ($input['value'] == ''){
$valid = false;
}
}
if ($valid){
// done good
} else {
echo "Fill in all the data!";
}
Upvotes: 0
Reputation: 4097
The best way use empty()
function
if(!empty($decoded))
{
// True
}
else
{
Any one of the values in array is empty
}
Upvotes: 0