Jump to content

Help!! form to mysql


SirFirekicker

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
Link to comment
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());
Link to comment
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
Link to comment
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">
Link to comment
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!!
Link to comment
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
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.