Jump to content

[SOLVED] What's the best way to achieve this?


Bricktop

Recommended Posts

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!

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.

 

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 :(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.