dp926460 2012-03-09 22:15
浏览 35
已采纳

如果输入字段不完整,则从变量中删除数据 - PHP

I have 2 sets of 4 input fields in my HTML Form. I expect to eventually have over 20 sets but have provided just 2 for this example.

Is it possible if a "set" of fields is incomplete that I can just remove that row from my resultant $all variable. So instead of having a row with the data '| | | |' it would just be completely blank.

It would just help clean things up.

Would this be easy enough to achieve?

Here is my current stuff.php script I'm using to test.

<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) . "

";
    }   

    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>

Many thanks for any pointers. Been struggling with this for a while now.

展开全部

  • 写回答

1条回答 默认 最新

  • dongwuli5105 2012-03-09 22:27
    关注

    Yes, provide a condition that only concatenates $values with $all as long as $values isn't empty.

    // 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) . "
    
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
  • ¥15 传人记程序做的plc 485从机程序该如何写
  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?
  • ¥50 libwebsockets 如何添加其他socket事件回调
  • ¥50 实现画布拖拽算子排布,通过flink实现算子编排计算,请提供思路
  • ¥15 esium自定义材质拉伸问题
  • ¥15 cmake+mingw使用<mysqlx/xdevapi.h>报错
  • ¥15 eNSP中防火墙的使用
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部