Jump to content

[SOLVED] Checkbox implode problem


Mutley

Recommended Posts

Hi guys, small problem, has worked for me before, not sure why it isn't now:

 

FORM:

 

10:00 - 11:00 <input type="checkbox" name="time[]" value="10:00 - 11:00" /><br />

11:00 - 12:00 <input type="checkbox" name="time[]" value="11:00 - 12:00" /><br />

12:00 - 13:00 <input type="checkbox" name="time[]" value="12:00 - 13:00" /><br />

13:00 - 14:00 <input type="checkbox" name="time[]" value="13:00 - 14:00" /><br />

14:00 - 15:00 <input type="checkbox" name="time[]" value="14:00 - 15:00" /><br />

15:00 - 16:00 <input type="checkbox" name="time[]" value="15:00 - 16:00" /><br />

 

PHP:

 

$times = $_POST['time'];

$times = implode(",", $times);

 

Link to comment
Share on other sites

The output of the form based on his input would be something like this:

 

time[]=11%3A00+-+12%3A00&time[]=12%3A00+-+13%3A00&time[]=13%3A00+-+14%3A00

 

So in the PHP, you would have to loop through the "time" variable and combine them.

Link to comment
Share on other sites

ober, what do you mean by that.

When you have a multi-select, the selected items are passed as an array identified by (in case of method=post where object name is "pick[]") $_POST['pick']

The array only contains the selected items. Same with a checkbox array, only the items checked will be passed into $_POST['time'] array.

 

Mutley, if you simply aren't getting anything into the array $_POST['time'], be sure your forms method = "post". That's a common thing to look over.

Link to comment
Share on other sites

OMG... I'm an idiot. I just got done answering an explode thread and my mind was stuck on that.

 

Mutley, ignore my posts in this thread. I need to go take a break. What you have should work fine with the exception of what Brian mentioned.

Link to comment
Share on other sites

No worries ober! :)

 

Form seems fine.

 

<form action="" method="post">
<table>
<tr>
	<td>Event Name:</td>
	<td><input type="text" name="name" /></td>
</tr>
<tr>
	<td colspan="2">Event Information:</td>
</tr>
<tr>
	<td colspan="2"><textarea id="content" style="width:450px; height:200px" name="info"></textarea></td>
