Jump to content

checkbox with multiple values processing into MySql


rilana

Recommended Posts

Hey Guyes it would be soooo awsom to get your help, i am soo stuck and soo despred, I need to finish this project and i just dont get how to do it.

I have a form to sign up for a newsletter which works fine. But now for the mobile version I changed it arround and I wanna give the user less choices. That means if the check a checkbox I should be able to assign multiple values in one checkbox. Like so:

<input type="checkbox" name="beruf[]"  value="4','9','10','11"/>

But the problem is that when I pass it thrue my php it doesnt understand that these are 4 different values. My PHP looks like that

$beruf = strip_tags($_POST['beruf']);

foreach($_POST['beruf'] as $b)

$sql = "INSERT INTO newsletter_beruf (userID, berufID) VALUES ";

$c = count($_POST['beruf']);

echo "anuzahl berufe $c<br>";

if($c > 1){

  $i=1;

  foreach($_POST['beruf'] as $b){

  $comma = ($i < $c) ? ',' : '';//add a coma to the end, but not the last one

  $sql .= "($userID,$b)$comma";

  $i++;

  }

  $sql .= '';

}else if ($c == 1){

  $sql .= "($userID, $b)";

}

echo $sql;

$mysql = mysql_query($sql);

 

}

 

the output is INSERT INTO newsletter_beruf (userID, berufID) VALUES (162, 4','9','10','11)

unstead of INSERT INTO newsletter_beruf (userID, berufID) VALUES (162,4),(162,9),(162,10),(162,11)

 

how can i do this right? Something like get the checkbox multiple values and assign each value to one.... please help, I would really apprechiate it!

Thabk you

Link to comment
Share on other sites

when you do this:

<input type="checkbox" name="beruf[]"  value="4,9,10,11"/>

 

because you included [] after the input name, it means the result will be an array, so you can have several inputs with the same name.

 

you grab the value with the array index:

$beruf = strip_tags($_POST['beruf'][0]);

 

if you only have 1 input, remove the [] so it will looklike this:

 

<input type="checkbox" name="beruf"  value="4,9,10,11"/>

 

and then this should work:

 

 

$beruf = strip_tags($_POST['beruf']);

Link to comment
Share on other sites

OK, I think I understand, but I do have more checkboxes with multiple values each time and they should all be in the same array.

So the only thing I can think off now is to make them all a different name in the form and then create the array in the PHP, does that sound right to you?

Link to comment
Share on other sites

there's nothing wrong with having the array... you can grab each one with:

 

$beruf1 = strip_tags($_POST['beruf'][0]);
$beruf2 = strip_tags($_POST['beruf'][1]);
$beruf3 = strip_tags($_POST['beruf'][2]);

 

of just do:

foreach($_POST['beruf'] as $val){
echo '<br />'.strip_tags($val);
}

Link to comment
Share on other sites

I am still trying to find a solution for this one. But no matter what I do it does not work. I tryed the explode function. like this:

 

<input type="checkbox" name="beruf[]"  value="4,9,10,11"/>

$berufZahlen = $_POST['beruf']; 
$beruf = explode(",",$berufZahlen);
print_r (explode(",",$berufZahlen));

 

But the explode function seems to be empty, the print_r returns

 

Array

(

    [0] => Array

)

 

But $beruf still somehow is filled, the mysql echo is like this

INSERT INTO newsletter_beruf (userID, berufID) VALUES (179, 4,9,10,11)

 

so in the end, still the same problem....

 

Please anymone, what am I doing wrong?

Link to comment
Share on other sites

did you even read what I said 2 posts ago?

 

you are sending over an array, therefor, when you grab the value, you are actually grabbing an entire array. You DO NOT want to explode() an array, that would just be silly, since explode creates an array from a string.

 

Print out the array:

 

echo '<pre>';
print_r($_POST['beruf']);
echo '</pre>';

 

 

Link to comment
Share on other sites

Hi, thank you. I read all your post and tryed it out. but here is the problem :

 

Array
(
    [0] => 4,9,10,11
)

 

this is when I print out the Array, it is that way because I am putting multiple values in one checkbox. And I will do this for more than one checkbox with the same name. So this checkbox should create this:

 

Array
(
    [0] => 4
    [1] => 9
    [2] => 10
    [3] => 11
)

Link to comment
Share on other sites

As I've said before... You are sending over an array like the one you printed:

Array
(
    [0] => 4,9,10,11
)

 

LOOK AT IT. it's an array containing ONE value at index 0 (zero). The value is 4,9,10,11.

if you want to seperate those values, you EXPLODE them (like I also said before). But you don't explode the whole array, because (like I also said before) explode() created an array from a string. exploding an array is just silly. You explode the variable you actually want, which in this case is index 0 of the array:

 

$beruf = explode(",",$$_POST['beruf'][0]);

Link to comment
Share on other sites

if $_POST['beruf'] is an array, you can loop through the values:

 

foreach ($_POST['beruf'] as $k => $v){
$beruf = explode(",",$v);
  // do something with it before it gets overwritten by the next value
  // etc...
}

 

* please note there's a typo in my previous post.. $$ should only be $

Link to comment
Share on other sites

  • 2 weeks later...
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.