Jump to content

dynamic form creation question


yellowepi

Recommended Posts

I don't know if this is the best way to develop a form dynamically or not, but this is the way I did.

I made an array and used the keys and values to fill in to make the form.

 

[red]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
<html>

<head>
	<title></title>
</head>
<body>
<table><form>
<?php
$forminput = array(
"Username" => "username",
"First Name" => "fname",
"Last Name" => "lname",
);
foreach ($forminput as $field => $value) {
        echo "<tr><td>" .$field . ": </td>
		 <td><input type='text' name='" .$value."'></td></tr>";
}

?> 
</form></table>
</body>
</html>

 

Is this the best way for creation of a form, or are there better ways. I am new to programming and feel pretty good about this one, but since I am new I am sure there are better ways.

Link to comment
https://forums.phpfreaks.com/topic/49067-dynamic-form-creation-question/
Share on other sites

I'm still very new at using php, and have heard of seperating all of this out. I know there are prebuilt templates like smarty etc, but how would you code this to make your own template?

 

If it would take to long to explain-just say so and I will try to figure it out.

how i would code this? using smarty:

 

login.tpl

{include file = 'header.tpl'}

<form action='{$action}' method='post'>
<table border="0">
<tr>
  <td><label for='{$lblUsername}'>{$txtUsername}</label></td>
  <td><input type='text' id='{$lblUsername}' name='{$lblUsername}' maxlength='25' /></td>
</tr>
<tr>
  <td><label for='{$lblPassword}'>{$txtPassword}</label></td>
  <td><input
  ...
</tr>
</table>
</form>

{include file='footer.tpl'}

 

login.php

$oSmarty = new Smarty;
$oSmarty->assign('action', '');
$oSmarty->assign('lblUsername', 'username');
// If you are using PEAR packages like Auth, this would be something like:
// $oSmarty->assign('lblUsername', $oAuth->getPostUsernameField());

$oSmarty->assign('txtUsername', 'Username:');
...
$oSmarty->display('login.tpl');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.