Bricktop Posted September 4, 2009 Share Posted September 4, 2009 Hi all, I would like to pass some varibales to a function based on the currently selected item in a drop-down box which has been built from a multidimensional array. However, the variables I want to pass are all held in the array. I wonder what the best way is to achieve this? My code: function clientdetails() { $clients = array( array( Name => "john", Username => john1, Password => pass1 ), array( Name => "frank", Username => frank2, Password => pass2 ), array( Name => "Bob", Username => bob3, Password => pass3 ), ); $rowcount = count($clients); $content .= '<form method="POST">'; $content .= '<select name="clientbackup">'; for ($row = 0; $row < $rowcount; $row++) { $content .= '<option value="'.$clients[$row]["Title"].'">'.$clients[$row]["Title"].'</option>'; } $content .= '</select>'; $content .= '<p>Email Backup File?'; $content .= '<input type="checkbox" name="email" /></p>'; $content .= '<p><input type="submit" name="submit" value="Submit" /></p>'; $content .= '</form>'; echo $content; } I have a function named backup which accepts the variables stored in the array above. For example I would call it like: backup($name,$username,$password) Basically, I would like to be able to choose a name from the list, click the "Submit" button and the relevant variables be passed to the backup() function. Is it best to do this with hidden input fields or is there some better/more secure way of doing what I want? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/173114-solved-whats-the-best-way-to-achieve-this/ Share on other sites More sharing options...
ToonMariner Posted September 4, 2009 Share Posted September 4, 2009 The structure of your array makes it quite difficult to work with... You see that each index of your array contains further indices of name, username and password. This makes searching that array much more difficult... Create the array so its structure looks like this. (is there any other way of doing this instead of putting the username and password details in?) <?php $clients = array('names' => array('john', frank, 'bob'), 'username' => 'john1', 'frank2', 'bob3'), 'password' => 'pass1', 'pass2', 'pass3'); ?> Now all you need do is loop through the child arrays. <select name="clientid" name="clientid"> <?php foreach($clients['names'] as $key => $val) { ?> <option value="<?php echo $key; ?>"><?php echo $val; ?>/option> <?php } ?> </select> Now in the processing script you can easily retrieve which user by the key that has been passed. <?php function backUp($id) { global $clients; $name = $clients['name'][$id]; $username = $clients['username'][$id]; $password = $clients['password '][$id]; // .. do your stuff.... } $clientid = $_POST['clientid']; // do some sanitization just incase! - like check its a number. backUp($clientid); ?> There are several other (perhaps preferable) ways of achieving this and perhaps you should have a think about your application and see if there is scope improve implementation. Link to comment https://forums.phpfreaks.com/topic/173114-solved-whats-the-best-way-to-achieve-this/#findComment-912455 Share on other sites More sharing options...
Bricktop Posted September 4, 2009 Author Share Posted September 4, 2009 Thanks ToonMariner, I structured my array like that so it was easy to add a new client in its very own block of code. I wonder if anyone knows if the following is possible? (PSEUDOCODE) //When user clicks "Submit" button if($_POST['clientbackup'] == $clients[$row]["Name"]) //Essentially matches a name in the array { backup($clients[$row]["Name"],$clients[$row]["Username"],$clients[$row]["Password"]) } I don't think the above can be achieved, I may just have to use hidden input fields Link to comment https://forums.phpfreaks.com/topic/173114-solved-whats-the-best-way-to-achieve-this/#findComment-912474 Share on other sites More sharing options...
Bricktop Posted September 4, 2009 Author Share Posted September 4, 2009 OK, I don't think I can do it with hidden input fields either! If anyone knows how I can pass the data from the arrays to my function easily (while maintaining my array structure) please let me know. Thanks Link to comment https://forums.phpfreaks.com/topic/173114-solved-whats-the-best-way-to-achieve-this/#findComment-912488 Share on other sites More sharing options...
ToonMariner Posted September 4, 2009 Share Posted September 4, 2009 don't pass the name through the url - pass the index key - numeric much easier to deal with. Link to comment https://forums.phpfreaks.com/topic/173114-solved-whats-the-best-way-to-achieve-this/#findComment-912577 Share on other sites More sharing options...
Bricktop Posted September 4, 2009 Author Share Posted September 4, 2009 Thanks ToonMariner. I'll mark this topic as solved and give your suggestions a try. Link to comment https://forums.phpfreaks.com/topic/173114-solved-whats-the-best-way-to-achieve-this/#findComment-912685 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.