Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Modificar y Borrar botones PHP

Estas en el tema de Modificar y Borrar botones PHP en el foro de PHP en Foros del Web. Hola, soy nuevo en el mundillo de la programacion aun, y tengo unas pequeñas dudas :S Tengo que hacer un script que se conecta a ...
  #1 (permalink)  
Antiguo 14/10/2013, 03:44
 
Fecha de Ingreso: octubre-2013
Ubicación: Torrelavega
Mensajes: 23
Antigüedad: 10 años, 6 meses
Puntos: 0
Modificar y Borrar botones PHP

Hola, soy nuevo en el mundillo de la programacion aun, y tengo unas pequeñas dudas :S

Tengo que hacer un script que se conecta a un router Mikrotik y de el saque un listado de las reglas NAT. para cada resultado que me salga dos botones, Modificar y Borrar.
Me conecto al router y saco el listado en una tabla con los dos botones sin problema, el problema esta en que no se como hacer que los botones funcionen :S

Os dejo el script completo:

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');
 
// 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='peartest.php' method='POST'>";
 
foreach ($responses as $response) {
    if ($response->getType() === RouterOS\Response::TYPE_DATA) {
        
		echo "<tr>";		
		echo "<td><input type='text' name='src' value='". $response->getArgument('src-address'). "'></input></td>";
        echo "<td><input type='text' name='toadd' value='". $response->getArgument('to-addresses'). "'></input></td>";
		echo "<td><input type='text' name='ports' value='". $response->getArgument('to-ports'). "'</input></td>";
        "\n";
		//Boton Modificar
		echo "<td><button type='submit' value='modificar' >Modificar</button>";
		//Boton Borrar
		echo "<form action='borrarnat.php' method='post'>
				<button type='submit' name='borrar' value=''>Borrar</button>";														
			
		echo "</tr>";
    }
}
echo "</table>";
echo "</form>";

?>
Tengo bastantes dudas, nose si crear 2 scripts uno para cada boton, o si agregarle a este mismo.. S:

Espero que me podais ayudar.

Muchas gracias y un saludo
  #2 (permalink)  
Antiguo 14/10/2013, 04:05
Avatar de Cuervoo  
Fecha de Ingreso: octubre-2013
Mensajes: 165
Antigüedad: 10 años, 6 meses
Puntos: 43
Respuesta: Modificar y Borrar botones PHP

Tenés problemas en html, no en php. Abrís la tabla y dentro de la tabla creás el form. Después primero cerrás la tabla y por último el form.

<table><form></table></form>

Y tendría que ser
<table><form></form></table>

Están al revés. Mirá el código fuente cuando te carga la página y fijate como te arma la página que seguro corrigiendo esa y alguna otra cosa que pueda haber al imprimir el html te va a andar.
  #3 (permalink)  
Antiguo 14/10/2013, 04:10
 
Fecha de Ingreso: octubre-2013
Ubicación: Torrelavega
Mensajes: 23
Antigüedad: 10 años, 6 meses
Puntos: 0
Respuesta: Modificar y Borrar botones PHP

Solucionado ese pequeño problemilla :) Aun asi me cargaba la tabla sin problemas.
El problema que tengo es en configurar los botones, nose como hacerlo :S
  #4 (permalink)  
Antiguo 14/10/2013, 04:19
 
Fecha de Ingreso: septiembre-2013
Mensajes: 46
Antigüedad: 10 años, 7 meses
Puntos: 1
Respuesta: Modificar y Borrar botones PHP

Yo no es que me explique especialmente bien, a ver si esto te ayuda
http://www.funcion13.com/2012/02/08/lista-de-elementos-ordenables-y-editables-usando-html5-jquery-php-y-mysql/
usa jquery, queda muy bien ya que no recargas la página.
  #5 (permalink)  
Antiguo 14/10/2013, 04:34
Avatar de Cuervoo  
Fecha de Ingreso: octubre-2013
Mensajes: 165
Antigüedad: 10 años, 6 meses
Puntos: 43
Respuesta: Modificar y Borrar botones PHP

Por lo que se ve, ese codigo imprime esto:

Código HTML:
Ver original
  1. <form action='peartest.php' method='POST'>
  2.    
  3.     <!-- ITERACION -->
  4.    
  5.     <input type='text' name='src' value='". $response->getArgument('src-address'). "'></input>
  6.     <input type='text' name='toadd' value='". $response->getArgument('to-addresses'). "'></input>
  7.     <input type='text' name='ports' value='". $response->getArgument('to-ports'). "'</input>
  8.    
  9.     <button type='submit' value='modificar' >Modificar</button>
  10.    
  11.     <form action='borrarnat.php' method='post'>
  12.         <button type='submit' name='borrar' value=''>Borrar</button>
  13.    
  14.     <!-- FIN ITERACION -->
  15.  
  16. </form>

Bien, los </input> no van.

Asi tendrían que quedar:
<input type='text' name='src' value='". $response->getArgument('src-address'). "' />

