Rockstar Rockstar - 12 days ago 6
PHP Question

Html form from php array

Desired OutputI want to display an html form from php array. I want (first name) input and (Last name) input to be in one row. I have used bootstrap row class but it raps both inputs in separate row like

<div class="row">first name </div>
<div class="row">Last name </div>


I would be thankful, If any one can help me solve this problem !

<?php

$form = array(

'fname' => array(
'title' => 'First Name',
'type' => 'text',
'size' => '50',
),

'lname' => array(
'title' => 'Last Name',
'type' => 'text',
'size' => '50',
),

'email' => array(
'title' => 'Email',
'type' => 'email',
'class' => 'form-control',
'size' => '50',

),
'submit' => array(
'title' => 'Register',
'type' => 'submit',
'size' => '50',
'class' => 'form-control',

),
)

?>

<form action="" method="post">
<?php

foreach ($contact_form as $name => $elements) {

$label = '<label>' . $elements['title'] . '</label><br>';

if ($elements['type'] == 'text') {

echo '<div class="row">';
echo $label . '<input type="' . $elements['type'] . '" name="' . $name . '" size="' . $elements['size'] . '" >';
echo '</div>';

} elseif ($elements['type'] == 'email') {
echo '<div class="row">';
echo $label . '<input type="' . $elements['type'] . '" name="' . $name . '" size="' . $elements['size'] . '">' . form_error($name) . '<br>';
echo '</div>';

}else{

echo '<input type="' . $elements['type'] . '" name="' . $name . '" value="' . $elements['title'] . '" size="' . $elements['size'] . '">' ;
}
}
?>
</form>

Answer

Update your PHP array to use Bootstrap sizing instead of percentages. I've also made a couple of other tweaks...

$form = [
    [
        'title' => 'First Name',
        'name' => 'first_name',
        'type' => 'text',
        'cols' => '6',
    ],
    [
        'title' => 'Last Name',
        'name' => 'last_name',
        'type' => 'text',
        'cols' => '6',
    ],
    [
        'title' => 'Email',
        'name' => 'email',
        'type' => 'email',
        'cols' => '6',
    ],
    [
        'title' => 'Register',
        'type' => 'submit',
        'cols' => '6',
    ],
]

Then you can use that data to create your form with a foreach loop,

<div class="container">
  <form class="form-horizontal">

    <?php foreach ($form as $element): ?>

      <div class="col-sm-<?= $element['cols'] ?>">

        <?php if ($element['type'] == 'submit): ?>

          <button type="submit" class="btn btn-default"><?= $element['title'] ?></button>

        <?php else: ?>

          <div class="form-group">
            <label for="<?= $element['name'] ?>" class="col-sm-4 control-label"><?= $element['title'] ?></label>
            <div class="col-sm-8">
              <input type="<?= $element['type'] ?>" class="form-control" id="<?= $element['name'] ?>" name="<?= $element['name'] ?>">
            </div>
          </div>

        <?php endif; ?>

      </div>

    <?php endforeach; ?>

  </form>
</div>  

Not tested but shouldn't be far off...

Comments