Jump to content

[SOLVED] multiple mysql row deletion via checkboxes


scarhand

Recommended Posts

OK I have this somewhat figured out after about an hour of googling.

 

So what I have right now is this:

 

<form method="post">

<?php
$showshoutsql = mysql_query("SELECT * FROM shouts ORDER BY $sortshout $ordershout");
$showshoutcount = mysql_num_rows($showshoutsql);

if (isset($_POST['removeselected']))
{
  for ($i = 0; $i < $showshoutcount; $i++)
  {
    $chex = $_POST['chex'][$i];
    echo "$chex <br />";
  }
}

while ($row = mysql_fetch_array($showshoutsql)) 
{
  echo "<input type=\"checkbox\" value=\"$id\" name=\"chex[]\"> \n";
}
?>

<br />

<input name="removeselected" type="submit" value="Remove Selected">

</form>

 

Now I know that THIS works. However, I do not want the checkbox's names to be "chex[]", I only want them to be "chex", otherwise they won't work with my Javascript code.

 

The problem is that when I remove the "[]", when $chex is echo'd it does not show the VALUE of each checkbox.

 

Any help would be greatly appreciated as I've tried multiple different things and none seem to work.

IMO i think this is javascript problem.. (if you can use an array in your javascript) not a PHP one, sure we can suggest "workarounds" but why not just fix the fault !

 

im sure its a simple way of turning the name "chex" into an array before it is echo'd

 

i mean if i go ahead and just make chex an array once its pulled from the database then that means i have to re-write all the javascript code

I agree. Modify the javascript to work with the array. It is the best way of passing all of the values of the IDs.

 

If its really not an option, you'd have to employ some more javascript to do something like populate a hidden field with a list of IDs, separated by commas, which you could then separate out in php. Better method is to modify the javascript though.

IMO i think this is javascript problem.. (if you can use an array in your javascript) not a PHP one, sure we can suggest "workarounds" but why not just fix the fault !

 

im sure its a simple way of turning the name "chex" into an array before it is echo'd

 

i mean if i go ahead and just make chex an array once its pulled from the database then that means i have to re-write all the javascript code

 

You miss the point. Unless chex is an array, then you will only ever recieve the last value from your $_POST array. A string can only hold one value - you need to pass lots of related values, for which an array is best.

IMO i think this is javascript problem.. (if you can use an array in your javascript) not a PHP one, sure we can suggest "workarounds" but why not just fix the fault !

 

im sure its a simple way of turning the name "chex" into an array before it is echo'd

 

i mean if i go ahead and just make chex an array once its pulled from the database then that means i have to re-write all the javascript code

 

You miss the point. Unless chex is an array, then you will only ever recieve the last value from your $_POST array. A string can only hold one value - you need to pass lots of related values, for which an array is best.

 

that makes sense as to why using foreach was getting me nowhere.

 

i am rewriting the javascript. thanks guys.

as an idea..

 

why not

 

<form method="post">

<?php
$showshoutsql = mysql_query("SELECT * FROM shouts ORDER BY $sortshout $ordershout");
$showshoutcount = mysql_num_rows($showshoutsql);

if (isset($_POST['removeselected']))
{
  for ($i = 0; $i < $showshoutcount; $i++)
  {
    $chex = $_POST['chex$i'];
    echo "$chex <br />";
  }
}

$n=0;
while ($row = mysql_fetch_array($showshoutsql)) 
{
  echo "<input type=\"checkbox\" value=\"$id\" name=\"chex$n\"> \n";
$n++;
}
?>

<br />

<input name="removeselected" type="submit" value="Remove Selected">

</form>

 

EDIT: WooHoo re-writing the JS is much better,

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.