Jump to content

Require select and click delete button for twice to perform delete function


IreneLing

Recommended Posts

Hi all.

Here is my scripts which allow user to check multiple rows of data and delete it , but it require select data and click for twice to delete the rows , what should be the error?

 

<form name="frmSearch" method="post" action="insert-add.php">

<table width="600" border="1">
  <tr>
    <th width="50"> <div align="center">#</div></th>
    <th width="91"> <div align="center">ID </div></th>
    <th width="198"> <div align="center">First Name </div></th>
    <th width="198"> <div align="center">Last Name </div></th>
    <th width="250"> <div align="center">Mobile Company </div></th>
    <th width="100"> <div align="center">Cell </div></th>
    <th width="100"> <div align="center">Workphone </div></th>
    <th width="100"> <div align="center">Group </div></th>
  </tr>
    </form>
<?

    echo "<form name='form1' method='post' action=''>";

while($objResult = mysql_fetch_array($objQuery))
{


    echo "<tr>";
    echo "<td align='center'><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"$objResult[addedrec_ID]\"></td>";
    echo "<td>$objResult[addedrec_ID] </td>";
    echo "<td>$objResult[FirstName]</td>";
    echo "<td>$objResult[LastName] </td>";
    echo "<td>$objResult[MobileCompany] </td>";
    echo "<td>$objResult[Cell] </td>";
    echo "<td>$objResult[WorkPhone] </td>";
    echo "<td>$objResult[Custgroup] </td>";

   echo "</tr>";

}


echo "<td colspan='7' align='center'><input name=\"delete\" type=\"submit\" id=\"delete\" value=\"Delete\">";

if (isset($_POST['delete']) && isset($_POST['checkbox'])) // from button name="delete"
{
$checkbox = ($_POST['checkbox']); //from name="checkbox[]"
     $countCheck = count($_POST['checkbox']);

for($d=0;$d<$countCheck;$d++)
     {
         $del_id  = $checkbox[$d];

$sql = "DELETE from UserAddedRecord where addedrec_ID = $del_id";

$result2=mysql_query($sql)  or trigger_error(mysql_error());;;

     }
         if($result2)
     {  
          $fgmembersite->GetSelfScript();
         }
         else
         {
echo "Error: ".mysql_error();
         }
}
  echo "</form>";

 

Thanks for every reply.

Link to comment
Share on other sites

Thanks for your reply Drummin , actually there are 2 forms in this page , above is to insert data , and below is display and delete.

 

<form name="frmSearch" method="post" action="insert-add.php">
  <table width="599" border="1">

<tr>
<td>  Mobile Phone: </td>
<td>  <input name="cell" type="text" id="cell" > </td>
</tr>

<tr>
<td>Mobile Company:</td>
<td><select name="mobilecompany">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</td>
</tr>

<tr>
<td>  First Name: </td>
<td>  <input name="firstname" type="text" id="firstname" > </td>
</tr>

<tr>
<td>  Last Name: </td>
<td>  <input name="lastname" type="text" id="lastname" > </td>
</tr>

<tr>
<td>Work Phone:</td>
<td><input style="text" name="workphone"></td>
</tr>

<tr>
<td>Fax:</td>
<td><input style="text" name="fax"></td>
</tr>

<tr>
<td>Email:</td>
<td><input style="text" name="email"></td>
</tr>

<tr>
<td>Gender:</td>
<td><select name="gender">
<option>Male</option>
<option>Female</option>
</select>
</td>
</tr>

<tr>
<td>Birthday:</td>
<td>

<input name="birthday" value="<?php echo htmlentities($disp_birthday) ?>">
<input type=button value="select" onclick="displayDatePicker('birthday', this);">

</td>
</tr>



<tr>
<td>  ID: </td>
<td>  <input type="text" name="user_id" value="<?php echo $id;?>"></td>
</tr>


<tr>
<td><input type="submit" value="Add" name="add"></td>
</tr>


  </table>
</form>

 

Some other codes...then continue...

 

    <table width="600" border="1">
      <tr>
        <th width="50"> <div align="center">#</div></th>
        <th width="91"> <div align="center">ID </div></th>
        <th width="198"> <div align="center">First Name </div></th>
        <th width="198"> <div align="center">Last Name </div></th>
        <th width="250"> <div align="center">Mobile Company </div></th>
        <th width="100"> <div align="center">Cell </div></th>
        <th width="100"> <div align="center">Workphone </div></th>
        <th width="100"> <div align="center">Group </div></th>
      </tr>

    <?

echo "<form name='form1' method='post' action=''>";

    while($objResult = mysql_fetch_array($objQuery))
    {


echo "<tr>";
   echo "<td align='center'><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"$objResult[addedrec_ID]\"></td>";
   echo "<td>$objResult[addedrec_ID] </td>";
   echo "<td>$objResult[FirstName]</td>";
   echo "<td>$objResult[LastName] </td>";
   echo "<td>$objResult[MobileCompany] </td>";
   echo "<td>$objResult[Cell] </td>";
   echo "<td>$objResult[WorkPhone] </td>";
   echo "<td>$objResult[Custgroup] </td>";

echo "</tr>";

    }


