PHP page inside a PHP page

Asked

Viewed 52 times

-1

Speak personally, then my problem is this, I have a PHP page that is my form and I have a PHP page to receive the information from the form and give the answer to the user, very simple thing. But I wanted the answer to the user to be inside the form page, when he sends the form, to appear inside a table inside the same page of the form, how do this?

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <title>lista de tarefas</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <form>
       <table id="tab"> 
    <?php
        $dat=isset($_GET["data"])?$_GET["data"]:"15/10/2019";
        $tar=isset($_GET["tarefa"])?$_GET:"não informado";
        $tab=$_GET["tab"];
        
        echo "<tr><td>$tar</td><td>até $dat</td></tr>";
         
    ?>
        </table>   
    </form>
</body>
</html>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title>cadastro de tarefas</title>
</head>
<body>
  <form method="GET" action="form.php">
    <fieldset>
    <legend>Crie Sua tarefa</legend>
    <p><label>tarefa</label>    : <input type="text" name="tarefa" ></p>
    <p><label>conclusão</label> : <input type="date" name="data"></p>
  </fieldset>
    <input type="submit" value="Enviar">
  </form>
</body>
</html>

2 answers

0

There are many ways to do that. The first (that I like best) and several companies use is using XML Httprequest :https://www.w3schools.com/xml/xml_http.asp. Using this technique, it is possible, for example, to make the request without even updating the entire page (the famous AJAX).

AJAX is a developer’s dream because you can:

  • Read data from a web server - after the page has been loaded
  • Refresh a web page without reloading it
  • Send data to a web server - in the background

I’ll make a simple example so you know how it works.

index php.:

<!DOCTYPE html>
<html lang="pt-br">

<head>
    <meta charset="utf-8">
    <title>cadastro de tarefas</title>
</head>

<body>
    <form method="GET" action="other-form.php">
        <fieldset>
            <legend>Crie Sua tarefa</legend>
            <p><label>tarefa</label> : <input type="text" name="tarefa" autofocus></p>
            <p><label>conclusão</label> : <input type="date" name="data"></p>
            <!-- <p><label>conclusão</label> : <input type="text" name="tab"></p> -->
        </fieldset>
        <input type="submit" value="Enviar">
        <div id="result"></div>

    </form>

    <script>
        //Usando Javascript
        function loadDoc(data) {
            //Isso é AJaxa ()
            //AJAX = Assíncrono Javascript XML.
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("result").innerHTML = this.responseText;
                }
            };
            xhttp.open("POST", "other-form.php", true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhttp.send(data);
        }
        document.getElementsByTagName("form")[0].onsubmit = function() {
            let tarefa = this.elements.tarefa.value;
            let data = this.elements.data.value;
            loadDoc(`tarefa=${tarefa}&data=${data}`);
            return false;
        }
    </script>
</body>

</html>

Second Archive:

other-form.php

<table id="tab" border>
    <?php

    $dat = isset($_POST["data"]) ? $_POST["data"] : "15/10/2019";

    $tar = isset($_POST["tarefa"]) ? $_POST['tarefa'] : "não informado";
    // $tab=$_GET["tab"];

    echo "<tr><td>$tar</td><td>até $dat</td></tr>";

    ?>
</table>

REFERENCE:
1. https://www.w3schools.com/js/js_ajax_intro.asp
2. https://www.w3schools.com/xml/xml_http.asp

  • I’ll see about AJAX, but I was able to resolve it ,I don’t know if in the right way. I switched to the POST method and deleted the action and is now giving the result I wanted, the more I liked AJAX, I will try to include in the project. Thank you!!

0

First to send the data to another page you used the method POST, besides that you don’t need the HTML code in the other file, you just need to receive and treat the data the way you want ( don’t know what you’ll want to do because you didn’t put the question ) and just redirect the user to your index and present an error or success message, for this you can use the $_SESSION PHP and obviously a header("Location: ") at the end to redirect your page. You can look here to learn more about: https://www.php.net/manual/en/reserved.variables.session.php . Another option, as Taffarel commented ai, is to use XML Httprequest, but which is mostly used in AJAX form along with jQuery nowadays, you can look here to learn more about it too: https://www.w3schools.com/php/php_ajax_php.asp .

  • I was able to solve it! I don’t know if I switched to the POST method and deleted the Action form. and is doing what I want, but I saw a little now on youtube about AJAX and found it interesting, I will try to do with it, Thank you!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.