IreneLing Posted October 20, 2011 Share Posted October 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249483-require-select-and-click-delete-button-for-twice-to-perform-delete-function/ Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 Continue putting your form together adding the checkboxes and then we'll look at it. Also missing your </table> tag. So fix things up and we'll help with the next step. Quote Link to comment https://forums.phpfreaks.com/topic/249483-require-select-and-click-delete-button-for-twice-to-perform-delete-function/#findComment-1280936 Share on other sites More sharing options...
IreneLing Posted October 20, 2011 Author Share Posted October 20, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249483-require-select-and-click-delete-button-for-twice-to-perform-delete-function/#findComment-1280939 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249483-require-select-and-click-delete-button-for-twice-to-perform-delete-function/#findComment-1280977 Share on other sites More sharing options...
Drummin Posted October 20, 2011 Share Posted October 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249483-require-select-and-click-delete-button-for-twice-to-perform-delete-function/#findComment-1280985 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.