<tr>
<td valign="top">Area code:</td>
<td>
<select id="area" name="area[]" size=6 multiple>
//Use area[] for php to get more value
<script>
frm = this.document.formname;
frm.area.options[frm.area.length] = new Option("--", "", false, false);
</script>
<?php
$id = @mysql_connect($host, $user,$dbpasswd) or
ErrMsg("Cannot connect to MySQL server");
@mysql_select_db($dbname,$id) or ErrMsg("Cannot open database");
$query = "select * from area where id=$uid;";
$result = mysql_query($query, $id);
while ($uarray = mysql_fetch_array($result)){
$area = $uarray["area"];
echo "<option value='$area'>$area";
}
mysql_free_result($result);
mysql_close($id);
?>
</select>
</td>
</tr>
<?php
// Get Multi-select data in a form
foreach($_POST['area'] as $value){
if ($value != ''){
$query = "insert into area (uid, area) values($uid, '$value');";
$result = mysql_query($query, $id) or ErrMsg("Query Error: $query");
}
}
/* To get multi check in a form:
Give the group of check box the same name, end with [],
PHP compiles the user input for this field into an array.
*/
?>
<input type='checkbox' name="codes[]" value='aaa'>
//Use codes[] for php to get more value as an array
<input type='checkbox' name="codes[]" value='bbb'>
//if didn't set value, php will get 'on' when checked
//else php get the value when checked.
//If unchecked, php get null no matter value is set.
<input type='hidden' value='' name='codesarray'>
<script>
function validate()
{
// Get checkbox array before submit
codearray = document.Formname.elements['codes[]'];
for (i=0; i{
if (codearray[i].checked == true)
{
document.Formname.codesarray.value += codearray[i].value + ",";
}
}
}
</script>
<?php
if (!isset($_POST['codes']))
{
$codesarray = '';
}else{
// Get an array of codes
$codes = $_POST['codes'];
$codesStr = implode(",", $codes); // combine entries into a string
}
echo "\n";
?>
Wednesday, October 05, 2005
PHP Tips -- Form
** get Multi-select data in a form
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment