pass an id value by a URL of a button

0

Hi, I would like to know how to pass the value of the id = information by the URL of the input type="submit"

the code is as follows:

 <div id="informacion">0</div>  

<form id="form1" name="form1" method="post" action="index3.php?var= <?php echo $id['informacion'];?>">
  <label>
  <input type="submit" name="Submit" value="Enviar" />
  </label>
</form>

I would like to receive on the page index3.php the value of the id = information that in this case is "0"

    
asked by diego c 18.01.2018 в 04:28
source

2 answers

1

Hello to solve this problem you must use DOM. Suppose that the page where the form is is called "login.php".

First the action of the form must indicate the page where it will be sent, which I believe is index3.php, then on this page you should go the following code within the corresponding php tags:

<?php
    $url = 'http://localhost/php/login.php'; //Url de donde esta el formulario

    //Se obtiene el contenido de la página del formulario  
        $html = file_get_contents($url);
    //Se genera el DOM  
        $doc = new DOMDocument;  
        $doc->loadHTML($html, LIBXML_COMPACT | LIBXML_HTML_NOIMPLIED | LIBXML_NONET);  

    //Se obtiene el elemento mediante su id (div)  
        $texto = $doc->getElementById('informacion');  

    //Obtener el texto del elemento  
        $textoDiv = $texto->textContent;  

    //Imprimir el resultado en el archivo index3.php  
        echo "Texto: " . $textoDiv;  
?>  

With this you will see the contents of the div on the page index3.php

I hope you help greetings!

    
answered by 18.01.2018 в 06:20
0

A little easier, in jQuery and AJAX:

$("#form1").submit(function(e) {
    e.preventDefault();
    var informacion = $("#informacion").html();
    $.ajax({
        method: "POST",
        url: "index3.php",
        data: {
            informacion: informacion
        }
    }).done(function(r) {
        alert("Hecho!");
    });
});

Or even more simple, in the form, add the information field as input, if you want the div to be there, you can add it with the type hidden:

<form id="form1" name="form1" method="post" action="index3.php">
    <input type="hidden" name="informacion" value="<?php echo $id['informacion'];?>">
    <label>
    <input type="submit" name="Submit" value="Enviar" />
    </label>
</form>
    
answered by 18.01.2018 в 10:32