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
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.

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.