rilana Posted June 28, 2011 Share Posted June 28, 2011 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 Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 28, 2011 Share Posted June 28, 2011 remove the single quotes <input type="checkbox" name="beruf[]" value="4,9,10,11"/> value grabbed in php will be: 4,9,10,11 now you can use explode() to separate them if necessary. Quote Link to comment Share on other sites More sharing options...
rilana Posted June 28, 2011 Author Share Posted June 28, 2011 I thaught I tryed that, but I will try again. Quote Link to comment Share on other sites More sharing options...
rilana Posted June 28, 2011 Author Share Posted June 28, 2011 No does not work. Output now is INSERT INTO newsletter_beruf (userID, berufID) VALUES (163, 4,9,10,11) Quote Link to comment Share on other sites More sharing options...
rilana Posted June 28, 2011 Author Share Posted June 28, 2011 could it be possible that I need to change this? $beruf = strip_tags($_POST['beruf']); Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 28, 2011 Share Posted June 28, 2011 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']); Quote Link to comment Share on other sites More sharing options...
rilana Posted June 28, 2011 Author Share Posted June 28, 2011 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? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 28, 2011 Share Posted June 28, 2011 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); } Quote Link to comment Share on other sites More sharing options...
rilana Posted June 30, 2011 Author Share Posted June 30, 2011 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? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 30, 2011 Share Posted June 30, 2011 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>'; Quote Link to comment Share on other sites More sharing options...
rilana Posted July 1, 2011 Author Share Posted July 1, 2011 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 ) Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 1, 2011 Share Posted July 1, 2011 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]); Quote Link to comment Share on other sites More sharing options...
rilana Posted July 1, 2011 Author Share Posted July 1, 2011 OK, wow this actualy works and now I get what you where trying to tell me! But how to I explode [0][1][2]... without over writing $beruf ? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 1, 2011 Share Posted July 1, 2011 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 $ Quote Link to comment Share on other sites More sharing options...
hannan Posted July 1, 2011 Share Posted July 1, 2011 Can you post the code... i want this but donno the codin part.. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 1, 2011 Share Posted July 1, 2011 Sorry, but there's a big difference in helping solve the problem and actually doing the work for you. If you need someone to code it for you, I'm sure you can hire a professional freelancer or something. Quote Link to comment Share on other sites More sharing options...
rilana Posted July 15, 2011 Author Share Posted July 15, 2011 Thank you so much! I finnaly got it working with your second last post. It took me a while to respond because I am on vacation now and dont allways have a proper connection :-) Thank you a lot! Rilana Quote Link to comment 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.