aeroswat Posted February 22, 2010 Share Posted February 22, 2010 Is there a way to submit all of the option values of a select box to a php file? Here's an example <form action="page.php" method="post"> <select name="bla"> <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> </select> <input type="submit" name="Submit" value="Submit" /> </form> and have the php $_POST variable show all of the option values instead of just the currently selected one. Link to comment https://forums.phpfreaks.com/topic/192978-php-post-option-values/ Share on other sites More sharing options...
sader Posted February 22, 2010 Share Posted February 22, 2010 <select multiple="multiple" name='x[]'> </select> and in php code u will have values in array foreach($_POST['x'] as $x) { echo $x; } Link to comment https://forums.phpfreaks.com/topic/192978-php-post-option-values/#findComment-1016297 Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 You could do so using an array... <form action="page.php" method="post"> <select name="bla"> <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> </select> <input type="hidden" name="blaOptions[]" value="a" /> <input type="hidden" name="blaOptions[]" value="b" /> <input type="hidden" name="blaOptions[]" value="c" /> <input type="submit" name="Submit" value="Submit" /> </form> This lets you keep what was selected in bla, but gives you an array $blaOptions that contain all the values that $_POST['bla'] could have been. Link to comment https://forums.phpfreaks.com/topic/192978-php-post-option-values/#findComment-1016299 Share on other sites More sharing options...
aeroswat Posted February 22, 2010 Author Share Posted February 22, 2010 <select multiple="multiple" name='x'> </select> and in php code u will have values in array foreach($_POST['x'] as $x) { echo $x; } That would only show the ones that are selected right? It wouldn't show them all would it? What I have is 2 boxes. One box holds different admin rights and the second box holds the rights that are currently assigned to that account. I have buttons that can move rights from one side to another. When the user hits submit I want it to send all the rights that are in the assigned rights box to my php page so that I can update the database with their new rights. I suppose that I could force javascript to select all of the options before it is submitted through the onclick event but is there any better way of doing this? Link to comment https://forums.phpfreaks.com/topic/192978-php-post-option-values/#findComment-1016300 Share on other sites More sharing options...
aeroswat Posted February 22, 2010 Author Share Posted February 22, 2010 You could do so using an array... <form action="page.php" method="post"> <select name="bla"> <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> </select> <input type="hidden" name="blaOptions[]" value="a" /> <input type="hidden" name="blaOptions[]" value="b" /> <input type="hidden" name="blaOptions[]" value="c" /> <input type="submit" name="Submit" value="Submit" /> </form> This lets you keep what was selected in bla, but gives you an array $blaOptions that contain all the values that $_POST['bla'] could have been. I thought about doing this but if i did this how would i set the hidden input when the box is updated? Link to comment https://forums.phpfreaks.com/topic/192978-php-post-option-values/#findComment-1016301 Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 You could do so using an array... <form action="page.php" method="post"> <select name="bla"> <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> </select> <input type="hidden" name="blaOptions[]" value="a" /> <input type="hidden" name="blaOptions[]" value="b" /> <input type="hidden" name="blaOptions[]" value="c" /> <input type="submit" name="Submit" value="Submit" /> </form> This lets you keep what was selected in bla, but gives you an array $blaOptions that contain all the values that $_POST['bla'] could have been. I thought about doing this but if i did this how would i set the hidden input when the box is updated? It's a little bit involved, but not too bad. I assume you're using javascript to move the values from one box to the other? My suggestion would be to create a hidden input field for every permission. Give the hidden input the name of the permission it pertains to. Then use javascript to change the value to either 0 (the user doesn't have that permission) or 1 (the user does have that permission). It doesn't need to be 0 or 1, it can be whatever is appropriate for your use, that's just an example. If you're using getElementById in javascript, I'd remind you to give the hidden inputs an id tag. If you already have a function that handles moving permissions from 1 list to another, use that same function to change the values of the hidden inputs. When done this way, the select boxes actually have nothing to do with the backend, they're just there to show the user what's going on. The backend looks for the settings of the hidden inputs. This is actually a pretty cool design idea, if I ever set up a system like this I hope you don't mind if I steal this idea. Link to comment https://forums.phpfreaks.com/topic/192978-php-post-option-values/#findComment-1016302 Share on other sites More sharing options...
aeroswat Posted February 22, 2010 Author Share Posted February 22, 2010 You could do so using an array... <form action="page.php" method="post"> <select name="bla"> <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> </select> <input type="hidden" name="blaOptions[]" value="a" /> <input type="hidden" name="blaOptions[]" value="b" /> <input type="hidden" name="blaOptions[]" value="c" /> <input type="submit" name="Submit" value="Submit" /> </form> This lets you keep what was selected in bla, but gives you an array $blaOptions that contain all the values that $_POST['bla'] could have been. I thought about doing this but if i did this how would i set the hidden input when the box is updated? It's a little bit involved, but not too bad. I assume you're using javascript to move the values from one box to the other? My suggestion would be to create a hidden input field for every permission. Give the hidden input the name of the permission it pertains to. Then use javascript to change the value to either 0 (the user doesn't have that permission) or 1 (the user does have that permission). It doesn't need to be 0 or 1, it can be whatever is appropriate for your use, that's just an example. If you're using getElementById in javascript, I'd remind you to give the hidden inputs an id tag. If you already have a function that handles moving permissions from 1 list to another, use that same function to change the values of the hidden inputs. When done this way, the select boxes actually have nothing to do with the backend, they're just there to show the user what's going on. The backend looks for the settings of the hidden inputs. This is actually a pretty cool design idea, if I ever set up a system like this I hope you don't mind if I steal this idea. Lol go for it So if I did the hidden inputs and I wanted to access them on php's end via the name of the permission like so $_POST['blaOptions']['A'] is there an easy way to do that? Or should i just scrap the idea of making the hidden inputs an array and give them individual names? Link to comment https://forums.phpfreaks.com/topic/192978-php-post-option-values/#findComment-1016307 Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 You could give the array names like so... <input type="hidden" name="permissionArray[somePermission]" value="on" /> Use Javascript to change the value from on or off. The name of the input field is totally up to you, but you're going to need 1 hidden input for every permission, and if you use getElementById in your javascript, you're also going to need an ID for each input field. Link to comment https://forums.phpfreaks.com/topic/192978-php-post-option-values/#findComment-1016311 Share on other sites More sharing options...
aeroswat Posted February 22, 2010 Author Share Posted February 22, 2010 You could give the array names like so... <input type="hidden" name="permissionArray[somePermission]" value="on" /> Use Javascript to change the value from on or off. The name of the input field is totally up to you, but you're going to need 1 hidden input for every permission, and if you use getElementById in your javascript, you're also going to need an ID for each input field. Ya. We misunderstood each other I think. Anywho I've made each of them have their own name for ease. I'm just adding it into the javascript now to finish it up. I'll let ya know if it works Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/192978-php-post-option-values/#findComment-1016316 Share on other sites More sharing options...
aeroswat Posted February 22, 2010 Author Share Posted February 22, 2010 Got it working and it's very sexy Thanks again! Link to comment https://forums.phpfreaks.com/topic/192978-php-post-option-values/#findComment-1016349 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.