Jump to content

Archived

This topic is now archived and is closed to further replies.

SirFirekicker

Help!! form to mysql

Recommended Posts

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

Share this post


Link to post
Share on other sites
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());

Share this post


Link to post
Share on other sites
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"?

Share this post


Link to post
Share on other sites
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());

Share this post


Link to post
Share on other sites
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]
<?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());
?>
[/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 $colors

Regards
Liam

Share this post


Link to post
Share on other sites
[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">

Share this post


Link to post
Share on other sites
[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]
<?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());
?>
[/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 $colors

Regards
Liam
[/quote]

That worked!! THANKS!!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.