";I need complete this code, but i dont know how..
P.D: I have this code too:
```text
$addRequest = new RouterOS\Request('/ip/firewall/nat/add');
$addRequest->setArgument('chain', 'dstnat');
$addRequest->setArgument ('protocol', 'tcp');
$addRequest->setArgument('action', 'dst-nat');
$addRequest->setArgument('to-addresses', '192.168.2.3');
$addRequest->setArgument ('to-ports', '4562');
```
But now, how i can convert this in a form? :S
Thanks :slight_smile:
doesn’t really add anything, since add requests need to actually contain the properties to be added.
You need to inspect the $_POST array, and convert it to the arguments.
Alternatively, you can just use the Util class, to which you can pass the $_POST array, MINUS anything that isn’t property/value pairs to be added. E.g.
<?php
//Listas desplegables
echo " ";
$util = new RouterOS\Util($client);
$util->changeMenu('/ip/firewall/nat');
//Peticion a la API
if (isset($_POST['add'])) {
unset($_POST['add']);
if ('' == $util->add($_POST)) {
echo '
";(Though keep in mind that with that approach, you don't get the exact error messages from RouterOS - you only know if adding failed or not, but not why)
I update the code, and its working, but i have another problem.
In radio buttons, when i choose src(chain srcnat), the action should be src-nat, and when i choose dst(chain dstnat), the action should be dst-nat
When i create a nat rule, it creates with action accept :S
I need add nat rules without values too, without protocol, src and dst ports :S
The first table, with nat rules print, works very well, thanks to u too, but the new table to add new rules no :S
Apparently, an empty key name is not allowed… try the above now (where I’ve added a “*” in front of all fields on adding; I’ve also corrected the form itself - it should start before the table, and I’ve made the add form separate to avoid sending everything else when adding).
Working, but we still have 2 problems, when i refresh after add, it add a new same rule, and i cant add new rule without protocol and ports. In winbox we can..
The redirect will force the browser to go back to the same page, but without any $_POST data following it. We do this only on success, since you may otherwise try to retry a previously failed request by refreshing the page.
An for the adding without empty values issue, replace
foreach ($_POST[$itemID] as $name => $value) {
$actionRequest->setArgument($name, $value);
}
with
foreach ($_POST[$itemID] as $name => $value) {
if ('' != $value) {
$actionRequest->setArgument($name, $value);
}
}