检查所有数组是否有值而不是空

时间:2015-10-24 06:16:00

标签: javascript php jquery html arrays

的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 
 }

以上代码中的好友我将表单值发送到process.php到AJAX function,在process.PHP我应该检查所有文本框应该包含值然后它应该继续到下一步,它应该回应抱歉你有空值。在我的代码中,我的最后一个框值为空<input type=" text" name="sub_name[]" value="">,结果也是returns [9] => Array ( [name] => sub_name[] **[value] =>** )),如何检查所有数组是否有值。提前致谢

4 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

首先,您需要在将数据发送到服务器之前验证数据。例如:

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

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

然后在服务器上,同样的事情:

    $("#continueToCanvas").click(function(){
    window.close();
    window.open("./canvas.html","new ","width=1000,height=600");

});
function canvasImage(){
    /*var x=document.getElementById('canvas');
    canvas=x.getContext('2d');
    var pic = new Image();
    pic.src=localStorage.getItem('imgData');
    pic.addEventListener("load",function(){canvas.drawImage(pic,0,0,x.width,x.height)},false);
}
window.addEventListener("load",canvasImage,false);*/

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var canvasOffset = $("#canvas").offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;

var startX;
var startY;
var isDown = false;


var pi2 = Math.PI * 2;
var resizerRadius = 8;
var rr = resizerRadius * resizerRadius;
var draggingResizer = {
    x: 0,
    y: 0
};
var imageX = 50;
var imageY = 50;
var imageWidth, imageHeight, imageRight, imageBottom;
var draggingImage = false;
var startX;
var startY;



var img = new Image();
img.onload = function () {
    imageWidth = img.width;
    imageHeight = img.height;
    imageRight = imageX + imageWidth;
    imageBottom = imageY + imageHeight
    draw(true, false);
}
img.src = localStorage.getItem('imgData');


function draw(withAnchors, withBorders) {

    // clear the canvas
   var abc=document.getElementById("canvasTextInput").value;
   var f= document.getElementById("fnt").value + "px";
   ctx.font=f + " Georgia";

    ctx.clearRect(0, 0, canvas.width, canvas.height);

   ctx.fillText(abc,200,200);  
 // draw the image
    ctx.drawImage(img, 0, 0, img.width, img.height, imageX, imageY, imageWidth, imageHeight);

    // optionally draw the draggable anchors
    if (withAnchors) {
        drawDragAnchor(imageX, imageY);
        drawDragAnchor(imageRight, imageY);
        drawDragAnchor(imageRight, imageBottom);
        drawDragAnchor(imageX, imageBottom);
    }

    // optionally draw the connecting anchor lines
    if (withBorders) {
        ctx.beginPath();
        ctx.moveTo(imageX, imageY);
        ctx.lineTo(imageRight, imageY);
        ctx.lineTo(imageRight, imageBottom);
        ctx.lineTo(imageX, imageBottom);
        ctx.closePath();
        ctx.stroke();
    }

}

function drawDragAnchor(x, y) {
    ctx.beginPath();
    ctx.arc(x, y, resizerRadius, 0, pi2, false);
    ctx.closePath();
    ctx.fill();
}

function anchorHitTest(x, y) {

    var dx, dy;

    // top-left
    dx = x - imageX;
    dy = y - imageY;
    if (dx * dx + dy * dy <= rr) {
        return (0);
    }
    // top-right
    dx = x - imageRight;
    dy = y - imageY;
    if (dx * dx + dy * dy <= rr) {
        return (1);
    }
    // bottom-right
    dx = x - imageRight;
    dy = y - imageBottom;
    if (dx * dx + dy * dy <= rr) {
        return (2);
    }
    // bottom-left
    dx = x - imageX;
    dy = y - imageBottom;
    if (dx * dx + dy * dy <= rr) {
        return (3);
    }
    return (-1);

}


function hitImage(x, y) {
    return (x > imageX && x < imageX + imageWidth && y > imageY && y < imageY + imageHeight);
}


function handleMouseDown(e) {
    startX = parseInt(e.clientX - offsetX);
    startY = parseInt(e.clientY - offsetY);
    draggingResizer = anchorHitTest(startX, startY);
    draggingImage = draggingResizer < 0 && hitImage(startX, startY);
}

function handleMouseUp(e) {
    draggingResizer = -1;
    draggingImage = false;
    draw(true, false);
}

function handleMouseOut(e) {
    handleMouseUp(e);
}

function handleMouseMove(e) {

    if (draggingResizer > -1) {

        mouseX = parseInt(e.clientX - offsetX);
        mouseY = parseInt(e.clientY - offsetY);

        // resize the image
        switch (draggingResizer) {
            case 0:
                //top-left
                imageX = mouseX;
                imageWidth = imageRight - mouseX;
                imageY = mouseY;
                imageHeight = imageBottom - mouseY;
                break;
            case 1:
                //top-right
                imageY = mouseY;
                imageWidth = mouseX - imageX;
                imageHeight = imageBottom - mouseY;
                break;
            case 2:
                //bottom-right
                imageWidth = mouseX - imageX;
                imageHeight = mouseY - imageY;
                break;
            case 3:
                //bottom-left
                imageX = mouseX;
                imageWidth = imageRight - mouseX;
                imageHeight = mouseY - imageY;
                break;
        }

        if(imageWidth<25){imageWidth=25;}
        if(imageHeight<25){imageHeight=25;}

        // set the image right and bottom
        imageRight = imageX + imageWidth;
        imageBottom = imageY + imageHeight;

        // redraw the image with resizing anchors
        draw(true, true);

    } else if (draggingImage) {

        imageClick = false;

        mouseX = parseInt(e.clientX - offsetX);
        mouseY = parseInt(e.clientY - offsetY);

        // move the image by the amount of the latest drag
        var dx = mouseX - startX;
        var dy = mouseY - startY;
        imageX += dx;
        imageY += dy;
        imageRight += dx;
        imageBottom += dy;
        // reset the startXY for next time
        startX = mouseX;
        startY = mouseY;

        // redraw the image with border
        draw(false, true);

    }


}


$("#canvas").mousedown(function (e) {
    handleMouseDown(e);
});
$("#canvas").mousemove(function (e) {
    handleMouseMove(e);
});
$("#canvas").mouseup(function (e) {
    handleMouseUp(e);
});
$("#canvas").mouseout(function (e) {
    handleMouseOut(e);
});
}

window.addEventListener("load",canvasImage,false);

答案 2 :(得分:0)

在发布数据之前检查一下,[].every可以做到这一点。 every允许您检查数组的所有元素是否通过了某个测试。

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

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

}

答案 3 :(得分:0)

在PHP5(&gt; = 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'));

您可以在数组中搜索''值。