Jump to content

Execute by dropdown


SkyRanger

Recommended Posts

Not sure if this is possible I am trying to do a multi delete:

 


<form name="form1" method="post" action="">
<table>
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>


// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
<tr><td>
<select name="dropdown">
<option value="option1">Choose an action...</option>
<option value="edit">Edit</option>
<option value="delete">Delete</option>
</select>
<a class="button" href="#">Apply to selected</a>


if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=nameslist.php\">";
}
}
mysql_close();

Link to comment
Share on other sites

While loops are always fun, but for what you wish to achieve, you want to look at another member in the loop family. The 'foreach' loop. This will allow for each individual id in your array to be stored in a single variable. See below example

 

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

   

    $theId = $row['id'];

    $theFname = $row['name'];

    $theLname = $row['lastname'];

    $theEmail = $row['email'];

   

 

          $deleteThese .= '<input name="contacts[]" type="checkbox" value="' . $theId . '" /> ' . $theFname . ' ' . $theLname . '<br />' . $theEmail . '<br /><br />';

 

}

 

Now we want to give a rule for our checkbox function. We want to capture whichever checkbox was ticked, and do something to that particular piece of data in the database. We know which piece of data in the database we will be working with because of our variable $theID declared in each input checkbox. See below how we make the foreach function work for us here

 

if(isset($_POST['delete_contacts']) && !empty($_POST['contacts'])) {

 

I always like to run the above if statement to check whether or not our submit button was pressed, and to ensure that there was actually some piece of information checked on our form before we move forward.

 

foreach($_POST['contacts'] as $allchecked) {

 

Now we do a foreach function. This will grab the value of each checked box on our form. Remember, the value of each checkbox is the current ID that we are working with. This is how we will tell our database what and where to delete.

 

        $query = "SELECT id FROM your_table_name WHERE id='$allchecked";

$sql = mysql_query($query) or die (mysql_error());

     

Here I like to do one more check to make sure our database found someone with the same ID as the checkbox value.

 

$existCount = mysql_num_rows($sql);

if($existCount > 0) {

 

After our count of the rows, I do the final query to the database, which is find the contact in the database, and delete that person from the database.

 

                $lastQuery = "DELETE FROM your_table_name WHERE id='$allchecked';

$sql2 = mysql_query($lastQuery);

 

Overall, the code will be the following:

 

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

   

    $theId = $row['id'];

    $theFname = $row['name'];

    $theLname = $row['lastname'];

    $theEmail = $row['email'];

   

 

          $deleteThese .= '<input name="contacts[]" type="checkbox" value="' . $theId . '" /> ' . $theFname . ' ' . $theLname . '<br />' . $theEmail . '<br /><br />';

 

}

 

if(isset($_POST['delete_contacts']) && !empty($_POST['contacts'])) {

    foreach($_POST['contacts'] as $allchecked) {

 

            $query = "SELECT id FROM your_table_name WHERE id='$allchecked";

    $sql = mysql_query($query) or die (mysql_error());

 

    $existCount = mysql_num_rows($sql);

            if($existCount > 0) {

                        $lastQuery = "DELETE FROM your_table_name WHERE id='$allchecked';

        $sql2 = mysql_query($lastQuery);

}

}

}

 

echo 'anything you want here and be sure to exit when you are done.';

 

    exit();

                         

 

The above is the simple PHP. Wherever you wish to display the list of options that will be deleted, echo this function in the HTML. See below

 

<?php echo "$deleteThese"; ?>

 

This will give you your list of checkboxes and names with emails listed below. Also, don't forget to add your form and button somewhere on the html.

 

<form action="some_page.php" method="post" enctype="multipart/form-data">

 

<input type="submit" name="delete_contacts" value"Delete Contacts" /> (NOTE: we establish the 'name' of this button here and it has to be the same name in the php where you are checking to see if this button was clicked)

 

</form>

 

 

And you are done. The code you are using is not necessary as far as I know. The above is all you need to capture the data from your database and remove it if its been checked. Hope this works, good luck!!

 

Link to comment
Share on other sites

P.S. I just realized you want to tell the code to either edit or delete a group....I would handle this on a different page. Ideally you will want your users to be able to delete from blah.php and edit from somewhereelse.php

 

Regarding editing, what exactly are the users editing? The user data? Some name and email data? Depending on what you are editing, I can assist with a script that will allow you to edit multiple rows of user information in one form.

Link to comment
Share on other sites

The select box is suppose to be above.

 

This code is outside of the form

 

// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

Link to comment
Share on other sites

Inside the form, outside the form is not the problem.  Since technically, everything in PHP is outside of the HTML scope.  As it is separate from HTML, and executed before the browser even thinks of displaying the markup.  However, the problem does lye in the fact that I don't see any PHP tags around that bit of code.  Which means that code is passed to the browser, and never parsed by PHP.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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