Help - Search - Members - Calendar
Full Version: forms created from a database
HTMLHelp Forums > Web Authoring > Web Site Functionality
rogerg1967
I am trying to setup a form to send a selection of towns to a database. The list of towns is taken from a database with a check box next to it and the submit button at the bottom. When the button is pressed i want the information to be passed to new records for each town selected. I am using dreamweaver to create the form and php mysql is the database. I am using a repeated regeion to diplay all the towns with a check box to the left. When I select the towns only the last record i selected goes into the database! can anyone please help with this as i have spent the last 7 days and nights trying to find the answer.

This is the lastest code which still wont work!!!!

<?php require_once('../Connections/gnprint.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
$insertSQL = sprintf("INSERT INTO distributor_regeions (town) VALUES (%s)",
GetSQLValueString($_POST['textfield'], "text"));

mysql_select_db($database_gnprint, $gnprint);
$Result1 = mysql_query($insertSQL, $gnprint) or die(mysql_error());
}

$colname_town_list = "-1";
if (isset($_POST['county_select'])) {
$colname_town_list = (get_magic_quotes_gpc()) ? $_POST['county_select'] : addslashes($_POST['county_select']);
}
mysql_select_db($database_gnprint, $gnprint);
$query_town_list = sprintf("SELECT * FROM county_town WHERE county = %s", GetSQLValueString($colname_town_list, "text"));
$town_list = mysql_query($query_town_list, $gnprint) or die(mysql_error());
$row_town_list = mysql_fetch_assoc($town_list);
$totalRows_town_list = mysql_num_rows($town_list);
?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form2" name="form2" method="post" action="<?php echo $editFormAction; ?>">
<label></label>
<label></label>
<label></label>
<table border="1">
<tr>
<?php
do { // horizontal looper version 3
?>
<td><table width="310" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20"><input type="checkbox" name="checkbox" value="checkbox" /></td>
<td width="144"><input name="textfield" type="text" id="textfield" value="<?php echo $row_town_list['town']; ?>" /></td>
<td width="76"><input type="hidden" name="MM_insert" value="form2" /></td>
</tr>
</table></td>
<?php
$row_town_list = mysql_fetch_assoc($town_list);
if (!isset($nested_town_list)) {
$nested_town_list= 1;
}
if (isset($row_town_list) && is_array($row_town_list) && $nested_town_list++ % 3==0) {
echo "</tr><tr>";
}
} while ($row_town_list); //end horizontal looper version 3
?>
</tr>
</table>
<input type="submit" name="Submit" value="Submit" />
</form>
<a href="select_county.php">Back
</a>
</body>
</html>
<?php
mysql_free_result($town_list);
?>

Thanks in advance

Roger
Brian Chandler
I haven't checked very carefully, but it looks as though you have lots of rows of the table, including a set of input elements, but the elements in different rows all have the same name. E.g. the checkbox is always called 'checkbox'. So the POST data can only give you one value, which is the last one.

You need to make each input element have a different name: so if the variable $town holds the current name (within the loop), instead of

<input type="checkbox" name="checkbox" value="checkbox" >

try

<input type="checkbox" name="ch$town" value="x" >

Then instead of the POST data looking like

checkbox=Bristol
checkbox=Cardiff
checkbox=Derby

[so what's the value of 'checkbox'... ?@#$!]

it will look like

chBristol=x
chCardiff=x
chDerby=x

and you can see which three are selected.
Darin McGrew
Well, you can use the same name for multiple checkboxes, but then you need to keep looking for values after finding the first value with that name. With Perl's CGI.pm, you do that by storing an array of values, rather than a scalar value. But I'm not familiar with PHP.
Brian Chandler
QUOTE
Well, you can use the same name for multiple checkboxes, but then you need to keep looking for values after finding the first value with that name. With Perl's CGI.pm, you do that by storing an array of values, rather than a scalar value. But I'm not familiar with PHP.



There is some hack for doing it (which I've forgotten), but it just seems better style to have parameters passed as post data being honest name-value pairs.
Christian J
Can you use NAME="foo[]" in this case, like you do with a MULTIPLE SELECT?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.