Cada vez que da una vuelta, abre un form y adentro le agregás un botón
<form action='borrarnat.php' method='post'>
<button type='submit' name='borrar' value=''>Borrar</button>

No sé qué efecto tiene crear un form dentro de otro pero no va asi. Además que no se cierre.

Revisá bien el código html que te está creando porque anda por ahi el problema.
  #6 (permalink)  
Antiguo 14/10/2013, 04:34
 
Fecha de Ingreso: octubre-2013
Ubicación: Torrelavega
Mensajes: 23
Antigüedad: 10 años, 6 meses
Puntos: 0
Respuesta: Modificar y Borrar botones PHP

Cita:
Iniciado por leeann699 Ver Mensaje
Yo no es que me explique especialmente bien, a ver si esto te ayuda
http://www.funcion13.com/2012/02/08/lista-de-elementos-ordenables-y-editables-usando-html5-jquery-php-y-mysql/
usa jquery, queda muy bien ya que no recargas la página.
Pero es que entre que js no le controlo (deberia lo se, pero en ASIR no se toca nada de javascript, solo php html y xml :S ) Y que funciona con consultas a una base de datos y sql..no me sirve de mucho, de todos modos gracias :)
  #7 (permalink)  
Antiguo 14/10/2013, 04:43
 
Fecha de Ingreso: octubre-2013
Ubicación: Torrelavega
Mensajes: 23
Antigüedad: 10 años, 6 meses
Puntos: 0
Respuesta: Modificar y Borrar botones PHP

Cita:
Iniciado por Cuervoo Ver Mensaje
Por lo que se ve, ese codigo imprime esto:

Código HTML:
Ver original
  1. <form action='peartest.php' method='POST'>
  2.    
  3.     <!-- ITERACION -->
  4.    
  5.     <input type='text' name='src' value='". $response->getArgument('src-address'). "'></input>
  6.     <input type='text' name='toadd' value='". $response->getArgument('to-addresses'). "'></input>
  7.     <input type='text' name='ports' value='". $response->getArgument('to-ports'). "'</input>
  8.    
  9.     <button type='submit' value='modificar' >Modificar</button>
  10.    
  11.     <form action='borrarnat.php' method='post'>
  12.         <button type='submit' name='borrar' value=''>Borrar</button>
  13.    
  14.     <!-- FIN ITERACION -->
  15.  
  16. </form>

Bien, los </input> no van.

Asi tendrían que quedar:
<input type='text' name='src' value='". $response->getArgument('src-address'). "' />

Cada vez que da una vuelta, abre un form y adentro le agregás un botón
<form action='borrarnat.php' method='post'>
<button type='submit' name='borrar' value=''>Borrar</button>

No sé qué efecto tiene crear un form dentro de otro pero no va asi. Además que no se cierre.

Revisá bien el código html que te está creando porque anda por ahi el problema.

El form solo le creo una vez, ya que le abro y le cierro fuera del foreach, creo que no habria problema con ello

Código:
 echo "<form action='peartest.php' method='POST'>";
 
foreach ($responses as $response) {
    if ($response->getType() === RouterOS\Response::TYPE_DATA) {
        
		echo "<tr>";
		echo "<td><input type='text' name='src' value='". $response->getArgument('src-address'). "'/></td>";
        echo "<td><input type='text' name='toadd' value='". $response->getArgument('to-addresses'). "'/></td>";
		echo "<td><input type='text' name='ports' value='". $response->getArgument('to-ports'). "'/></td>";
        "\n";
		//Boton Modificar
		echo "<td><button type='submit' value='modificar' >Modificar</button>";
		//Boton Borrar
		echo "<form action='borrarnat.php' method='post'>
				<button type='submit' name='borrar' value=''>Borrar</button>";														
			
		echo "</tr>";
    }
}
echo "</form>";
echo "</table>";
El otro form, que va a borrarnat.php no lo hagais caso, fue una prueba mas que nada, por si tiraba a la otra pagina. De todos modos nose como crear los botones como os lo dije antes :S
  #8 (permalink)  
Antiguo 14/10/2013, 07:12
 
Fecha de Ingreso: octubre-2013
Ubicación: Torrelavega
Mensajes: 23
Antigüedad: 10 años, 6 meses
Puntos: 0
Respuesta: Modificar y Borrar botones PHP

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>";

?>
  #9 (permalink)  
Antiguo 14/10/2013, 09:34
Avatar de AnGuisi  
Fecha de Ingreso: julio-2013
Ubicación: San Felipe - Yaracuy
Mensajes: 122
Antigüedad: 10 años, 9 meses
Puntos: 2
Respuesta: Modificar y Borrar botones PHP

Podrías hacer esto:

Eliminas el atributo action de tu etiqueta form y haces lo siguiente:

Código HTML:
Ver original
  1. <button type="submit"   onclick = "this.form.action = 'PAGINA ELIMINAR'" name="eliminar" ">Eliminar</button>
  2.  
  3. <button type="submit"   onclick = "this.form.action = 'PAGINA EDITAR'" name="editar" ">Editar</button>

Etiquetas: botones, nat, router, scripts
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:03.