Hola de nuevo, siento por el doble post, pero he conseguido mejorar bastante mi codigo. Aun asi sigue sin funcionar los botones, es algun problema en el action..
Código:
<?php
use PEAR2\Net\RouterOS;
// require_once 'pear2\src\PEAR2\Autoload.php';
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';
$client = new RouterOS\Client('192.168.150.161', 'admin', 'admin');
//Action
if (isset($_POST['act'])) {
$act = $_POST['act'];
unset($_POST['act']);
//Limit the action only to known ones, for security's sake
switch ($act) {
case 'set':
case 'remove':
$actionRequest = new Request("/ip/firewall/nat/{$act}");
$itemIDs = array_filter(
array_keys($_POST),
function ($val) {
return '*' === $val[0];//All item IDs start with a "*"
}
);
foreach ($itemIDs as $id) {
$actionRequest->setArgument('numbers', $id);
foreach ($_POST[$id] as $name => $value) {
$actionRequest->setArgument($name, $value);
}
$responses = $client->sendSync($actionRequest);
//If you want to do some error handling, do it here by inspecting $responses.
//You can safely assume that if the table shows your old data, as opposed to the new one,
//the action failed.
}
}
}
// Tabla
echo "<table align='center' border=1 bordercolor='black'>";
echo "<tr><td align=left size=3>Src Address</td><td size=3>To Addresses</td><td size=3>Ports</td><td align=left size=3>Modificar/Eliminar</td></tr>";
// Peticion a la API
$responses = $client->sendSync(new RouterOS\Request('/ip/firewall/nat/print'));
echo "<form action='natfinal.php' method='POST'>";
foreach ($responses as $response) {
if ($response->getType() === RouterOS\Response::TYPE_DATA) {
echo "<tr>";
echo '<td><input type="text" name="' . $response('.id') . '[src-address]" value="' . $response('src-address'). '"/></td>';
echo '<td><input type="text" name="' . $response('.id') . '[to-addresses]" value="' . $response('to-addresses'). '"/></td>';
echo '<td><input type="text" name="' . $response('.id') . '[to-ports]" value="' . $response('to-ports'). '"/></td>';
"\n";
//Boton Modificar
echo '<td><button type="submit" name="act[' . $response('.id') . ']" value="set">Modificar</button>';
//Boton Borrar
echo '<button type="submit" name="act[' . $response('.id') . ']" value="remove">Borrar</button>';
echo "</td></tr>";
}
}
echo "</form>";
echo "</table>";
?>