在PHP中验证动态文本框数组值

时间:2013-06-28 08:31:31

标签: php

问题在于,当显示第二个表单时,验证不会按照我需要的方式进行。无论我在TextBoxEntry中是否有值,我都会收到消息Saved。理想情况下,如果文本框不为空,那么只有我应该收到消息Saved,但即使我没有任何内容,我也会收到消息Saved

有人可以帮忙吗?

由于

以下是代码:

<?php 
SESSION_start();
include ("../connection/index.php"); 
?>

<!DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
        <form name="form1" method="post" action="">

        <?php
        $SQLbrands="SELECT * FROM brands";
        $runBrands=mysqli_query($db, $SQLbrands) or die ("SQL Error");
        $noRow=mysqli_num_rows($runBrands);
        $brndTable = "<table border='1' cellspacing='0' cellpadding='1' id='brndTable1' class='brndTable1'>";
        $brndTable .= "<thead><tr><th class='brT11'>Brand Name</th><th class='brT21'>Variant</th><th class='brT31'>SKU</th><th class='brT41'></th></tr></thead>";
        $brndTable .= "<tbody>";
        while ($reK = mysqli_fetch_array($runBrands))
        {
            $wec = $reK['id']; $wec2 = $reK['bvariant']; $wec3 = $reK['bsku'];
            $brndTable .= "<tbody class='colormine'><tr>";
            $brndTable .= "<td class='brT1'>".$reK["name"]."</td>";
            $brndTable .= "<td class='brT2'>".$reK["address"]."</td>";
            $brndTable .= "<td class='brT3'>".$reK["tphone"]."</td>";
            $brndTable .= "<td class='brT4'><input type='checkbox' name='checkedMe[]' value='$wec' /></td>";
            $brndTable .= "</tr>";
        }
        $brndTable .= "</tbody>";
        $brndTable .= "</table>";


        echo $brndTable;
        ?>  

        <input type="submit" name="sendone" id="sendone" value="OneClick">
        </form>
</div>

<div id="two">
        <?php

            if(isset($_POST['sendone']))
            {   if(!isset($_POST['checkedMe']))
                {echo 'No check boxes are selected!'; return;} else 
                {
                $mohan = "<form method='post' action=''><table border='1' id='myTad'  cellspacing='0' cellpadding='2'>
                    <tr class='tabColor'>
                    <td>Name</td>
                    <td>Address</td>
                    <td>Phone</td>
                    </tr>";
                foreach($_POST['checkedMe'] as $checkedMe)
                            {
                                $checkedMe=mysqli_real_escape_string($db,$checkedMe);
                                $QR = "SELECT bname, bvariant, bsku FROM brands WHERE id='$checkedMe'";
                                $rr = mysqli_query($db,$QR) or die ("SQL Error");
                                $roV = mysqli_num_rows($rr);
                                $rr = $rr->fetch_assoc();

                                $mohan .= "<tr class='sc_eght' ><td class='sc_five'>".$rr['bname']." ".$rr['bvariant']." ".$rr['bsku'];
                                $mohan .="</td>
                                   <td><textarea  style='width:80px;' name='schema[]' id='vtext' class='sc_one' rows='1' cols='1' maxlength='100'></textarea></td>
                                   <td><input  style='width:80px;' type='text' name='TextBoxEntry[]' id='vtext' class='sc_two' size='80' maxlength='5'></td>
                                   </tr>";

                            }
                                $mohan .= "<input type='submit' name='sendMe' id='sendMe' value='Save'></table></form>";
                                $_SESSION['cb'] = $mohan; }

                                if(isset($_SESSION['cb'])) 
                                echo $_SESSION['cb']; 
            }   


        if(isset($_POST['sendMe']))
        {
            if(!(isset($_POST['TextBoxEntry']) || $_POST['TextBoxEntry'] == ''))
            {echo $_SESSION['cb']; echo "Empty!"; return;} else  { echo $_SESSION['cb']; echo "Saved!"; return;}
        }

            ?>
</div>
</body>
</html> 

4 个答案:

答案 0 :(得分:0)

更改if(!(isset($_POST['TextBoxEntry']) || $_POST['TextBoxEntry'] == ''))

if( !isset($_POST['TextBoxEntry']) || $_POST['TextBoxEntry'] == '')

答案 1 :(得分:0)

尝试

    if(empty($_POST['TextBoxEntry']))
    {
      echo $_SESSION['cb']; 
      echo "Empty!";
    } else  { 
      echo $_SESSION['cb']; 
      echo "Saved!";
    }

我刚刚意识到'TextBoxEntry'是一个数组。 希望它有所帮助。

干杯!!

答案 2 :(得分:0)

if(!(isset($_POST['TextBoxEntry']) || $_POST['TextBoxEntry'] == ''))等于if(!isset($_POST['TextBoxEntry'] && $_POST['TextBoxEntry'] != ''),但您需要将其更改为

if(!isset($_POST['TextBoxEntry'] || $_POST['TextBoxEntry'] == '')

另一件事是我认为您的代码中不需要return语句。

答案 3 :(得分:0)

而不是这行代码

if(!(isset($_POST['TextBoxEntry']) || $_POST['TextBoxEntry'] == ''))

你应该有这个

if( (!(isset($_POST['TextBoxEntry'])) || $_POST['TextBoxEntry'] == ''))

因为当你拥有代码时,程序会取消所有条件。所以你需要另一个()来保持第一部分的否定。

试试这件事。希望它有效!