SirFirekicker Posted July 22, 2006 Share Posted July 22, 2006 Hello!I have a problem with a form that saves into a mysql table..The thing is I want to have 2 checkboxes example 1 and 2And I have a hidden field named test and I want the value of this hidden field to be " 'checkbox1' - 'checkbox2' "Because its the hidden field thats been sent to mysql table.How do I do that ??Sincerely, Johan Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/ Share on other sites More sharing options...
shocker-z Posted July 22, 2006 Share Posted July 22, 2006 I'm confused in what you want to acheive??RegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62012 Share on other sites More sharing options...
SirFirekicker Posted July 22, 2006 Author Share Posted July 22, 2006 I want the values from my checkboxes to be put in my 'meny''checkbox1' - 'checkbox2' - 'checkbox2' so it puts in: 'checkbox1' - 'checkbox2' - 'checkbox2' example 1 - 2 - 3 (if 1,2,3 is the values) mysql_query("INSERT INTO adminmeny (user, datum, meny) values ('" .$_POST['user']. "', '$datum', '" .$_POST['meny']. "' )") or exit(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62016 Share on other sites More sharing options...
SirFirekicker Posted July 22, 2006 Author Share Posted July 22, 2006 oops of course I mean 'checkbox1' - 'checkbox2' - 'checkbox3' Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62017 Share on other sites More sharing options...
dark dude Posted July 22, 2006 Share Posted July 22, 2006 Um... explain it slowly and I might be able to help you..Do you mean the script takes information from the checkbox form, then puts it into "adminmeny"? Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62019 Share on other sites More sharing options...
SirFirekicker Posted July 22, 2006 Author Share Posted July 22, 2006 I want to put the values of all the checked checkboxes into 'meny' Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62027 Share on other sites More sharing options...
SirFirekicker Posted July 22, 2006 Author Share Posted July 22, 2006 separeted by - Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62028 Share on other sites More sharing options...
wildteen88 Posted July 22, 2006 Share Posted July 22, 2006 Try, $meny = implode('-', $_POST['meny']);then use $meny within your query:ysql_query("INSERT INTO adminmeny (user, datum, meny) values ('" .$_POST['user']. "', '$datum', '" .$meny. "' )") or exit(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62036 Share on other sites More sharing options...
shocker-z Posted July 22, 2006 Share Posted July 22, 2006 So why dont you use somthing liek this?[code]Blue: <input type="checkbox" name="color[]" value="Blue">Red: <input type="checkbox" name="color[]" value="Red">Yellow: <input type="checkbox" name="color[]" value="Yellow">[/code]second page that info is submitted to[code]<?phpforeach($_POST['color'] as $color) {$colors=$colors.' - '.$color;}mysql_query("INSERT INTO adminmeny (user, datum, meny) values ('" .$_POST['user']. "', '$datum', '" .$colors. "' )") or exit(mysql_error());?>[/code]I just used colors as an example but that *should* work basicaly color[] makes the checkboxes into an array and the foreach taks each ticked checkbox value and adds it to the variable $colorsRegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62039 Share on other sites More sharing options...
SirFirekicker Posted July 22, 2006 Author Share Posted July 22, 2006 [quote author=wildteen88 link=topic=101461.msg401634#msg401634 date=1153571132]Try, $meny = implode('-', $_POST['meny']);then use $meny within your query:ysql_query("INSERT INTO adminmeny (user, datum, meny) values ('" .$_POST['user']. "', '$datum', '" .$meny. "' )") or exit(mysql_error());[/quote]Nothing is inserted.. my checkboxes:<input name="meny" type="checkbox" id="sidor" value="1"><input name="meny" type="checkbox" id="nyheter" value="2"> Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62046 Share on other sites More sharing options...
SirFirekicker Posted July 22, 2006 Author Share Posted July 22, 2006 [quote author=shocker-z link=topic=101461.msg401637#msg401637 date=1153571326]So why dont you use somthing liek this?[code]Blue: <input type="checkbox" name="color[]" value="Blue">Red: <input type="checkbox" name="color[]" value="Red">Yellow: <input type="checkbox" name="color[]" value="Yellow">[/code]second page that info is submitted to[code]<?phpforeach($_POST['color'] as $color) {$colors=$colors.' - '.$color;}mysql_query("INSERT INTO adminmeny (user, datum, meny) values ('" .$_POST['user']. "', '$datum', '" .$colors. "' )") or exit(mysql_error());?>[/code]I just used colors as an example but that *should* work basicaly color[] makes the checkboxes into an array and the foreach taks each ticked checkbox value and adds it to the variable $colorsRegardsLiam[/quote]That worked!! THANKS!! Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62050 Share on other sites More sharing options...
kenrbnsn Posted July 22, 2006 Share Posted July 22, 2006 If you use that solution and no boxes were checked, you will get an error since checkboxes are only returned to the script if they are checked. Also, your string will always start with a "-".To fix these problems, try:[code]<?php$colors = (isset($_POST['color'])?implode('-',$_POST['color']):'';$q = "INSERT INTO adminmeny (user, datum, meny) values ('" .$_POST['user']. "', '$datum', '$colors' )";$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());?>[/code]The first line says "if the array exists, use implode to put a dash between each element of the array else make the variable a zero length string".Ken Quote Link to comment https://forums.phpfreaks.com/topic/15324-help-form-to-mysql/#findComment-62062 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.