Jump to content

[SOLVED] Can't get data from array


Bricktop

Recommended Posts

Hi all,

 

I have a selct box with data from an array, I press submit and then want that data to appear via another function, but I can't seem to grab the data from that function.  here's my code:

 

//SELECT BOX
function clientbackup()
{

$clients = array('hostname' => array('john', 'frank', 'bob'),
			 'database' => array('dbjohn', 'dbfrank', 'dbbob'),
                 'username' => array('john1', 'frank2', 'bob3'),
                 'password' => array('pass1', 'pass2', 'pass3'));

    $content .= '<form method="POST" action="?fct=backup">';
$content .= '<select name="clientid">';

foreach($clients['database'] as $key => $val)
{
$content .= '<option value="'.$key.'">'.$val.'</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>';
$content3 .= '</form>';	

echo $content;
}

//BACKUP FUNCTION
function backup()
{
global $clients;

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$id = $_POST['clientid'];

echo $clients['hostname'][$id];
echo $clients['database'][$id];
echo $clients['username'][$id];
echo $clients['password'][$id];
}
}

 

Anyone what I've done wrong?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/173211-solved-cant-get-data-from-array/
Share on other sites

The problem is that you are trying to use the global keyword to being something that is defined in one function into another function. It does not work that way.

 

The solution: Place the $clients array in your main code, add it as a parameter in each function definition prototype, and place it into the function call as a parameter. By doing this you make your functions general purpose and you can call them with any current or future $clients information by just setting up the $clients array in the main code, rather than going through the code in your function and changing it. One of the points of using functions is that you write them and test them once, and then reuse them whenever and wherever you want. There should not be any application specific data defined inside of a function definition.

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.