turpentyne Posted July 31, 2012 Share Posted July 31, 2012 I have a php loop that generates values from the database. I want to push their choice _POST into a string session variable, but how if I don't know the _POST name? did that make any sense? So I have this code (extracted from a loop): $ExpandImageTemplate = <<<OutHTML %4\$s <br> <button type="submit" name="%3\$s" id="go" value="%2\$s"> <img src="%3\$s" width="147" height="34" alt="image to come" title="choice" /> </button> OutHTML; $Output .= sprintf ($ExpandImageTemplate, htmlspecialchars ($row['comp_cat_name']),htmlspecialchars ($row['folder_path']),htmlspecialchars ($row['image_filepath']),htmlspecialchars ($row['component_name'])); I won't know what name each button will have when it submits. but I want to do something: session_start(); if(!isset($_SESSION['options_picked'])){ $_SESSION['options_picked'] = ""; } // here's where I'm stumped if (isset($_POST["?????????"])) { $_SESSION['options_picked'] .= $_POST["????????"]; } Or maybe there's an easier way? Quote Link to comment https://forums.phpfreaks.com/topic/266520-get-_post-results-when-i-dont-know-names/ Share on other sites More sharing options...
cpd Posted July 31, 2012 Share Posted July 31, 2012 POST data tends to come from a form... you would have created the form so you'll know the name of the field. Your question kind of doesn't make sense. You firstly say your generating values from a database and then go on to say you want to set $_POST data to a $_SESSION variable. There is no link between the database and you wanting to set $_POST data to a $_SESSION variable. Quote Link to comment https://forums.phpfreaks.com/topic/266520-get-_post-results-when-i-dont-know-names/#findComment-1365833 Share on other sites More sharing options...
turpentyne Posted July 31, 2012 Author Share Posted July 31, 2012 Forgive me for not knowing how to explain this. I'm new and I'm trying to do this with SprintF - something I've never worked with before. And maybe I'm going about this wrong.. The button name is written in PHP as: name="%3\$s" The %3\$s value comes from the sprintf (in this case, $row['component_name']) : $Output .= sprintf ($ExpandImageTemplate, htmlspecialchars ($row['comp_cat_name']),htmlspecialchars ($row['folder_path']),htmlspecialchars ($row['component_name']),htmlspecialchars ($row['image_filepath'])); This value, in turn, comes from a query to the database. What they clicked on, could be one of a dozen possibilities, in this case. I don't know what that key is, because I didn't set it. the database essentially determined it, via a loop. When they make their selection, the page refreshes and I need to add that choice to my session variable. Quote Link to comment https://forums.phpfreaks.com/topic/266520-get-_post-results-when-i-dont-know-names/#findComment-1365836 Share on other sites More sharing options...
Christian F. Posted August 1, 2012 Share Posted August 1, 2012 You can use the name of the input to create an array inside the $_POST array, so that you can easily loop through the content. All you have to do is to use [] as a part of the name, like this: <input type="text" name="options[%3\$s]" value="%4$s"> Then you can just loop through all of the options like this: foreach ($_POST['options'] as $key => $value) { That said: Whenever you have variable names for input fields, chances are that you're doing things in an overly complex manner. Most of the time it's better to either use a static name, and act upon the content sent, or use another field to send the name you've tried to use as a name. Quote Link to comment https://forums.phpfreaks.com/topic/266520-get-_post-results-when-i-dont-know-names/#findComment-1365877 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.