Foreach + InnerHtml take a concrete value

0

I have a foreach where several forms are generated and for each one there is a modify button, I want to give that button modify to take the input name="txtdireccion_mac" that belongs to it.

--- HTML

 @foreach($datos_usuario as $value) 
                <tr>
                <form action="Borrar_mac" id="idformac_usu" name="formac_usu" method="POST" >
                    {!! csrf_field(); !!}
                    <td><input type="text" name="txtdireccion_mac" readonly="readonly" value="{!! $value->Direccion_mac !!}"></td>
                    <td><input type="text" name="txt_descripcion" readonly="readonly" value="{!! $value->Descripcion !!}"  ></td>
                    <td><input type="text" name="txtfecha" readonly="readonly" value=" {!! $value->Fecha !!} "  ></td>

                    <td><input type="button" name="modificar" onclick="cambiarMac(this)" value="Modificar Mac" ></td>
                    <td><input type="submit" name="btn_borrar" value="Borrar Mac"></td>

                </form>
                </tr>   
                  @endforeach   
            </table>

        <div id='formulario_cambio_mac'>

        </div>

Javascript

 function cambiarMac(obj){
                var txt1 = "<form action='cambiar_datos_mac' method='POST'>";
                txt1 += "<input type='hidden' name='identificadormac' id='ididentificadormac' value=''>";
                txt1 += "Escribe la nueva Mac <input type='text' name='txtmac' value=''  ><br/>";
                txt1 += "Escribe la Descripcion  <input type='text' name='txtdescripcion' value='' ><br/>";
                txt1 += "<input type='submit' name='btn_enviar' value='Cambiar Mac'>";
                txt1 += "</form>";

                document.getElementById("formulario_cambio_mac").innerHTML = txt1;
                var mac=obj.value;
                document.getElementById('identificadormac').value = mac; 
            }

With the hidden field is to search the old mac on the bbdd and change it to the new one. The problem is that when I click on the button and paint the form, I do not know how to get the mac address of that row from that form.

If there is a simpler way or something, I am open to suggestions.

    
asked by EduBw 29.11.2017 в 23:44
source

1 answer

2

You can generate a dynamic id with some value of your query, like for example the current mac, and we send it to the function cambiarMac the mac so that it knows how to look for the input. For example:

@foreach($datos_usuario as $value) {

        <input type="text" name="txtdireccion_mac"

        // generamos id dinamico
        id="mac_{!! $value->Direccion_mac !!}"
        readonly="readonly" value="{!! $value->Direccion_mac !!}">

        <input type="button" name="modificar" 

        // le enviamos el id como parametro a la funcion cambiarMac
        onclick="cambiarMac(this,'mac_{!! $value->Direccion_mac !!}')" value="Modificar Mac" >

Then in your function you only have to receive the parameter and look for the input by the sent id:

function cambiarMac(obj, idMac){

    var valueMacActual = document.getElementById(idMac).value;

    //...
}
    
answered by 30.11.2017 / 00:05
source