implode function not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tonyledenko
Forum Newbie
Posts: 15
Joined: Thu Sep 11, 2008 4:22 pm

implode function not working

Post by tonyledenko »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello,

I am trying to combine multiple values in a string to be submitted to a shopping cart. However, I can only get one of the two values to pass through to the cart. Any suggestions? Thanks in advance!

Code: Select all

<?php
if(isset($_POST['part'])) {
$checkbox = $_POST['part'];
$string = implode("^",$checkbox);
echo $string;
}
?>

Code: Select all

<form name='CartItem' action='https://www.ram-mount.com/RamCart/CartPutItem.php' method='POST'>
<input type='checkbox' name='part' value='RAM-VB-162'>
<input type='checkbox' name='part' value='RAM-VB-154'>
<input type='submit' value='Add To Cart'>
</form>

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: implode function not working

Post by Kadanis »

If I recall correctly you need to put square brackets on the name part of the form to specify an array

Code: Select all

 
<form name='CartItem' action='https://www.ram-mount.com/RamCart/CartPutItem.php' method='POST'>
<input type='checkbox' name='part[]' value='RAM-VB-162'>
<input type='checkbox' name='part[]' value='RAM-VB-154'>
<input type='submit' value='Add To Cart'>
</form>
 
tonyledenko
Forum Newbie
Posts: 15
Joined: Thu Sep 11, 2008 4:22 pm

Re: implode function not working

Post by tonyledenko »

I tried your suggestion, and no values pass now. Any other suggestions?

<?php
if(isset($_POST['part'])) {
$checkbox = $_POST['part'];
$string = implode("^",$checkbox);
echo $string;
}
?>

<form name='CartItem' action='https://www.ram-mount.com/RamCart/CartPutItem.php' method='POST'>
<input type='checkbox' name='part[]' value='RAM-VB-162'>
<input type='checkbox' name='part[]' value='RAM-VB-154'>
<input type='submit' value='Add To Cart'>
</form>
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: implode function not working

Post by Kadanis »

I'm not sure what the rest of your processing script does so can't really help, but when i tested this snippet on our dev server it worked fine and returned the following from a var dump

Code: Select all

 
array(1) { 
    ["part"]=>  array(2) { 
        [0]=>  string(10) "RAM-VB-162" 
        [1]=>  string(10) "RAM-VB-154" 
    } 
} 
 
RAM-VB-162^RAM-VB-154 
 
This is the snippet I tested, called test.php

Code: Select all

 
<?php
var_dump($_POST);
 
if(isset($_POST['part'])) {
$checkbox = $_POST['part'];
$string = implode("^",$checkbox);
echo $string;
}
?>

Code: Select all

 
<form name='CartItem' action='test.php' method='POST'>
<input type='checkbox' name='part[]' value='RAM-VB-162'>
<input type='checkbox' name='part[]' value='RAM-VB-154'>
<input type='submit' value='Add To Cart'>
</form>
 
tonyledenko
Forum Newbie
Posts: 15
Joined: Thu Sep 11, 2008 4:22 pm

Re: implode function not working

Post by tonyledenko »

When I submit the following it works. Just wondering why its not working with PHP. Thanks anyway.

<form name='CartItem' action='https://www.ram-mount.com/RamCart/CartPutItem.php' method='POST'>
<input type='hidden' name='part' value='RAM-VB-162^RAM-VB-154'>
<input type='submit' value='Add To Cart'>
</form>
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: implode function not working

Post by Kadanis »

Sounds like there could be something else going on, as the suggested code should work in a normal HTTP / PHP environment. Weird.

Good Luck anyway.
Post Reply