[英]Html form from php array
I want to display an html form from php array.
我想从php数组中显示一个html表单。 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
我已经使用了bootstrap行类,但它将两个输入分别用于单独的行
<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>
Update your PHP array to use Bootstrap sizing instead of percentages. 更新PHP数组以使用Bootstrap大小而不是百分比。 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, 然后,您可以使用该数据通过foreach循环创建表单,
<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... 未经测试但不应远离......
You can manage by conditions. 您可以按条件进行管理。
foreach ($form as $name => $elements) {
if($name != 'lname'){
echo '<div class="row">';
}
// Code here
if($name != 'lname'){
echo "</div>";
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.