Mutley Posted March 30, 2009 Share Posted March 30, 2009 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); Quote Link to comment Share on other sites More sharing options...
ober Posted March 30, 2009 Share Posted March 30, 2009 That's probably because those results are not separated by commas in $_POST. They are passed as array values. print_r($_POST); and then you can see what I'm talking about. Quote Link to comment Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 ober, implode takes an array and makes a string out of it separating the values by parameter 1. Mutley, by the looks of it, that code should work for you, what is it doing that is not what it is supposed to do? Quote Link to comment Share on other sites More sharing options...
ober Posted March 30, 2009 Share Posted March 30, 2009 <snip> Sorry Brian, I'm an idiot! Quote Link to comment Share on other sites More sharing options...
ober Posted March 30, 2009 Share Posted March 30, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mutley Posted March 30, 2009 Author Share Posted March 30, 2009 How would I do that? Quote Link to comment Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 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. Quote Link to comment Share on other sites More sharing options...
ober Posted March 30, 2009 Share Posted March 30, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mutley Posted March 30, 2009 Author Share Posted March 30, 2009 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> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 30, 2009 Share Posted March 30, 2009 What do you get when you do if(isset($_POST['time'])) { echo '<pre>'.print_r($_POST['time'], true).'</pre>'; } Quote Link to comment Share on other sites More sharing options...
Mutley Posted March 30, 2009 Author Share Posted March 30, 2009 Nothing, the IF statement doesn't even pass. ??? Thanks for your help. Quote Link to comment Share on other sites More sharing options...
ober Posted March 30, 2009 Share Posted March 30, 2009 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 ) Quote Link to comment Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 can we see the code w/ the test code from wildteen? Still trying to trouble shoot this Quote Link to comment Share on other sites More sharing options...
Mutley Posted March 30, 2009 Author Share Posted March 30, 2009 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> Quote Link to comment Share on other sites More sharing options...
Mutley Posted March 30, 2009 Author Share Posted March 30, 2009 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? Quote Link to comment Share on other sites More sharing options...
Mutley Posted March 30, 2009 Author Share Posted March 30, 2009 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? Quote Link to comment Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 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); } } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 30, 2009 Share Posted March 30, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.