【问题标题】:Placing session variables into html form fields - how do I make it DRY?将会话变量放入 html 表单字段 - 如何使其干燥?
【发布时间】:2014-07-29 15:46:51
【问题描述】:

我正在编写一个使用 html/php 来收集用户信息的页面。当用户点击提交时,它会将他们带到另一个页面,他们可以在将信息提交到系统之前确认他们的信息。如果出现问题,用户可以选择单击“返回”按钮并编辑信息。我希望他们最初输入的信息出现在所有表单字段中。

<td> <label for="firstname"> First Name </label> </td>
<td> <input type="name" id="firstname" name="firstname" <?php if ($_SESSION[firstname]<>""){echo "value='$_SESSION[firstname]'";}?> /> </td> </tr>
<tr>
<td> <label for="lastname"> Last Name </label> </td>
<td> <input type="name" id="lastname" name="lastname" <?php if ($_SESSION[lastname]<>""){echo "value='$_SESSION[lastname]'";}?> /> *required</td> </tr>
<tr>
<td> <label for="email"> Email </label></td>
<td> <input type="email" id="email" name="email"<?php if ($_SESSION[email]<>""){echo "value='$_SESSION[email]'";}?> /> *required</td> </tr>
<tr> 
<td> <label for="phone"> Phone </label> </td>
<td> <input type="tel" id="phone" name="phone" <?php if ($_SESSION[phone]<>""){echo "value='$_SESSION[phone]'";}?> /> *required</td> </tr>
<tr>
<td> <label for="address1"> Address Line 1 </label> </td>
<td> <input type="text" id="address1" name="address1" <?php if ($_SESSION[address1]<>""){echo "value='$_SESSION[address1]'";}?> /> </td> </tr>
<tr>
<td> <label for="address2"> Address Line 2 </label> </td>
<td> <input type="text" id="address2" name="address2" <?php if ($_SESSION[address2]<>""){echo "value='$_SESSION[address2]'";}?> /> </td></tr>
<tr>
<td> <label for="city"> City </label> </td>
<td> <input type="text" id="city" name="city" <?php if ($_SESSION[city]<>""){echo "value='$_SESSION[city]'";}?> /> *required</td></tr>

如何让这个更干燥?

【问题讨论】:

标签: php html dry


【解决方案1】:

我很快就做到了,这真的不是我自己做的方式,但从技术上讲,它会让它变得干燥。请记住在您拥有的任何类中实现这一点。此外,清理用户输入。在您认为合适的地方进行改进。

忽略这没有实现 MVC(你应该研究)的事实,我会做以下事情:

<?php

instanceTd($type = 'open')
{
    switch($type){
        case 'open':
            echo '<td>';
        case 'closed':
            echo '</td>';
    }
}

displayHTML($htmlType, $property)
{
    // double quotes allow variables to be parsed by PHP with ease

    $name = $property['name'];
    $value = $property['value'];
    $label = $property['value'];
    $type = $property['type'];

    switch($htmlType)
    {
        case 'label':
            echo "<label for='$name'> $label </label>";
            exit;
        case 'input':
            echo "<input type='$type' id='$name' name='$name' value='$value'/>";
            if($property['required'] == 'true')
            {
                echo '*required';
            }
            exit;
    }
}

$array = array(
    1 => array(  // index acts as order in this case.
        'name' => 'firstname',
        'label' => 'First Name',
        'type' => 'name',
        'value' => '',
        'required' => 'true'
    ),
    2 => array(
        'name' => 'lastname',
        'label' => 'Last Name',
        'type' => 'name',
        'value' => '',
        'required' => 'true'
    ),
    3 => array(
        'name' => 'email',
        'label' => 'Email',
        'type' => 'email',
        'value' => '',
        'required' => 'true'
    ),
    4 => array(
        'name' => 'phone',
        'label' => 'Phone',
        'type' => 'tel',
        'value' => '',
        'required' => 'true'
    ),
    5 => array(
        'name' => 'address1',
        'label' => 'Address Line 1',
        'type' => 'text',
        'value' => '',
        'required' => 'false'
    ),
    6 => array(
        'name' => 'address2',
        'label' => 'Address Line 2',
        'type' => 'text',
        'value' => '',
        'required' => 'false'
    ),
    7 => array(
        'name' => 'city',
        'label' => 'City',
        'type' => 'text',
        'value' => '',
        'required' => 'true'
    )
);

//For each field you want displayed do the following.
foreach($array as $index => $property)
{

    // Check if the field has been posted, then set the value to that POST value
    if(isset($_POST[$property['name']])){
        $property['value'] = $_POST[$property['name']];
    }

    instanceTd('open');
    displayHTML('label', $property);
    displayHTML('input', $property);
    instanceTd('closed');
}

【讨论】:

  • 这个代码是无效的 PHP 并且如果它本来是它将会很容易受到 xss 攻击。
猜你喜欢
  • 2011-06-22
  • 2018-08-29
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 2010-11-09
相关资源
最近更新 更多