SirChick Posted November 8, 2007 Share Posted November 8, 2007 I have a delete button in a while loop which echos next to each message per row but how do i make them assigned to the message ID so depending on which submit button is pressed the correct id is deleted... this is what i have... i also must add the the check box must be unique aswell because the user has to tick the checkbox before pressing delete but at the moment there all the same name so its not going to work in the process validation: echo '<tr> <td width="200"><center><font style="font-size:14px" color="#FFFFFF" face="Arial">From: <font color="#32CD32">'; Echo $UserName; Echo'</font></br><font color="#EE9A4D">'; Echo $Staff; Echo'</font>'; Echo'</br><font color="#FFFFFF">Sent On: </font><font color="#32CD32">'; Echo $SentOn; Echo'</font></br></center></font></td> <td width="200"><center><font style="font-size:14px" color="#FFFFFF" face="Arial">Subject: </font></br><font color="#32CD32">'; Echo $Subject; Echo'</br><hr size="2" width="40" id="Line2" style="width:200px;height:2px;z-index:0">'; Echo'</font>'; Echo'<font color="#FFFFFF">Message: </font><font color="#32CD32"></br><b>'; Echo $Message; Echo'</font></b></center></font></td>'; Echo' <td background="menu_gradient.gif" bgcolor="#FFFFFF" style="background-repeat: repeat-x;"><center><font style="font-size:14px" color="#FFFFFF" face="Arial">'; Echo'<a href="reportext.php?report=';Echo$MessageID;Echo'">Report</a></br></br>'; Echo'<input type="checkbox" id="Checkbox1"" name="Checkbox1" value=""> <input type="submit" id="Button1"" name="Button1" value="Delete">'; Echo'</center></td></tr>'; what should I do ? Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 8, 2007 Share Posted November 8, 2007 You would either need to wrap the submit button inside <form> tags and then have the ID to delete in the url the form submits to, or in a hidden field within that form. Alternatively, you can use javascript onClick to forward to a specific url containing the ID to delete. This isn't really a php problem, maybe someone can move it to an HTML related forum. Quote Link to comment Share on other sites More sharing options...
SirChick Posted November 8, 2007 Author Share Posted November 8, 2007 it is in form tags ... im only showing u the layout because i dont see why u'd need to see the variable assignments, but are u referring to $_GET ? i was hoping to put $messageID as the submit name but then realised the form process would have to have tons of checks per id which isnt logical. i can show u the whole thing if it is needed ? Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 8, 2007 Share Posted November 8, 2007 My bad, sorry. You want it so that the user has check the box, then delete the record? Can they check multiple boxes and delete them all with one button? If they can only check one box, and hit the delete button, they should probably be radio buttons instead of check boxes so only one can be selected at once. Otherwise, the only real way to submit a specific value to the server with a submit button is by its name. You could have your radio buttons/check boxes contain the ID of the record as the value, then each button's name would be something like delete-XX where XX is the ID that of the record as well. Then on the back end, you would find the ID of the selected radio button or check box, and see if a POST value with a key like 'delete-XX' is present. There really isn't an easy way around the issue the way you are doing it. Quote Link to comment Share on other sites More sharing options...
SirChick Posted November 8, 2007 Author Share Posted November 8, 2007 well the user can delete more than one yes... so many check boxes can be clicked and there is global "delete select" button which is outside the while loop but for individual deleting i wanted the delete button on each row..and that button would represent which one to delete. but if i have a while loop the submit buttons is always going to have the same name.. so how do i unique them... is there an easier way to do this ? Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 8, 2007 Share Posted November 8, 2007 The same way you added the $MessageID; to the Report link. <input type="submit" id="Button1"" name="delete-<?php echo $MessageID; ?>" value="Delete"> Quote Link to comment Share on other sites More sharing options...
SirChick Posted November 8, 2007 Author Share Posted November 8, 2007 oh i get you thanks how does the form process obtain the value ? Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 8, 2007 Share Posted November 8, 2007 That's the weird thing about it. Check the $_POST (using var_dump() or print_r())array after submitting the form. It'll show you all the values it contains and how the data is structured. You will most likely have to loop through each value in the $_POST array, check if the key begins with 'delete-', and extract the ID number from the string. Once you have the ID, you can then check to see if the ID is present in the array of checked boxes. Quote Link to comment Share on other sites More sharing options...
SirChick Posted November 8, 2007 Author Share Posted November 8, 2007 oh my lord that sounds confusing already.. =/ Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 8, 2007 Share Posted November 8, 2007 It's a bit confusing because its kind of an unconventional way of doing things. Anyways, here's something to get you started. Paste the code in to a regular php file and run it. Then you can play around with it and see where you get. The var_dump() in the script shows you exactly what gets sent to the server every time you hit a button. <form action="<?php echo $PHP_SELF; ?>" method="post"> <?php for($i = 1; $i <= 10; $i++): ?> <div> Record #<?php echo $i; ?> <input type="checkbox" id="Checkbox1" name="delete_confirm[]" value="<?php echo $i; ?>"> <input type="submit" id="delete" name="delete-<?php echo $i; ?>" value="Delete"> </div> <?php endfor; ?> </form> <br /><br /> <?php if ($_POST) { var_dump($_POST); foreach ($_POST as $key => $value) { if (preg_match('/^delete-([0-9]+)$/', $key, $matches)) { $delete_id = $matches[1]; break; } } // check if the checkbox for $delete_id was checked if (is_array($_POST['delete_confirm'])) { if (in_array($delete_id, $_POST['delete_confirm'])) { echo "Deleting record ID {$delete_id}!"; } else { echo "Check box for record ID {$delete_id} was not checked!"; } } else { echo "No check boxes checked!"; } } ?> I used a regex match to do 2 things at once (check if the key begins with 'delete-' and extract the ID if it does). How you check if it begins with 'delete-' and extract the ID can be done differently using regular string manipulation functions. Those may be simpler, but a preg_match with an appropriate regex pattern will do it all at once. Quote Link to comment Share on other sites More sharing options...
SirChick Posted November 8, 2007 Author Share Posted November 8, 2007 so could i do <input type="submit" id="Button1"" name="delete-<?php echo $_SESSION['MessageID']; ?>" value="Delete"> then $MessageID = $_POST['$_SESSION['MessageID']; in the process cos that would make life easier ? Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 9, 2007 Share Posted November 9, 2007 I guess, in theory that could work. I can't say for sure as I don't know what the rest of your code looks like. It would actually be this though: $MessageID = $_POST['delete-'.$_SESSION['MessageID']; You have to make sure you keep the 'delete-' part of the key there. Quote Link to comment 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.