</tr>
<tr>
	<td>Dates of Event:</td>
	<td>
		Day 1: 
		<select name="sD1">
			<?
			// Cycle through days
			$i=1;
			while($i <= 31){
				if($i==date("j")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>
		<select name="sM1">
			<option value="1" <? if(date("n")==1){ echo "selected";}?>>January</option>
			<option value="2" <? if(date("n")==2){ echo "selected";}?>>February</option>
			<option value="3" <? if(date("n")==3){ echo "selected";}?>>March</option>
			<option value="4" <? if(date("n")==4){ echo "selected";}?>>April</option>
			<option value="5" <? if(date("n")==5){ echo "selected";}?>>May</option>
			<option value="6" <? if(date("n")==6){ echo "selected";}?>>June</option>
			<option value="7" <? if(date("n")==7){ echo "selected";}?>>July</option>
			<option value="8" <? if(date("n")=={ echo "selected";}?>>August</option>
			<option value="9" <? if(date("n")==9){ echo "selected";}?>>September</option>
			<option value="10" <? if(date("n")==10){ echo "selected";}?>>October</option>
			<option value="11" <? if(date("n")==11){ echo "selected";}?>>November</option>
			<option value="12" <? if(date("n")==12){ echo "selected";}?>>December</option>
		</select>
		<select name="sY1">
			<?
			// Cycle through years
			$i=date("Y");
			while($i <= (date( "Y" )+20)){
				if($i==date("Y")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>
		<br />Day 2: 
		<select name="sD2">
			<?
			// Cycle through days
			$i=1;
			while($i <= 31){
				if($i==date("j")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>
		<select name="sM2">
			<option value="1" <? if(date("n")==1){ echo "selected";}?>>January</option>
			<option value="2" <? if(date("n")==2){ echo "selected";}?>>February</option>
			<option value="3" <? if(date("n")==3){ echo "selected";}?>>March</option>
			<option value="4" <? if(date("n")==4){ echo "selected";}?>>April</option>
			<option value="5" <? if(date("n")==5){ echo "selected";}?>>May</option>
			<option value="6" <? if(date("n")==6){ echo "selected";}?>>June</option>
			<option value="7" <? if(date("n")==7){ echo "selected";}?>>July</option>
			<option value="8" <? if(date("n")=={ echo "selected";}?>>August</option>
			<option value="9" <? if(date("n")==9){ echo "selected";}?>>September</option>
			<option value="10" <? if(date("n")==10){ echo "selected";}?>>October</option>
			<option value="11" <? if(date("n")==11){ echo "selected";}?>>November</option>
			<option value="12" <? if(date("n")==12){ echo "selected";}?>>December</option>
		</select>
		<select name="sY2">
			<?
			// Cycle through years
			$i=date("Y");
			while($i <= (date( "Y" )+20)){
				if($i==date("Y")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>
		<br />Day 3: 
		<select name="sD3">
			<?
			// Cycle through days
			$i=1;
			while($i <= 31){
				if($i==date("j")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>
		<select name="sM3">
			<option value="1" <? if(date("n")==1){ echo "selected";}?>>January</option>
			<option value="2" <? if(date("n")==2){ echo "selected";}?>>February</option>
			<option value="3" <? if(date("n")==3){ echo "selected";}?>>March</option>
			<option value="4" <? if(date("n")==4){ echo "selected";}?>>April</option>
			<option value="5" <? if(date("n")==5){ echo "selected";}?>>May</option>
			<option value="6" <? if(date("n")==6){ echo "selected";}?>>June</option>
			<option value="7" <? if(date("n")==7){ echo "selected";}?>>July</option>
			<option value="8" <? if(date("n")=={ echo "selected";}?>>August</option>
			<option value="9" <? if(date("n")==9){ echo "selected";}?>>September</option>
			<option value="10" <? if(date("n")==10){ echo "selected";}?>>October</option>
			<option value="11" <? if(date("n")==11){ echo "selected";}?>>November</option>
			<option value="12" <? if(date("n")==12){ echo "selected";}?>>December</option>
		</select>
		<select name="sY3">
			<?
			// Cycle through years
			$i=date("Y");
			while($i <= (date( "Y" )+20)){
				if($i==date("Y")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>


	</td>
</tr>
<tr>
	<td>Times:</td>
	<td>

		<table>
			<tr>
				<td>
					10:00 - 11:00 <input type="checkbox" name="time[]" value="10:00 - 11:00" /><br />
					11:00 - 12:00 <input type="checkbox" name="time[]" value="11:00 - 12:00" /><br />
					12:00 - 13:00 <input type="checkbox" name="time[]" value="12:00 - 13:00" /><br />
					13:00 - 14:00 <input type="checkbox" name="time[]" value="13:00 - 14:00" /><br />
					14:00 - 15:00 <input type="checkbox" name="time[]" value="14:00 - 15:00" /><br />
					15:00 - 16:00 <input type="checkbox" name="time[]" value="15:00 - 16:00" /><br />
				</td><td>
					16:00 - 17:00 <input type="checkbox" name="time[]" value="16:00 - 17:00" /><br />
					17:00 - 18:00 <input type="checkbox" name="time[]" value="17:00 - 18:00" /><br />
					18:00 - 19:00 <input type="checkbox" name="time[]" value="18:00 - 19:00" /><br />
					19:00 - 20:00 <input type="checkbox" name="time[]" value="19:00 - 20:00" /><br />
					20:00 - 21:00 <input type="checkbox" name="time[]" value="20:00 - 21:00" /><br />
					21:00 - 22:00 <input type="checkbox" name="time[]" value="21:00 - 22:00" />
				</td>
			</tr>
		</table>

	</td>
<tr>
	<td> </td>
	<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>

Link to comment
Share on other sites

Must be something wrong with your server config... it works fine for me when I copy and paste your code:

 

Array

(

    [0] => 11:00 - 12:00

    [1] => 12:00 - 13:00

    [2] => 13:00 - 14:00

)

 

Link to comment
Share on other sites

Whole file code, including PHP part:

 

<?
if($_POST['name']){

$name = mysql_real_escape_string($_POST['name']);
$info = mysql_real_escape_string($_POST['info']);
$gettimes = $_POST['time'];
$times = implode(",", $gettimes);

$sD1 = mysql_real_escape_string($_POST['sD1']);
$sD2 = mysql_real_escape_string($_POST['sD2']);
$sD3 = mysql_real_escape_string($_POST['sD3']);

$sM1 = mysql_real_escape_string($_POST['sM1']);
$sM2 = mysql_real_escape_string($_POST['sM2']);
$sM3 = mysql_real_escape_string($_POST['sM3']);

$sY1 = mysql_real_escape_string($_POST['sY1']);
$sY2 = mysql_real_escape_string($_POST['sY2']);
$sY3 = mysql_real_escape_string($_POST['sY3']);

if(empty($name) || empty($info)){
echo "<b>Please fill in all the fields</b><br /><br />";
}else{

$sql="INSERT INTO `events` (`name`,`info`,`date`,`times`) VALUES ('$name','$info','$sD1-$sM1-$sY1,$sD2-$sM2-$sY2,$sD3-$sM3-$sY3','$times')";
mysql_query($sql);
//echo $sql;

echo "<b>Thank-you, the event has been created succesfully.</b><br /><br />";

}
}
?>

<script type="text/javascript" src="../jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
	// General options
	mode : "textareas",
	theme : "advanced",
	plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

	// Theme options
	theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,

	// Example content CSS (should be your site CSS)
	content_css : "css/content.css",

	// Drop lists for link/image/media/template dialogs
	template_external_list_url : "lists/template_list.js",
	external_link_list_url : "lists/link_list.js",
	external_image_list_url : "lists/image_list.js",
	media_external_list_url : "lists/media_list.js",
	forced_root_block : '',

	// Replace values for the template plugin
	template_replace_values : {
		username : "Some User",
		staffid : "991234"
	}
});
</script>

You can add a new event below.
<br /><br />
Once you add a new event, it automatically becomes live and removes the current one from the page.
<br /><br />

<form action="" method="post">
<table>
<tr>
	<td>Event Name:</td>
	<td><input type="text" name="name" /></td>
</tr>
<tr>
	<td colspan="2">Event Information:</td>
</tr>
<tr>
	<td colspan="2"><textarea id="content" style="width:450px; height:200px" name="info"></textarea></td>
</tr>
<tr>
	<td>Dates of Event:</td>
	<td>
		Day 1: 
		<select name="sD1">
			<?
			// Cycle through days
			$i=1;
			while($i <= 31){
				if($i==date("j")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>
		<select name="sM1">
			<option value="1" <? if(date("n")==1){ echo "selected";}?>>January</option>
			<option value="2" <? if(date("n")==2){ echo "selected";}?>>February</option>
			<option value="3" <? if(date("n")==3){ echo "selected";}?>>March</option>
			<option value="4" <? if(date("n")==4){ echo "selected";}?>>April</option>
			<option value="5" <? if(date("n")==5){ echo "selected";}?>>May</option>
			<option value="6" <? if(date("n")==6){ echo "selected";}?>>June</option>
			<option value="7" <? if(date("n")==7){ echo "selected";}?>>July</option>
			<option value="8" <? if(date("n")=={ echo "selected";}?>>August</option>
			<option value="9" <? if(date("n")==9){ echo "selected";}?>>September</option>
			<option value="10" <? if(date("n")==10){ echo "selected";}?>>October</option>
			<option value="11" <? if(date("n")==11){ echo "selected";}?>>November</option>
			<option value="12" <? if(date("n")==12){ echo "selected";}?>>December</option>
		</select>
		<select name="sY1">
			<?
			// Cycle through years
			$i=date("Y");
			while($i <= (date( "Y" )+20)){
				if($i==date("Y")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>
		<br />Day 2: 
		<select name="sD2">
			<?
			// Cycle through days
			$i=1;
			while($i <= 31){
				if($i==date("j")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>
		<select name="sM2">
			<option value="1" <? if(date("n")==1){ echo "selected";}?>>January</option>
			<option value="2" <? if(date("n")==2){ echo "selected";}?>>February</option>
			<option value="3" <? if(date("n")==3){ echo "selected";}?>>March</option>
			<option value="4" <? if(date("n")==4){ echo "selected";}?>>April</option>
			<option value="5" <? if(date("n")==5){ echo "selected";}?>>May</option>
			<option value="6" <? if(date("n")==6){ echo "selected";}?>>June</option>
			<option value="7" <? if(date("n")==7){ echo "selected";}?>>July</option>
			<option value="8" <? if(date("n")=={ echo "selected";}?>>August</option>
			<option value="9" <? if(date("n")==9){ echo "selected";}?>>September</option>
			<option value="10" <? if(date("n")==10){ echo "selected";}?>>October</option>
			<option value="11" <? if(date("n")==11){ echo "selected";}?>>November</option>
			<option value="12" <? if(date("n")==12){ echo "selected";}?>>December</option>
		</select>
		<select name="sY2">
			<?
			// Cycle through years
			$i=date("Y");
			while($i <= (date( "Y" )+20)){
				if($i==date("Y")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>
		<br />Day 3: 
		<select name="sD3">
			<?
			// Cycle through days
			$i=1;
			while($i <= 31){
				if($i==date("j")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>
		<select name="sM3">
			<option value="1" <? if(date("n")==1){ echo "selected";}?>>January</option>
			<option value="2" <? if(date("n")==2){ echo "selected";}?>>February</option>
			<option value="3" <? if(date("n")==3){ echo "selected";}?>>March</option>
			<option value="4" <? if(date("n")==4){ echo "selected";}?>>April</option>
			<option value="5" <? if(date("n")==5){ echo "selected";}?>>May</option>
			<option value="6" <? if(date("n")==6){ echo "selected";}?>>June</option>
			<option value="7" <? if(date("n")==7){ echo "selected";}?>>July</option>
			<option value="8" <? if(date("n")=={ echo "selected";}?>>August</option>
			<option value="9" <? if(date("n")==9){ echo "selected";}?>>September</option>
			<option value="10" <? if(date("n")==10){ echo "selected";}?>>October</option>
			<option value="11" <? if(date("n")==11){ echo "selected";}?>>November</option>
			<option value="12" <? if(date("n")==12){ echo "selected";}?>>December</option>
		</select>
		<select name="sY3">
			<?
			// Cycle through years
			$i=date("Y");
			while($i <= (date( "Y" )+20)){
				if($i==date("Y")){
					$sel="selected";
				}else{
					$sel="";
				}
				echo "<option value='$i' $sel>$i</option>";
				$i++;
			}
			?>
		</select>


	</td>
</tr>
<tr>
	<td>Times:</td>
	<td>

		<table>
			<tr>
				<td>
					10:00 - 11:00 <input type="checkbox" name="time[]" value="10:00 - 11:00" /><br />
					11:00 - 12:00 <input type="checkbox" name="time[]" value="11:00 - 12:00" /><br />
					12:00 - 13:00 <input type="checkbox" name="time[]" value="12:00 - 13:00" /><br />
					13:00 - 14:00 <input type="checkbox" name="time[]" value="13:00 - 14:00" /><br />
					14:00 - 15:00 <input type="checkbox" name="time[]" value="14:00 - 15:00" /><br />
					15:00 - 16:00 <input type="checkbox" name="time[]" value="15:00 - 16:00" /><br />
				</td><td>
					16:00 - 17:00 <input type="checkbox" name="time[]" value="16:00 - 17:00" /><br />
					17:00 - 18:00 <input type="checkbox" name="time[]" value="17:00 - 18:00" /><br />
					18:00 - 19:00 <input type="checkbox" name="time[]" value="18:00 - 19:00" /><br />
					19:00 - 20:00 <input type="checkbox" name="time[]" value="19:00 - 20:00" /><br />
					20:00 - 21:00 <input type="checkbox" name="time[]" value="20:00 - 21:00" /><br />
					21:00 - 22:00 <input type="checkbox" name="time[]" value="21:00 - 22:00" />
				</td>
			</tr>
		</table>

	</td>
<tr>
	<td> </td>
	<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>

Link to comment
Share on other sites

It seems when I do fill the checkboxes in, I get this error:

 

Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /home/sfengine/public_html/admin/includes/header.inc.php on line 27

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/sfengine/public_html/admin/includes/header.inc.php:27) in /home/sfengine/public_html/admin/includes/header.inc.php on line 35

 

But when I don't fill them in, it works without any errors.

 

Why would a checkbox POST cause this?

Link to comment
Share on other sites

Problem found but no soloution....

 

This code is in the header:

 

	foreach ($_POST as $key => $el) {
	$_POST[$key] = mysql_real_escape_string($el);
}
//Get

 

Cleans the POSTs but how do I bypass it for the checkboxes and not make other forms on other pages insecure?

Link to comment
Share on other sites

change

   foreach ($_POST as $key => $el) {
      $_POST[$key] = mysql_real_escape_string($el);
   }

to

   foreach ($_POST as $key => $el) {
     if(!is_array($el)){
      $_POST[$key] = mysql_real_escape_string($el);
     }
   }

Link to comment
Share on other sites

Because when you dont select any checkboxes, $_POST['time'] wont exist. However when you do select a checkbox it will.

 

mysql_real_escape_string should only be used for strings. $_POST['time'] will be an array and this is why you get an error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.