Jump to content


Photo

Help!! form to mysql


  • Please log in to reply
11 replies to this topic

#1 SirFirekicker

SirFirekicker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 July 2006 - 10:02 AM

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 2

And 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

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 22 July 2006 - 10:57 AM

I'm confused in what you want to acheive??

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 SirFirekicker

SirFirekicker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 July 2006 - 11:23 AM

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());

#4 SirFirekicker

SirFirekicker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 July 2006 - 11:24 AM

oops

of course I mean 'checkbox1' - 'checkbox2' - 'checkbox3'

#5 dark dude

dark dude
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 22 July 2006 - 11:32 AM

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"?

#6 SirFirekicker

SirFirekicker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 July 2006 - 11:52 AM

I want to put the values of all the checked checkboxes into 'meny'

#7 SirFirekicker

SirFirekicker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 July 2006 - 11:53 AM

separeted by -

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 12:25 PM

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());

#9 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 22 July 2006 - 12:28 PM

So why dont you use somthing liek this?

Blue: <input type="checkbox" name="color[]" value="Blue">
Red: <input type="checkbox" name="color[]" value="Red">
Yellow: <input type="checkbox" name="color[]" value="Yellow">

second page that info is submitted to

<?php
foreach($_POST['color'] as $color) {
$colors=$colors.' - '.$color;
}
mysql_query("INSERT INTO adminmeny (user, datum, meny) values ('" .$_POST['user']. "', '$datum', '" .$colors. "' )") or exit(mysql_error());
?>


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 $colors

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#10 SirFirekicker

SirFirekicker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 July 2006 - 12:53 PM

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());


Nothing is inserted..

my checkboxes:
<input name="meny" type="checkbox" id="sidor" value="1">
<input name="meny" type="checkbox" id="nyheter" value="2">

#11 SirFirekicker

SirFirekicker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 July 2006 - 12:57 PM

So why dont you use somthing liek this?

Blue: <input type="checkbox" name="color[]" value="Blue">
Red: <input type="checkbox" name="color[]" value="Red">
Yellow: <input type="checkbox" name="color[]" value="Yellow">

second page that info is submitted to

<?php
foreach($_POST['color'] as $color) {
$colors=$colors.' - '.$color;
}
mysql_query("INSERT INTO adminmeny (user, datum, meny) values ('" .$_POST['user']. "', '$datum', '" .$colors. "' )") or exit(mysql_error());
?>


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 $colors

Regards
Liam


That worked!! THANKS!!

#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 July 2006 - 02:24 PM

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:
<?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());
?>

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users