problem with implode()

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
drace
Forum Newbie
Posts: 3
Joined: Sun Feb 17, 2008 4:59 pm

problem with implode()

Post by drace »

Hi there,

i've got a very simple database that is displayed in a html table. each entry in the table has a unique id which is the primary key, each row in the table has a checkbox.

This is part of the code that loads data into the table (php while loop, $ID is ID of entry)

Code: Select all

echo "<tr> <td> <input type=checkbox name='check[]' value='$ID'> </td>";
Once the user has made his selection the editform is called up in a separate window using this javascript function:

Code: Select all

function OpenWindow()
{
  window.open(editform.php,'mywindow','width=400,height=300');
  header("Location: http://localhost/");
}
editform.php then proceeds to get the array with the value of the checkboxes like this:

Code: Select all

$check = $_REQUEST['check'];
$idlist = implode(",", $check);
which is supposed to give me a variable containing the id of the entry the user has selected via the checkbox.

This does not work. this is the error message i am getting:

Warning: implode() [function.implode]: Invalid arguments passed in C:\xampp\htdocs\editform.php on line 33

i am a bit stuck, is there an alternate way to pass the id to the editform, or a way to get the implode function working in this context? any help would be greatly appreciated.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: problem with implode()

Post by Benjamin »

What is the contents of $check?
drace
Forum Newbie
Posts: 3
Joined: Sun Feb 17, 2008 4:59 pm

Re: problem with implode()

Post by drace »

hi,

Code: Select all

echo("check: $check");
gives me this:

check:

thus i would say $check is empty
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: problem with implode()

Post by Benjamin »

Can't really implode an empty string heh.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: problem with implode()

Post by Luke »

Implode expects an array (second arg) and returns a string. If it doesn't get an array, it throws an error. Try something like:

Code: Select all

$check = isset($_POST['check']) ? $_POST['check'] : array();
$idlist = implode(",", $check);
Checkboxes work a little strange... if you don't check it, it isn't included in the $_POST array at all. So you have to check that the value exists before attempting to use it.
drace
Forum Newbie
Posts: 3
Joined: Sun Feb 17, 2008 4:59 pm

Re: problem with implode()

Post by drace »

thats great, ill give that a try. thanks for your help
Post Reply