Apr 20, 2008

PHP Form Creation

PHP Form Creation

This page will provide an example form which is partially created from the SQL database. It mixes HTML and PHP to perform this function. A JavaScript routine which is shown on the next page is used to validate the form.

<?php
echo "<form name=\"addLink\" onSubmit=\"return checkForm(this);\" action=\"addurl.php\" method=\"POST\">";
?>
<table align="center" border ="0" cellspacing="0" cellpadding="0">
<tr><td><b>URL: </b></td><td><input type="text" name="url" size="60"></td></tr>
<tr><td><b>Optional GIF URL: </b></td><td><input type="text" name="gifurl" size="60"></td></tr>
<tr><td><b>Title: </b></td><td><input type="text" name="title" size="60"></td></tr>
<tr><td><b>Description: </b></td><td rowspan = "2"><textarea wrap="virtual" name="description" value="" cols="50" rows="5"></textarea></td><tr> <tr><td>(Up to 255 chars)</td></tr>
<tr><td><b>Your Name: </b></td><td><input type="text" name="uname" size="60"></td></tr>
<tr><td><b>Your E-mail: </b></td><td><input type="text" name="email" size="60"></td></tr>
<tr><td><b>Site Type: </b>></td><td><SELECT NAME="stype" SIZE="1">
<OPTION VALUE="0">Commercial
<OPTION VALUE="1">Government
<OPTION VALUE="2">Educational
<OPTION VALUE="3">Personal
<OPTION VALUE="4">Organizational
</select>
</td></tr>
<tr><td><b>Category: </b></td><td>
<?php
$chandle = mysql_connect("localhost", $dbuser, $dbpass)
or die("Connection Failure to Database");
mysql_select_db($dbname, $chandle) or die ("Database not found.");
$mainsection="sections";
$query1="select * from " . $mainsection . " order by t1.name"; //select all sections
$result = mysql_db_query($dbname, $query1) or die("Failed Query of " . $query1);
while($thisrow=mysql_fetch_row($result))
{
$sectdesc[$i1]=$thisrow[1];
$sectid[$i1]=$thisrow[0]; //set section values at level 1.
$i1++;
}
?>
<SELECT NAME="category" SIZE="1">
<?php
echo "<OPTION VALUE=\"\"> ";
for ($i2=1; $i2<=$i1; $i2++)
{
echo "<OPTION VALUE=\"" . $sectid[$i2] . "\">" . $sectdesc[$i2];
}
?>
</select></td><tr>
</table>
<table align="center" border ="0" cellspacing="5" cellpadding="5">
<tr><td><input type="submit" name="submit" value="Add/Change Link"></td><td><input type="reset" value="Clear Form"></td><tr>
</table>
</form>

The available categories are built from the PHP database by reading the available sections and setting their order to be alphabetical

No comments:

Post a Comment

Popular Posts