DeX Posted May 28, 2010 Share Posted May 28, 2010 Simple question, I know. I have a form that's built dynamically like so: foreach ($all_rows as $column){ print ("<tr>"); print ("<td>". $column['id'] . "</td>"); print ("<td>". $column['name'] . "</td>"); print ("<td><input type='text' name='".$column['id']."' id='".$column['id']."' value='".$column['pricePerPiece']."'/></td>"); print ("<td><input type='text' name='weight' id='weight' value='".$column['weight']."'/></td>"); print ("<td>". $column['sku'] . "</td>"); print ("</tr>"); } I left out some of the table structure but the page works fine, I know how to do that part. So I have the input field for pricePerPiece in that column and all the values are loaded from the database. I need to get those values and update the database if the user changes any of them. So what I'm doing right now is I set the name/ID of each input box to be the database ID of that item and I'll update the database where the ID equals that value. The problem is when I submit it, how do I get all the input values and then loop through them to update the database with each one? I've attached a screenshot of the page. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/203218-how-do-i-get-all-the-text-input-values-from-a-form/ Share on other sites More sharing options...
.Stealth Posted May 28, 2010 Share Posted May 28, 2010 You don't need to loop through them, just do it all in one query. You get the values either from $_GET or $_POST depending on what method your form is using. So you would use: $column = $_POST['column_id']; Where column_id is the column name set by this bit: name='".$column['id']."' Then run a sql UPDATE query. Quote Link to comment https://forums.phpfreaks.com/topic/203218-how-do-i-get-all-the-text-input-values-from-a-form/#findComment-1064756 Share on other sites More sharing options...
ChemicalBliss Posted May 28, 2010 Share Posted May 28, 2010 To loop through something the easiest way is to A) Know the number of items to loop, and B) Have all the items in an array, or varaibles naming scheme that will always be the same. C) Have all the information needed in a logical way. eg: Each "name" attribute of the input elements on your HTML form should have a generic name (eg form1), a Row ID, and the Column name it belongs to, eg; print ("<input type='text' name='form1[".$column['id']."][pricePerPiece]' id='".$column['id']."' value='".$column['pricePerPiece']."'/> <input type='hidden' name='form1[".$column['id']."' value='"."' />"); // Outputs example: <input type='text' name='form1[2][pricePerPiece]' id='2' value='4.99'/> On the receiving end you would do something like: $form = $_POST['form1']; // Santize $form, make sure its an array etc.. // Loop array foreach($form as $rowid => $columnarray){ // set query start $query = "UPDATE table SET "; // Loop each column for this row foreach($columnarray as $column => $value){ // Add set phrase to query $query .= $column."='".$value."' "; } // End query: $query .= "WHERE id='".$rowid."'"; // debug echo query echo($query); } This method allows you to display all your rows and update columns dynamically without having to know the column names or row ids on the processing page. You need to make sure you sanitize and secure this properly, as it could potentialy let users modify your database in an irrecoverable way. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/203218-how-do-i-get-all-the-text-input-values-from-a-form/#findComment-1064774 Share on other sites More sharing options...
DeX Posted May 28, 2010 Author Share Posted May 28, 2010 Thanks, that worked beautifully! You guys are quick. Quote Link to comment https://forums.phpfreaks.com/topic/203218-how-do-i-get-all-the-text-input-values-from-a-form/#findComment-1064787 Share on other sites More sharing options...
DeX Posted May 28, 2010 Author Share Posted May 28, 2010 Actually I spoke too soon. I tried it for a form with multiple input columns and get this error on the page: MySQL Query fail: UPDATE miscellaneous SET materialsPrice='' labourPrice='' WHERE id='3' You can see it's missing the comma between the assignments, otherwise it works fine. How do I display the comma in there? Quote Link to comment https://forums.phpfreaks.com/topic/203218-how-do-i-get-all-the-text-input-values-from-a-form/#findComment-1064812 Share on other sites More sharing options...
DeX Posted May 28, 2010 Author Share Posted May 28, 2010 I can't figure out how to edit my post. Here's what I did. Is this right? // Loop each column for this row foreach($columnarray as $column => $value){ if ($count == 0) { // Add set phrase to query $query .= $column."='".$value."' "; $count = $count + 1; } else { // Add set phrase to query $query .= ", " . $column."='".$value."' "; } } And I set the count to 0 again outside the loop. Quote Link to comment https://forums.phpfreaks.com/topic/203218-how-do-i-get-all-the-text-input-values-from-a-form/#findComment-1064837 Share on other sites More sharing options...
MatthewJ Posted May 28, 2010 Share Posted May 28, 2010 Sorry, I didn't notice the last line about needing the comma... You could just add a comma after each value. If you do that though, you need to add a check for the last value because you obviously do not want to add a comma at the end. So logically, if it is not the last item of the array you're looping, add a comma. If it is, don't add the comma. That should get you started. Quote Link to comment https://forums.phpfreaks.com/topic/203218-how-do-i-get-all-the-text-input-values-from-a-form/#findComment-1064845 Share on other sites More sharing options...
ChemicalBliss Posted May 29, 2010 Share Posted May 29, 2010 Ah yes, as MatthewJ stated Sorry, I didn't notice the last line about needing the comma... You could just add a comma after each value. If you do that though, you need to add a check for the last value because you obviously do not want to add a comma at the end. So logically, if it is not the last item of the array you're looping, add a comma. If it is, don't add the comma. That should get you started. What i would do is build an array of items in the SET loop, then you can use "implode()" to add a string between each array item and turn it into a single string.eg, // Loop array foreach($form as $rowid => $columnarray){ // set query start $query = "UPDATE table "; // must have Space on the end $column_num = count($columnarray); // Get total number of array items (columns) in this form, Starting from 1. // Construct the SET phrase part. $set = array(); for($i=0;$i<$column_count;$i++){ // Add set phrase to query $set[] = $column_keys[$i]."='".$column_vals[$i]."' "; } if(count($set) >= 1){ $query .= "SET ( ".implode(",",$set)." )"; } // End query: $query .= " WHERE id='".$rowid."'"; // debug echo query echo($query); } -cb- Quote Link to comment https://forums.phpfreaks.com/topic/203218-how-do-i-get-all-the-text-input-values-from-a-form/#findComment-1064988 Share on other sites More sharing options...
DeX Posted June 14, 2010 Author Share Posted June 14, 2010 You guys are awesome, thanks. I have another question now, what if I wanted to add checkboxes? I set up the columns in the database to be of type BOOL because they're either on or off. There's a screenshot at the bottom of the columns I added on the web page and here's the code that I took from earlier in this thread and modified a bit. The delete checkbox on the far right works, I'm trying to get the middle 4 to input their true/false values into the database for checked/unchecked. if (isset($_POST['saveChanges'])) { $form = $_POST['form1']; $query3 = "DELETE from options where id = "; $deleteCount = 0; $deleteRows = false; // Santize $form, make sure its an array etc.. // Loop array foreach($form as $rowid => $columnarray) { // set query start $query = "UPDATE options SET "; $query2 = "INSERT into options (name, materialsPrice, labourPrice, weight, sku) VALUES ('"; $count = 0; // Loop each column for this row foreach($columnarray as $column => $value) { if ($value == 'delete') { $deleteRows = true; if ($deleteCount == 0) { $query3 .= $rowid; $deleteCount += 1; } else { $query3 .= " or id = " . $rowid; } } else { if ($count == 0) { if ($value == '') { $query2 .= "0'"; } else { $query2 .= $value . "'"; } // Add set phrase to query $query .= $column."='".$value."' "; $count = $count + 1; } else { if ($value == '') { $query2 .= ", 0"; } else { $query2 .= ", " . $value; } // Add set phrase to query // if ($column == 'quantityCheckbox') // { // $value = "off"; // } $query .= ", " . $column."='".$value."' "; } } } // End query: $query .= "WHERE id='".$rowid."'"; $query2 .= ")"; $checkForRow = @mysql_query("select * from options where id = '".$rowid."'"); if(mysql_num_rows($checkForRow) > 0) { echo $query; $updateTable = $db->query($query); } else { $insertValues = $db->query($query2); } } if ($deleteRows) { $deleteRowsResult = $db->query($query3); } } <? $count2 = 1; foreach ($all_rows as $column) { print ("<tr>"); print ("<td>". $column['name'] . "</td>"); print ("<td><input type='text' name='form1[".$column['id']."][materialsPrice]' id='".$column['id']."' value='".$column['materialsPrice']."'/> <input type='hidden' name='form1[".$column['id']."' value='"."' /></td>"); print ("<td><input type='text' name='form1[".$column['id']."][labourPrice]' id='".$column['id']."' value='".$column['labourPrice']."'/> <input type='hidden' name='form1[".$column['id']."' value='"."' /></td>"); print("<td><input type = 'checkbox' name = 'form1[".$column['id']."][quantityCheckbox]' id = '".$column['id']."' checked = '".$column['quantityCheckbox']."' value = '".$column['quantityCheckbox']."'/> <input type='hidden' name='form1[".$column['id']."' value='"."' /></td>"); print("<td><input type = 'checkbox' name = 'form1[".$column['id']."][widthCheckbox]' id = '".$column['id']."' checked = '".$column['widthCheckbox']."' value = '".$column['widthCheckbox']."'/>"); print("<td><input type = 'checkbox' name = 'form1[".$column['id']."][lengthCheckbox]' id = '".$column['id']."' checked = '".$column['lengthCheckbox']."' value = '".$column['lengthCheckbox']."'/>"); print("<td><input type = 'checkbox' name = 'form1[".$column['id']."][heightCheckbox]' id = '".$column['id']."' checked = '".$column['heightCheckbox']."' value = '".$column['heightCheckbox']."' /> "); print ("<td><input type='text' name='form1[".$column['id']."][weight]' id='".$column['id']."' value='".$column['weight']."'/> <input type='hidden' name='form1[".$column['id']."' value='"."' /></td>"); print ("<td><input type='text' name='form1[".$column['id']."][sku]' id='".$column['id']."' value='".$column['sku']."'/> <input type='hidden' name='form1[".$column['id']."' value='"."' /></td>"); print ("<td><input type='checkbox' name='form1[".$column['id']."][delete]' id='".$column['id']."' value='delete'/> <input type='hidden' name='form1[".$column['id']."' value='"."' /></td>"); print ("</tr>"); $count2 += 1; } ?> </tbody> </table> <div id="subnav"> <input type="hidden" name="saveChanges" id="saveChanges" value="saveChanges" /> <input type="submit" name="submit" value="Save" class="logout" /> <?php echo "<button onClick='addRow(".$count2.");return false;'>Add Row</button>" ?> Columns for middle 4 checkboxes are: - quantityCheckbox - widthCheckbox - lengthCheckbox - heightCheckbox [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/203218-how-do-i-get-all-the-text-input-values-from-a-form/#findComment-1072078 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.