cutielou22 Posted April 19, 2017 Share Posted April 19, 2017 (edited) I am working on a item helper script. It consists of users selecting from multiple radio buttons for multiple items and updating them all at once. My question is the code below is used for each item the user has (used in "while ($stmt->fetch()){" statement") . With the way I have it set up, how will I make it so it updates the correct item for the right option? Also. how do I grab the bracket variable use_item[$id] ? <tr width=\"100%\"> <td> <p>$name</p> </td> <td> <center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"keep\"$check_keep></center> </td> <td width=\"50\"> <center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"stock\"$check_stock></center> </td> <td width=\"50\"> <center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"discard\"$check_discard></center> </td> <td width=\"50\"> <center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"donate\"$check_donate></center> </td> <td width=\"50\"> <center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"gallery\"$check_gallery></center> </td> <td width=\"50\"> <center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"gallery3\"$check_gallery3></center> </td> <td width=\"50\"> <center><input name=\"use_item[$id]\" type=\"radio\" id=\"use_item[$id]\" value=\"deposit\"$check_deposit></center> </td> </tr> The form takes them to the page that updates using the information they choose from above. A small snippet below. if(isset($_POST['submit'])){ $use = $_POST['use_item']; foreach($use as $use_item){ $item = $_POST['item_id']; /////// UPDATING MYSQLI INSERTED HERE ////////////////// } } Thank you for your time. Hopefully I posted enough information for your help. Edited April 19, 2017 by cutielou22 Quote Link to comment Share on other sites More sharing options...
dkub Posted April 19, 2017 Share Posted April 19, 2017 What is $_POST['item_id']? I don't see that included in the form snippet above. What I do see, however, is an ID passed to use_item. $_POST['use_item'][3] would therefore be used to update the item with an ID of 3, no? See where I'm going with this? Quote Link to comment Share on other sites More sharing options...
cutielou22 Posted April 19, 2017 Author Share Posted April 19, 2017 $_POST['item_id'] is there - it is just not shown in the top snippet. <input type=\"hidden\" name=\"item_id\" value=\"$item_id\"> So for $_POST['use_item'][3] - the "3" is the variable in the [] brackets right? And this is how I will get the id. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 19, 2017 Share Posted April 19, 2017 "And this is how I will get the id." Ummm - You understand that you have to set that $id value before you echo out your input elements to have a value there? It doesn't just show up when you use your PHP code to retrieve the POST values. Just making sure you know that since I didn't see any line setting a value and assumed that it was happening earlier in the snippet. 1 Quote Link to comment Share on other sites More sharing options...
Strider64 Posted April 19, 2017 Share Posted April 19, 2017 (edited) I agree with ginerjm. I just wanted to add Radio Buttons can have only one value in the first place, so I don't even know why you are using an array? Unless all of the $id is going to be the same? An if that was the case there would be no need for an array for you are using Radio Buttons. <div class="radioBlock"> <input type="radio" id="radio1" name="reason" value="support" checked> <label class="radioStyle" for="radio1">support</label> <input type="radio" id="radio2" name="reason" value="advertise"> <label class="radioStyle" for="radio2">advertise</label> <input type="radio" id="radio3" name="reason" value="error"> <label class="radioStyle" for="radio3">Report a Bug</label> </div> However, I really don't understand what you are trying to do, so onward..... An to use an array you would in a form you wouldn't use $id, you would do something like: <input id="example" type="radio" name="use_item[id]" value=""> and this is how I retrieve it using php: $data = filter_input(INPUT_POST, 'use_item', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY); Edited April 19, 2017 by Strider64 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 19, 2017 Share Posted April 19, 2017 I don't like this code at all. A lot of bogus html and center tags and P tags inside td elements? WTF? As for the brackets on name attributes - as was said, if it's a radio element then only one can be selected for a given name, so why all these buttons using values from different sources all having the same name? That means only one can be selected, unless the bracket syntax allows multiples. I wouldn't know - I don't use radios that much. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 19, 2017 Share Posted April 19, 2017 The approach makes sense if there are in fact multiple items. But then the item IDs come from the array indexes, not this strange hidden field: foreach ($_POST['use_item'] as $item_id => $item_value) { // update $item_id with $item_value } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 19, 2017 Share Posted April 19, 2017 I agree Jacques that your sample makes sense, but the OP's use of an index as well as a disparate set of values from multiple folders is hard to follow. Normally a set of radio buttons pertains to a set of values that appear to be related. These don't, although they very well could be. Adding the index to the name's array nomenclature just confuses everything. I wonder if the OP has ever read up on how to use? She obviously has never used an html table properly. 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.