Help with Form Array
Posted: Thu Apr 03, 2008 1:01 pm
Hi, I'm semi new to php - I've been learning it for about a year now and I'm trying to create a form. I want the form to have multiple input field, but not all the fields have to be filled out. But the fields that are filled out print, and those that do not don't.
There is a name and email field to fill out, then there are other fields that are optional. Say you fill out the contact info but only one option field it should out put:
Name Email DATA1
if you fill out two fields it should output
Name Email DATA1
Name Email DATA2
But this is what happends (one field filled out)
Name Email Data1
Name Email
Please Help - Here is my code:
***** PLEASE USE THE CODE TAG WHEN POSTING *****
There is a name and email field to fill out, then there are other fields that are optional. Say you fill out the contact info but only one option field it should out put:
Name Email DATA1
if you fill out two fields it should output
Name Email DATA1
Name Email DATA2
But this is what happends (one field filled out)
Name Email Data1
Name Email
Please Help - Here is my code:
***** PLEASE USE THE CODE TAG WHEN POSTING *****
Code: Select all
<?php
$contact = ($_POST['name']." ". $_POST['email']);
$color = array($_POST['color1'],$_POST['color2'],$_POST['color3']);
if (array_key_exists('submit', $_POST)) {
foreach ($color as $value) {
echo "$contact"." "."$value"."<br />";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
</head>
<body>
<div style="width:400px;">
<fieldset>
<legend><b>Fav Colors</b></legend>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<table width="36%" border="0" cellspacing="3">
<tr>
<td width="49%">Name
<input type="text" name="name" id="name" /></td>
<td width="51%">Email
<input type="text" name="email" id="email" /></td>
</tr>
<tr>
<td colspan="2"><br />
Colors:
<br />
<input type="text" name="color1" id="color1" /></td>
</tr>
<tr>
<td colspan="2"><input type="text" name="color2" id="color2" /></td>
</tr>
<tr>
<td colspan="2"><input type="text" name="color3" id="color3" /></td>
</tr>
<tr>
<td colspan="2"><br /><input type="submit" name="submit" id="submit" value="Submit" /> </td>
</tr>
</table>
</form>
</fieldset>
</div>
</body>
</html>