【问题标题】:Removing Data from Variable if Input Fields incomplete - PHP如果输入字段不完整,则从变量中删除数据 - PHP
【发布时间】:2012-03-27 12:19:21
【问题描述】:

我的 HTML 表单中有 2 组 4 个输入字段。我预计最终会有超过 20 套,但在这个例子中只提供了 2 套。

是否有可能如果“一组”字段不完整,我可以从结果 $all 变量中删除该行。所以不要有一行数据'| | | |'它只是完全空白。

它只会帮助清理东西。

这很容易实现吗?

这是我当前用来测试的 stuff.php 脚本。

<html>

<head>

<title>Test PHP</title>

</head>

<body>

<?php

if (isset($_POST['sendform'])) {


    $ierrors = array();
    $all = '';

    // Loop over the values 1 through 2
    foreach( range( 1, 2) as $i)
    {
        // Create an array that stores all of the values for the current number
        $values = array( 
            'p' . $i . 'height' => $_POST['p' . $i . 'height'], 
            'p' . $i . 'width' => $_POST['p' . $i . 'width'], 
            'p' . $i . 'length' => $_POST['p' . $i . 'length'], 
            'p' . $i . 'weight' => $_POST['p' . $i . 'weight']
        );

        // Validate every value
        foreach( $values as $key => $value)
        {
            if( empty( $value))
            {
                $ierrors[] = "Value $key is not set";
            }
            // You can add more validation in here, such as:
            if( !is_numeric( $value))
            {
                $ierrors[] = "Value $key contains an invalid value '$value'";
            }
        }

        // Join all of the values together to produce the desired output
        $all .= implode( '|', $values) . "\n\n";
    }   

    var_dump($all);

}

?>

<form action="stuff.php" method="post">

    <div id="npup0" class="hidden">
        <div class="parcel-group"> 
            <div class="parcel-title"> 
              <label for="p1weight">Parcel 1</label> 
            </div> 
            <div class="minis weight"> 
              <input type="text" id="p1weight" name="p1weight" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p1weight']))) { echo htmlspecialchars($_POST['p1weight']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" /> 
            </div> 
            <div class="minis length">  
                <input type="text" id="p1length" name="p1length" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p1length']))) { echo htmlspecialchars($_POST['p1length']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" /> 
            </div> 
            <div class="minis width"> 
                <input type="text" id="p1width" name="p1width" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p1width']))) { echo htmlspecialchars($_POST['p1width']); } ?>"  onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" /> 
            </div> 
            <div class="minis height">  
                <input type="text" id="p1height" name="p1height" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p1height']))) { echo htmlspecialchars($_POST['p1height']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" /> 
            </div> 
        </div>
    </div>

    <div id="npup1" class="hidden">
        <div class="parcel-group"> 
            <div class="parcel-title"> 
              <label for="p1weight">Parcel 2</label> 
            </div> 
            <div class="minis weight"> 
              <input type="text" id="p2weight" name="p2weight" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p2weight']))) { echo htmlspecialchars($_POST['p2weight']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" /> 
            </div> 
            <div class="minis length"> 
            <input type="text" id="p2length" name="p2length" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p2length']))) { echo htmlspecialchars($_POST['p2length']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" /> 
            </div> 
            <div class="minis width"> 
            <input type="text" id="p2width" name="p2width" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p2width']))) { echo htmlspecialchars($_POST['p2width']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" /> 
            </div> 
            <div class="minis height"> 
            <input type="text" id="p2height" name="p2height" value="<?php if ((isset($_POST['sendform'])) && (!empty($_POST['p2height']))) { echo htmlspecialchars($_POST['p2height']); } ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" /> 
            </div> 
        </div>
    </div>

    <p><input type="submit" value="click me" id="sendform" name="sendform" /></p>

</form>

</body>

</html>

非常感谢您的任何指点。已经为此苦苦挣扎了一段时间。

【问题讨论】:

  • 为什么使用范围?相反,您有两个分开的名字足以提交
  • 嗨,山姆。我正在使用 Ranges,因为我预计最终会有超过 20 组这 4 个输入字段。

标签: php arrays forms input foreach


【解决方案1】:

是的,只要$values 不为空,请提供一个仅连接$values 和$all 的条件。

// Assume all values are empty.
$allEmpty = true;

// Validate every value
foreach( $values as $key => $value)
{
    if( empty($value))
        $ierrors[] = "Value $key is not set";
    else
        $allEmpty = false;


    // You can add more validation in here, such as:
    if( !is_numeric( $value) ) 
        $ierrors[] = "Value $key contains an invalid value '$value'";
}

// Join all of the values together to produce the desired output
if (!$allEmpty)
    $all .= implode( '|', $values) . "\n\n";

【讨论】:

  • 感谢您的回答。不幸的是,它仍然输出空行。我填写了 Parcel 1 字段并将 Parcel 2 字段留空,但它仍然输出:string '4|3|2|1 ||| ' (length=14)
  • @mcgarriers 我注意到我对空值数组使用 empty 时的疏忽。考虑我的更新。
  • 感谢您的编辑。我仍然相信我们还不是 100% 正确的。现在,当我完成 Parcel 1 字段并将 Parcel 2 字段留空但仍输出:string '||| ' (length=5)
  • 对不起,我的错。它工作得很好:D 非常感谢乔希。你可能刚刚度过了我的周末 :-) 祝你有个伟大的父亲。
  • @mcgarriers 没问题。你也是!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
相关资源
最近更新 更多