php遍历输出数组的措施,多选值获取

By admin in 编程 on 2019年8月6日

php教程遍历输出数组的方法

php教程中checkbox值获取,显示,多选值获取

由于checkbox特殊性所以我们要获取它的值,在很我语言中都不一样的,在php教程
以我们是以数组形式来处理,并且利用遍历他的值,下面有大量实例。

<html>
<head>
<title>Foreach Demo</title>
</head>
<body>
<?

最简单checkbox获取值代码

<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h1>checkbox demo</h1>

<h3>demonstrates checkboxes</h3>
<form action =”handleformcheckbox.php”>

<ul>
  <li><input type =”checkbox” name =”chkfries” value
=”11.00″>fries</li>
  <li><input type =”checkbox” name =”chksoda”  value
=”12.85″>soda</li>
  <li><input type =”checkbox” name =”chkshake” value
=”1.30″>shake</li>
  <li><input type =”checkbox” name =”chkketchup” value
=”.05″>ketchup</li>
</ul>
<input type =”submit”>
</form>

</body>
</html>

<!– handleformcheckbox.php
<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h3>demonstrates reading checkboxes</h3>
<?
print <<<here
chkfries: $chkfries <br>
chksoda: $chksoda <br>
chkshake: $chkshake <br>
chkketchup: $chkketchup <br>
<hr>

here;

$total = 0;

if (!empty($chkfries)){
  print (“you chose fries <br>”);
  $total = $total + $chkfries;
}

if (!empty($chksoda)){
  print (“you chose soda <br>”);
  $total = $total + $chksoda;
}

if (!empty($chkshake)){
  print (“you chose shake <br>”);
  $total = $total + $chkshake;
}

if (!empty($chkketchup)){
  print (“you chose ketchup <br>”);
  $total = $total + $chkketchup;
}

print “the total cost is $$total”;

?>
</body>
</html>

$list = array(“A”, “B”, “C”, “D”, “E”);

<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h1>checkbox demo</h1>

–>

print “<ul>n”;
foreach ($list as $value){
  print ” <li>$value</li>n”;
} // end foreach
print “</ul>n”;

<h3>demonstrates checkboxes</h3>
<form action =”handleformcheckbox.php”>

实例

?>
</body>
</html>

<ul>
  <li><input type =”checkbox” name =”chkfries” value
=”11.00″>fries</li>
  <li><input type =”checkbox” name =”chksoda”  value
=”12.85″>soda</li>
  <li><input type =”checkbox” name =”chkshake” value
=”1.30″>shake</li>
  <li><input type =”checkbox” name =”chkketchup” value
=”.05″>ketchup</li>
</ul>
<input type =”submit”>
</form>

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get[food];
$self = htmlentities($_server[‘php_self’]);
if (!empty($food)) {
    echo “the foods selected
are:<br />”;
    foreach($food as $foodstuf)
    {
        echo
“<strong>”.htmlentities($foodstuf).”</strong><br
/>”;
    }
}
else
{
    echo (“<form action=”$self” “);
    echo (‘method=”get”>
    <fieldset>
        <label>italian <input type=”checkbox” name=”food[]”
value=”italian” />
</label>
        <label>mexican <input type=”checkbox” name=”food[]”
value=”mexican” />
</label>
        <label>chinese <input type=”checkbox” name=”food[]”
value=”chinese”
        checked=”checked” /></label>
    </fieldset>
    <input type=”submit” value=”go!” >’);
}
?>
</body>
</html>

实例二

</body>
</html>

多选checkbox

<?
$letters[0] = ‘A’;
$letters[1] = ‘B’;
$letters[3] = ‘D’;
$letters[2] = ‘C’;

<!– handleformcheckbox.php
<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h3>demonstrates reading checkboxes</h3>
<?
print <<<here
chkfries: $chkfries <br>
chksoda: $chksoda <br>
chkshake: $chkshake <br>
chkketchup: $chkketchup <br>
<hr>

