Jump to content

Grab data from multiple checkboxes and multiple dropdowns


tsm1248
 Share

Recommended Posts

Ok so i want to grab an id from the checkbox then grab the option drop down associated with that check box and update a mysql row here is my code so far

 

any help is awesome help taaaanks guys ;)

 

<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("db1") or die(mysql_error());
$query = "SELECT * FROM tickets ORDER BY id DESC";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{

print "<input name='delete[]' value='{$row['id']}' type='checkbox'>";
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("db1") or die(mysql_error());
$query2 = "SELECT * FROM admin";
$result2 = mysql_query($query2) or die(mysql_error());
print "<form name='namestoupdate' method='post' action='update.php'>\n";
print '<select>';
while($row2 = mysql_fetch_array($result2))
{
if ($row2['priv']==prov){print '<option value="'.$row2['user'].'" name="prov['.$i++.']">'.$row2['user'].'</option>';}
}
print '</select>';
print "<input type='submit' value='submit' />";
print "</form>";
}
?>

 

Visual aid

sxho5d.jpg

Link to comment
Share on other sites

You don't tie in your checkbox with its dropdown list

if ($row2['priv']==prov){print '<option value="'.$row2['user'].'" name="prov['.$i++.']">'.$row2['user'].'</option>';}
}

 

change to:

 

if ($row2['priv']==prov){print '<option value="'.$row2['user'].'" name="prov['.$row['id'].']">'.$row2['user'].'</option>';}
}

 

checkbox returns a value only when checked

listbox returns 1 item which is selected

so the key for the listbox can be the value from the checkbox, so know you know which checkbox belongs to which list item

 

 

this creates a mult-dimensional array

in your code check yer checkboxes

$delete[]=array();
if(isset($_POST['delete']))
{
    foreach($_POST['delete'] as $ticket)
       $delete[]=$_POST['prov'][$ticket];
}
echo count($delete) . " tickets to remove <br />";
for($i=0;$i<count($delete);$i++)
{
  $n=$i+1;
  echo " #{$n} - {$delete[$i]} <br />";
}

Link to comment
Share on other sites

First off thanks for responding i really need some help on this one a bit stuck

 

test1.php

<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("db1") or die(mysql_error());
$query = "SELECT * FROM tickets ORDER BY id DESC";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{

print "<input name='delete[]' value='{$row['id']}' type='checkbox'>";
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("db1") or die(mysql_error());
$query2 = "SELECT * FROM admin";
$result2 = mysql_query($query2) or die(mysql_error());
print "<form name='namestoupdate' method='post' action='test.php'>\n";
print '<select>';
while($row2 = mysql_fetch_array($result2))
{
if ($row2['priv']==prov){print '<option value="'.$row2['user'].'" name="prov['.$row['id'].']">'.$row2['user'].'</option>';}
}
print '</select>';
print "<input type='submit' value='submit' />";
print "</form>";
}
?>

 

test.php

 

<?php
$delete[]=array();
if(isset($_POST['delete'])){    
foreach($_POST['delete'] as $ticket)      
$delete[]=$_POST['prov'][$ticket];
}
echo count($delete) . " tickets to remove <br />";
for($i=0;$i<count($delete);$i++){  $n=$i+1;  
echo " #{$n} - {$delete[$i]} <br />";
}
?>

 

This is what the code now looks like and outputs the following regardless if a checkbox is checked and its the same output each time

 

output

 

1 tickets to remove

#1 - Array

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

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