dougaopu7938 2013-07-23 17:42
浏览 14
已采纳

通过PHP中的foreach进行验证

Hi can some one help me out with the following. This prints every time Empty! or Full! whenever there is a blank or a non blank text box is detected.

What I need is the following;

  1. Out of the series of text boxes (in the array) even only one text is detected blank just echo Empty! ONLY ONCE not every time there a blank textbox.

  2. If ONLY all the text boxes are non-blank, then echo Full! only ONCE!

How do you suggest that I change the following? Thank you.

if(isset($_POST['Save']))
{
    if(is_array($_POST['name']))
    {
    foreach($_POST['name'] as $Value)
        {if($Value == '')
            {
            echo "<table border='1'><tr><td>Response</td></tr></table>";
            echo "Empty!";
            } 
            else 
            {
            echo "<table border='1'><tr><td>Response</td></tr></table>";
            echo "Full";
            }
        }
    }
}

EDIT

echo "<td><input style='width:60px' type='text' name='name[]' id='vtext' class='sc_two'     size='80' maxlength='5'></td>

then in a validation code I have the following;

if(isset($_POST['Save']))
{
    if($_POST['name']=='')
    {
    echo "<table border='1'><tr><td>Responses</td></tr></table>";
    echo "Empty";} 
    else 
    {
    echo "<table border='1'><tr><td>Responses</td></tr></table>";
    echo "Saved!";}
}
  • 写回答

3条回答 默认 最新

  • doujiepin5547 2013-07-23 17:48
    关注
    if(isset($_POST['Save'])) {
        if(is_array($_POST['name'])) {
    
            $result = 'Full!'; // Result defaults to 'Full!'
    
            // But if we find an empty value we change it to 'Empty!'
            foreach($_POST['name'] as $value){
                if($value === ''){
                    $result = 'Empty!';
                    break;
                }
            }
    
            // Output the response
    ?>
            <table border="1"><tr><td>Response</td></tr></table>
            <?=$result?>
    <?php
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记