<?php
$options = array(‘option 1’, ‘option 2’, ‘option 3’);

$valid = true;
if (is_array($_get[‘input’])) {
    $valid = true;
    foreach($_get[‘input’] as $input) {
        if (!in_array($input, $options)) {
            $valid = false;
        }
    }
    if ($valid) {
        //process input
    }
}
?>

foreach ($letters as $letter) {
    print $letter;
}
?>
 

here;

实例checkbox多值获取

$total = 0;

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get[“food”];//www.3ppt.com
if (!empty($food)){
    echo “the foods selected are: <strong>”;
    foreach($food as $foodstuff){
        echo ‘<br />’.htmlentities($foodstuff);
    }
    echo “</strong>.”;
}
else {
    echo (‘
    <form action=”‘. htmlentities($_server[“php_self”]).'”
method=”get”>
        <fieldset>
            <label>
                italian
                <input type=”checkbox” name=”food[]”
value=”italian” />
            </label>
            <label>
                mexican
                <input type=”checkbox” name=”food[]”
value=”mexican” />
            </label>
            <label>
                chinese
                <input type=”checkbox” name=”food[]”
value=”chinese” checked=”checked” />
            </label>
        </fieldset>
        <input type=”submit” value=”go!” />
    </form> ‘);
    }
?>
</body>
</html>

if (!empty($chkfries)){
  print (“you chose fries <br>”);
  $total = $total + $chkfries;
}

if (!empty($chksoda)){
  print (“you chose soda <br>”);
  $total = $total + $chksoda;
}

if (!empty($chkshake)){
  print (“you chose shake <br>”);
  $total = $total + $chkshake;
}

if (!empty($chkketchup)){
  print (“you chose ketchup <br>”);
  $total = $total + $chkketchup;
}

print “the total cost is $$total”;

?>
</body>
</html>
–>

实例

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get[food];
$self = htmlentities($_server[‘php_self’]);
if (!empty($food)) {
    echo “the foods selected
are:<br />”;
    foreach($food as $foodstuf)
    {
        echo
“<strong>”.htmlentities($foodstuf).”</strong><br
/>”;
    }
}
else
{
    echo (“<form action=”$self” “);
    echo (‘method=”get”>
    <fieldset>
        <label>italian <input type=”checkbox” name=”food[]”
value=”italian” />
</label>
        <label>mexican <input type=”checkbox” name=”food[]”
value=”mexican” />
</label>
        <label>chinese <input type=”checkbox” name=”food[]”
value=”chinese”
        checked=”checked” /></label>
    </fieldset>
    <input type=”submit” value=”go!” >’);
}
?>
</body>
</html>

多选checkbox

<?php
$options = array(‘option 1’, ‘option 2’, ‘option 3’);

$valid = true;
if (is_array($_get[‘input’])) {
    $valid = true;
    foreach($_get[‘input’] as $input) {
        if (!in_array($input, $options)) {
            $valid = false;
        }
    }
    if ($valid) {
        //process input
    }
}
?>

实例checkbox多值获取

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get[“food”];
if (!empty($food)){
    echo “the foods selected are: <strong>”;
    foreach($food as $foodstuff){
        echo ‘<br />’.htmlentities($foodstuff);
    }
    echo “</strong>.”;
}
else {
    echo (‘
    <form action=”‘. htmlentities($_server[“php_self”]).'”
method=”get”>
        <fieldset>
            <label>
                italian
                <input type=”checkbox” name=”food[]”
value=”italian” />
            </label>
            <label>
                mexican
                <input type=”checkbox” name=”food[]”
value=”mexican” />
            </label>
            <label>
                chinese
                <input type=”checkbox” name=”food[]”
value=”chinese” checked=”checked” />
            </label>
        </fieldset>
        <input type=”submit” value=”go!” />
    </form> ‘);
    }
?>
</body>
</html>

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 澳门新葡亰官网app 版权所有