Page 1 of 1

Ignoring spaces from checkbox input

Posted: Wed Feb 28, 2007 10:42 am
by divasatanica
Hi, I have a form that gets its checkbox values from a table called 'destination'. The form also gathers user information and puts it in another table called 'customer'. When it comes to selecting the checkbox values and submitting, the code works as it takes the customer's userid and the destination id from the checkbox values and putting them in a new table called 'custdest' EXCEPT if the destination has a space in it's name, e.g "New York". It completely ignores this value and moves on to the next.

Here's the code for the form

Code: Select all

mysql_select_db("system", $con);
$result = mysql_query("SELECT * FROM destination");

while($row = mysql_fetch_array($result))
  {
  echo $row['destname'];
  echo "<input type=\"checkbox\" name=\"" . $row['destname'] . "\" value=\"" . $row['destid'] . "\">";
  echo "<br />";
  }

mysql_close($con);
?>
and here's the code for the processing

Code: Select all

while($row = mysql_fetch_array($result))
  {
  $currentdbdest = $row['destname'];
  $currentpostdest = $_POST[$currentdbdest] + "";
  echo $currentpostdest;
  if ($currentpostdest != null){
		echo $row['destname'];
		mysql_query("INSERT INTO custdest (userid, destid) VALUES ('$userid', '$row[destid]')");
	}
  }
anyone's help will be much appreciated!

Posted: Wed Feb 28, 2007 10:47 am
by feyd
The browser is renaming the name because it's not correct. It would be more simple to simply name the check boxes the same name (that would conveniently store into an array you can iterate over.)

A name such as "selection[]" would populate $_POST['selection'] as an array of the values.

Posted: Wed Feb 28, 2007 1:17 pm
by visitor-Q
feyd wrote:The browser is renaming the name because it's not correct. It would be more simple to simply name the check boxes the same name (that would conveniently store into an array you can iterate over.)

A name such as "selection[]" would populate $_POST['selection'] as an array of the values.
i agree with feyd. as far as i know, that is standard procedure for processing checkboxes using php.