echo "<td colspan='7' align='center'><input name=\"delete\" type=\"submit\" id=\"delete\" value=\"Delete\">";

if (isset($_POST['delete']) && isset($_POST['checkbox'])) // from button name="delete"
     {
$checkbox = ($_POST['checkbox']); //from name="checkbox[]"
         $countCheck = count($_POST['checkbox']);


for($d=0;$d<$countCheck;$d++)
         {
$del_id = intval($checkbox[$d]);

$sql = "DELETE from UserAddedRecord where addedrec_ID = $del_id";

$result2=mysql_query($sql)  or trigger_error(mysql_error());;;

         }
             if($result2)
         {    
              $fgmembersite->GetSelfScript();
             }
             else
             {
echo "Error: ".mysql_error();
             }
     }

echo "</form>";

    ?>
    </table>

 

 

 

Link to comment
Share on other sites

Check out this sample form.

<?php
//print_r($_POST);
if (isset($_POST['delete'])) // from button name="delete"
{
foreach($_POST as $key => $value){
IF ($key!="delete"){
//mysql_query("DELETE FROM UserAddedRecord WHERE addedrec_ID=$key");
echo "Item $key was removed<br />";
	}
}
}
?>
<html>
<head>
<title>Sample</title>
</head> 
<body> 
<form name='form1' method='post' action=''>
     <table width="600" border="1">
      <tr>
        <th width="50"> <div align="center">#</div></th>
        <th width="91"> <div align="center">ID </div></th>
        <th width="198"> <div align="center">First Name </div></th>
        <th width="198"> <div align="center">Last Name </div></th>
        <th width="250"> <div align="center">Mobile Company </div></th>
        <th width="100"> <div align="center">Cell </div></th>
        <th width="100"> <div align="center">Workphone </div></th>
        <th width="100"> <div align="center">Group </div></th>
      </tr>

    <?php
echo "<tr>";
   echo "<td align='center'><input name=\"43\" type=\"checkbox\" value=\"checkfordelete\" /></td>";
   echo "<td>43</td>";
   echo "<td>Jim</td>";
   echo "<td>Sample</td>";
   echo "<td>Sprint</td>";
   echo "<td>515-526-8956</td>";
   echo "<td>598-664-7895</td>";
   echo "<td>Custgroup</td>";
echo "</tr>";
echo "<tr>";
   echo "<td align='center'><input name=\"52\" type=\"checkbox\" value=\"checkfordelete\" /></td>";
   echo "<td>52</td>";
   echo "<td>Jim</td>";
   echo "<td>Sample</td>";
   echo "<td>Sprint</td>";
   echo "<td>515-526-8956</td>";
   echo "<td>598-664-7895</td>";
   echo "<td>Custgroup</td>";
echo "</tr>";
echo "<tr>";
   echo "<td align='center'><input name=\"22\" type=\"checkbox\" value=\"checkfordelete\" /></td>";
   echo "<td>22</td>";
   echo "<td>Jim</td>";
   echo "<td>Sample</td>";
   echo "<td>Sprint</td>";
   echo "<td>515-526-8956</td>";
   echo "<td>598-664-7895</td>";
   echo "<td>Custgroup</td>";
echo "</tr>";
echo "<tr>";
   echo "<td align='center'><input name=\"13\" type=\"checkbox\" value=\"checkfordelete\" /></td>";
   echo "<td>13</td>";
   echo "<td>Jim</td>";
   echo "<td>Sample</td>";
   echo "<td>Sprint</td>";
   echo "<td>515-526-8956</td>";
   echo "<td>598-664-7895</td>";
   echo "<td>Custgroup</td>";
echo "</tr>";
/*
    while($objResult = mysql_fetch_array($objQuery))
    {
echo "<tr>";
   echo "<td align='center'><input name=\"$objResult[addedrec_ID]\" type=\"checkbox\" value=\"checkfordelete\" /></td>";
   echo "<td>$objResult[addedrec_ID] </td>";
   echo "<td>$objResult[FirstName]</td>";
   echo "<td>$objResult[LastName] </td>";
   echo "<td>$objResult[MobileCompany] </td>";
   echo "<td>$objResult[Cell] </td>";
   echo "<td>$objResult[WorkPhone] </td>";
   echo "<td>$objResult[Custgroup] </td>";
echo "</tr>";
    } 
*/
echo "<tr>";
echo "<td colspan='8' align='center'><input name=\"delete\" type=\"submit\"  value=\"Delete\" /></td>";
echo "</tr>";
    ?>
    </table>
</form>
</body>
</html>

Link to comment
Share on other sites

Keep in mind that <form> tags should not go around <tr> or <td> tags.  Only around whole table or inside a cell (<td></td>).

Form names and id's need to be unique.  By using the record id as the name, the name will be unique and you can easily process a foreach statement as each posted value equals the $key.  Just be sure to filter out any other posted values like "submit", which is why I added the IF statement.

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.