I'm new to php and im running xampp and the apache server is running, i created an html (phptext.html) form below which
inputs the user's name and sends it to a php file called phptext.php.

now the problem i'm getting is the html form sends data to the php file but my browser does
not display the user's input. it only displays the text String "Thanks for Answering,".

i got error reporting on and it does not show me any errors.
So i dont know what seems to be the problem, if its my xammp installation how can i troubleshoot it.

<!-- html file -->

<html>
    <head>
        <title>Entering Data into text fields</title>
    </head>
    <body>
        <h1>Entering Data into text fields</h1>
        <form method="get" action="phptext.php">
            whats your Name?
            <input name="data" type="text"  />
            <input type="submit" value="Send" />
        </form>
    </body>

</html>




<!-- php file -->

<html>
<head>
<title>Reading Data from text fields</title>
</head>

<body>
    <h1>Reading Data from text fields</h1>
    Thanks for Answering,
    <?php
        echo $_REQUEST["data"];
    ?>
</body>
</html>

try this in stead

<!-- html file -->
<html>
    <head>
        <title>Entering Data into text fields</title>
    </head>
    <body>
        <h1>Entering Data into text fields</h1>
        <form method="post" action="phptext.php">
            whats your Name?
            <input name="data" type="text"  />
            <input type="submit" value="Send" />
        </form>
    </body>
</html>



<!-- php file -->
<html>
<head>
<title>Reading Data from text fields</title>
</head>
<body>
    <h1>Reading Data from text fields</h1>
    Thanks for Answering,
    <?php
        $data=$_POST['data'];
        echo '$data';
    ?>
</body>
</html>

Good practice when working with processing forms is to check if the value has been entered first and then also clean the input using htmlspecialchars function (i.e. to prevent nasty users to inject scripts or html code in input box):

<!-- php file -->
<html>
<head>
<title>Reading Data from text fields</title>
</head>
<body>
<h1>Reading Data from text fields</h1>

<?php
if(isset($_POST['data']) && !empty($_POST['data'])) {
    // clean the input so it is safe to be inserted in html code
    $data = htmlspecialchars($_POST['data']);
    echo 'Thanks for Answering, ' . $data;
} else {
    echo 'You did not input anything!';
}
?>
</body>
</html>

To debug put this on first line of PHP code:

die(print_r($_POST, 1));

It will display all the values that have been sent over in a $_POST array and stop execution of script so you can do examination.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.