通过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;
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.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!";}
}
您可以通过以下方式帮助我。 每当检测到空白或非空白文本框时, 我需要的是以下内容; p>
在一系列文本框中(在数组中)甚至 只检测到一个文本空白只有echo 如果仅 strong>所有文本框都是非空白,则仅回显 您如何建议我更改以下内容? 谢谢。 p>
编辑 strong> p>
然后在验证码中我有以下内容; p>
Empty! code>或
Full! code>就会打印出来。 p>
空! code> ONLY ONCE strong>并非每次都有空白文本框。 p> li>
Full! code>
ONCE! strong> p> li> \ n ol>
if(isset($ _ POST ['Save']))
{
if if(is_array($ _ POST ['name']))
{
foreach($ _ POST ['name'] as $ Value)
{if($ Value =='')
{
echo“&lt; table border ='1'&gt;&lt; tr&gt;&lt; ; td&gt;响应&lt; / td&gt;&lt; / tr&gt;&lt; / table&gt;“;
echo”空!“;
}
其他
{
echo”&lt; table border ='1'&gt; ;&lt; tr&gt;&lt; td&gt;响应&lt; / td&gt;&lt; / tr&gt;&lt; / table&gt;“;
echo”完整“;
}
}
}
}
code > pre>
echo“&lt; td&gt;&lt; input style ='width:60px' type ='text'name ='name []'id ='vtext'class ='sc_two'size ='80'maxlength ='5'&gt;&lt; / td&gt;
code> pre> \ n
if(isset($ _ POST ['Save']))
{
if( $ _POST ['name'] =='')
{
echo“&lt; table border ='1'&gt;&lt; tr&gt;&lt; td&gt;响应&lt; / td&gt;&lt; / tr&gt;&lt; / 表&gt;“;
echo“Empty”;}
else
{
echo“&lt; table border ='1'&gt;&lt; tr&gt;&lt; td&gt; Responses&lt; / td&gt;&lt; / tr&gt;&lt; / table&gt;” ;
echo“已保存!”;}
}
code> pre>
div>
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
}
}
if(isset($_POST['Save']))
{
if(is_array($_POST['name']))
{
$full = 0;
foreach($_POST['name'] as $Value){
if($Value == '')
{
$full = ++;
}
}
}
echo "<table border='1'><tr><td>Response</td></tr></table>";
echo $full == 0 ? "Full" : "Empty" //Shorthand if notation
}
You have to run your checks then output the response outside the loop.
You don't need a loop, use array_search
to tell if there's an empty element in the array.
if (array_search('', $_POST['name'])) {
$result = 'Empty!';
} else {
$result = 'Full!';
}
echo '<table border="1"><tr><td>Response</td></tr></table>';
echo $result;