简体   繁体   中英

Unable To echo an HTML <form></form> within PHP

I am trying to echo an HTML form within PHP but I just can't get it done. It just echo pre-formatted HTML. I ain't getting the form.

Here is my PHP script
do-reset.php

<?php

require_once 'connect.php';

session_start();

if($_SERVER['REQUEST_METHOD'] === 'GET') {


if(isset($_GET['email']) && !empty($_GET['email']) && isset($_GET['hash']) && !empty($_GET['hash'])) {

    $email = htmlentities(mysqli_real_escape_string($connection, trim($_GET['email'])));
    $hash = htmlentities(mysqli_real_escape_string($connection, trim($_GET['hash'])));

    $search_query = "SELECT email, hash, status FROM users WHERE email = '{$email}' AND forgot_password_hash = '{$hash}' AND
               status = '1'";

    $do_search_query = mysqli_query($connection, $search_query);

    if($do_search_query) {

        $count_rows = mysqli_num_rows($do_search_query);

        if($count_rows > 0) {

            $_SESSION['email'] = $email;
            $_SESSION['hash'] = $hash;


            echo "<form method='post' action='do-reset.php'><input type='password' name='password'><br><input type='submit' value='Reset My Password'></form>";


        }
        else {
            $data = array("result" => -3, "message" => "Invalid URL");
        }


    }
    else {
        $data = array("result" => -2, "message" => "Something Went Wrong! Try Again Later.");
    }
}
else
{
    $data = array("result" => -1, "message" => "Certain Request Parameters Are Missing!");
}

}
else {
 $data = array("result" => 0, "message" => "Incorrect Request Method!");   
}



mysqli_close($connection);
/* JSON Response */
header('Content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);



?>

在此处输入图片说明

1.: Remove the header('Content-type: application/json'); This will basically tell the browser to display the output as text.

2.: to preserve formatting, you can use <pre> -tags:

echo "<pre>";
echo json_encode($data, JSON_PRETTY_PRINT);`
echo "</pre>";

Different approach: only set content type to application/json when the $data -array is filled

if(!empty($data)){
    header('Content-type: application/json');
    echo json_encode($data, JSON_PRETTY_PRINT);
}

I found a way out! $type_json did the trick for me.

    <?php

require_once 'connect.php';

session_start();

$type_json = true;

if($_SERVER['REQUEST_METHOD'] === 'GET') {


    if(isset($_GET['email']) && !empty($_GET['email']) && isset($_GET['hash']) && !empty($_GET['hash'])) {

        $email = htmlentities(mysqli_real_escape_string($connection, trim($_GET['email'])));
        $hash = htmlentities(mysqli_real_escape_string($connection, trim($_GET['hash'])));

        $search_query = "SELECT email, hash, status FROM users WHERE email = '{$email}' AND forgot_password_hash = '{$hash}' AND
                   status = '1'";

        $do_search_query = mysqli_query($connection, $search_query);

        if($do_search_query) {

            $count_rows = mysqli_num_rows($do_search_query);

            if($count_rows > 0) {

                $_SESSION['email'] = $email;
                $_SESSION['hash'] = $hash;

                $type_json = false;

                echo "<form method='post' action='do-reset.php'><input type='password' name='password'><br><input type='submit' value='Reset My Password'></form>";


            }
            else {
                $data = array("result" => -3, "message" => "Invalid URL");
            }


        }
        else {
            $data = array("result" => -2, "message" => "Something Went Wrong! Try Again Later.");
        }
    }
    else
    {
        $data = array("result" => -1, "message" => "Certain Request Parameters Are Missing!");
    }

}
else {
    $data = array("result" => 0, "message" => "Incorrect Request Method!");   
}


mysqli_close($connection);
/* JSON Response */

if($type_json) {
    header('Content-type: application/json');
    echo json_encode($data, JSON_PRETTY_PRINT);
}


?>

PHP is executed on the server side and is treated as a script instead of a markup language, meaning the HTML on the requested page doesnt matter to the server at all it only cares about the PHP. so if you did

<?php
if(true) {
    ?>
    <form>Hello</form>
    <?php
}
?>

the html form will only be displayed with the text hello if the statement is true, which true always is true ofcourse. you could replace this with any statement, for example to check if someone hes entered something in a field from the form they submitted.

hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM