set variable(s) from checkboxes - display multiple results

I am using the below to sent ssh commands through a web interface. Below is an example to get the hostname of a Linux computer.

I need to change this form, from a dropdown menu, to checkboxes. I know how to do this in html, but I don’t know how to tell php to handle it. Reason is, what if multiple checkboxes are selected? how would target=“main” display results from multiple computers?

index.html

[code]

Select Client
Client001
Client002
Client003

[/code]

hostname.php

[code] <?php
include(‘Net/SSH2.php’);
include(‘variables.php’);

$ip = $_GET[‘clients’];
$ssh = new Net_SSH2($ip);
if (!$ssh->login($user, $pass)) {exit(‘Login Failed’);}
echo $ssh->exec(‘hostname’);
?>[/code]

Well, normally you would not want to stick an IP address in a webpage as hackers can get it with ease.
Normally, you would use values that point to the IP and then place the IP’s into table to protect them.
You can simply use code numbers or names and then look up the actual IP addresses. If this is for a LAN that
is secured at one location and outsiders can not access the webpage, then it is less of a security issue.

As far as multiple checkboxes, that is quite easy if you use an array for them. Here is a link that explains in the
solution how to do it. Hope this helps!
http://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes

Thanks for your reply. I’m nearly there, but its really confusing for me.
I need to be able to select multiple hosts, and then choose a command to run on them.

[code]
Client001

Client002

Client003

Client004

Client005


Select Command Uptime Disk Usage

[/code]

Okay, you have the HTML page sorted out. Now, you need to alter your PHP code to parse thru all of the
checkmarks that are selected and do each of the them instead of just the first one. Therefore, you need to
combine your old code with the code in the post I showed you. Something loosely and untested like this:
[php]
if(!empty($_POST[‘check_list’])) {
foreach($_POST[‘check_list’] as $ip) {
$ssh = new Net_SSH2($ip);
if (!$ssh->login($user, $pass)) {exit(‘Login Failed’);}
echo $ssh->exec(‘hostname’);
}
}
[/php]
Basically, instead of calling the checked input $check, I made it $ip which should have the value of the IP.
Then, it processes any of the checked IP’s in the foreach loop. Not tested, but, should work for you…

EDIT: Note that if the user does not check any of them, nothing is done at all. Also, I usually use !isset()
instead of !empty() but either should work…

ok, this is exactly what I have now (except user & pass). It doesn’t return any errors, but target="_blank" doesn’t return anything. Just a blank browser.

test.html

[code]
Client001

Client002

Client003

Client004

Client005


Select Command Uptime Hostname

[/code]

test.php
[php]<?php
include(‘Net/SSH2.php’);

$user = “userhere”;
$pass = “passhere”;
$ip = $_GET[‘check_list’];
$ssh = new Net_SSH2($ip);
$cmds = $_GET[‘commands’];

if(!empty($_POST[‘check_list’])) {
foreach($_POST[‘check_list’] as $ip) {
$ssh = new Net_SSH2($ip);
if (!$ssh->login($user, $pass)) {exit(‘Login Failed’);}
echo $ssh->exec($cmds);
}
}
?>[/php]

Short on time right now, but, what are you trying to do with _blank? That makes the browser open a new page.
When you select one or more IP’s and one command, what are you attempting to do with the output? Are you
not just trying to dump the results on the same page you have open? If so, you want to echo the results, right?

Your action sends the form to the “test.php” page, but, in a new window. Is that what you wanted?
And, you are now switching to $_GET instead of $_POST arrays. Since your data is in POSTED values, you can
not grab them from the $_GET[] array. I am confused what you are now doing…

Perhaps you meant to type $_POST and just mistyped it as $_GET ??? Hope so…

Yeah you’re right. _GET was a typo. I changed to _POST and its working :slight_smile: Thanks!

Any way I can get the output of each selected checkbox on a different line?
Right now everything’s in one line.

Well, congrats! Now, remember you are showing your data on a web page! So, just add a line break!

Change: echo $ssh->exec($cmds);

To: echo $ssh->exec($cmds) . “
”;

You can add it before or after the echo depending on what is also displayed in that area. You could also use CSS
formatting, but, for this simple code, I think the line break is just fine…

I will mark this one solved… See you in the next puzzler you run into…

Sponsor our Newsletter | Privacy Policy | Terms of Service