将会话变量放入html表单字段 - 如何使其干燥?

时间:2014-07-29 15:46:52

标签: php html dry

我正在使用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>

如何让这更干?

1 个答案:

答案 0 :(得分:-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');
}