TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Multiple Checkbox problems (http://www.talkphp.com/general/1959-multiple-checkbox-problems.html)

riscphree 01-15-2008 03:27 AM

Multiple Checkbox problems
 
What I'm trying to do is delete items in a mysql database that are displayed with a simple checkbox. Here is the form that is generated.

Code:

<form action="" method="post">

sql query here

print"<input type="checkbox" class="delete-checkbox" value="{$row['id']}" name="deletebox[]" />";

<input type="submit" name="submit" value="delete messages" />
</form>

Here is the php that I'm using when its POSTED

Code:

if(isset($_POST['submit'])) {
foreach($_POST['deletebox'] as $deletebox1){
mysql_query("DELETE FROM private_messages WHERE id='$deletebox1'") or die(mysql_error());
}
}

However, nothing happens, which seems to be something with the array, but I can't figure it out. I've tried putting something to print out what is in the array that is passed, but it never prints anything out, like its blank. And I'm positive there are values in the deletebox[] value when you check it.

Wildhoney 01-15-2008 03:36 AM

That code actually works perfectly for me. This is what I have:

php Code:
<?php

if(isset($_POST['submit']))
{
    foreach($_POST['deletebox'] as $deletebox1)
    {
        $deletebox1 = (int) $deletebox1;
        echo "DELETE FROM private_messages WHERE id=$deletebox1" . '<br />';
    }
}

?>

And the HTML...

html4strict Code:
<form action="" method="post">

    <input type="checkbox" class="delete-checkbox" value="1" name="deletebox[]" />
    <input type="submit" name="submit" value="delete messages" />

</form>

I can really only surmise that either the $_POST array is being overwritten, or that for whatever reason there are no items being posted, but I'm not sure why that would be. Is your code getting to that segment where you execute the SQL statements?

riscphree 01-15-2008 03:56 AM

Man, I've been going over this for hours .. still not working and I see absolutely nothing wrong. I'm pretty much to the point where I'll pay someone to fix this, it's getting out of hand.

Wildhoney 01-15-2008 04:01 AM

I feel for you! I've been in similar situations where things defy all reason. Sadly though I'm off to bed any time soon ;-) Maybe you can post us a bit more of the code - considering it isn't too long. Or maybe a pastebin otherwise.

Alan @ CIT 01-15-2008 08:36 AM

When the html is loaded, can you view source and see if $row['id'] is filling in the blank correctly in your checkbox form field?

Alan

riscphree 01-15-2008 02:16 PM

Quote:

When the html is loaded, can you view source and see if $row['id'] is filling in the blank correctly in your checkbox form field?
Yeah, it is being loaded.

xenon 01-15-2008 06:11 PM

Please, oh please, turn error reporting on (error_reporting(E_ALL)) when in debug mode. The problem is in the printing business:

PHP Code:

print"<input type="checkbox" class="delete-checkbox" value="{$row['id']}" name="deletebox[]" />"

Why aren't the double quotes inside the print statement escaped or smth? Like so...

PHP Code:

print '<input type="checkbox" class="delete-checkbox" value="' $row['id'] . '" name="deletebox[]" />'


riscphree 01-15-2008 06:30 PM

Quote:

The problem is in the printing business:
Because my real code is:

Code:

print "<div class=\"delete\"><input type=\"checkbox\" class=\"delete-checkbox\" value=\"{$row['id']}\" name=\"deletebox\" /></div>";
I just took out the escapes so it was easier to read.

Nor 01-15-2008 07:54 PM

They keep telling you:

Code:

print "<div class=\"delete\"><input type=\"checkbox\" class=\"delete-checkbox\" value=\"{$row['id']}\" name=\"deletebox\" /></div>";
should be:
Code:

print "<div class=\"delete\"><input type=\"checkbox\" class=\"delete-checkbox\" value=\"{$row['id']}\" name=\"deletebox[]\" /></div>";
You might not notice but the name of the delete box has the [] after the work deletebox now ;)...

riscphree 01-16-2008 07:40 PM

I've got it at least to recognize the number of items being submitted, but it doesn't pass the values still. when I checked three boxes it ouput:

Code:

Array
(
    [deletebox] => Array
        (
            [0] =>
            [1] =>
            [2] =>
        )

    [submit] => delete messages
)


EyeDentify 01-17-2008 08:33 AM

Are you submiting via a Ajax JavaScript POST call or GET .

Cause i have had trouble with values donīt being transfered in some cases because of the form element being laid out with the help of Table elements.

Canīt remebmer right now wich Framework i used but it was:
jQuery, Prototype, MooTools, or someone like that.

Just a thought.

/EyeDentify

riscphree 01-17-2008 01:42 PM

Nope, just doing a regular form submission.

Salathe 01-17-2008 02:27 PM

Try using var_dump rather than print_r because the former will be able to show us the types of the values (integer, string, etc) as well as their actual value (e.g, FALSE does not display anything with print_r).

Nor 01-17-2008 02:54 PM

Try echoing the value out to the page and see if there is an id being shown to the browser.

riscphree 01-17-2008 06:17 PM

using var_dump
Code:

var_dump($_POST)
and checked three boxes (IDs of 37, 27, and 21)

Gives me:

Code:

array(2) { ["deletebox"]=>  string(0) "" ["submit"]=>  string(15) "delete messages" }

riscphree 01-17-2008 06:18 PM

Quote:

Try echoing the value out to the page and see if there is an id being shown to the browser.
In the links, the IDs are properly shown.


All times are GMT. The time now is 06:21 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0