-2

This is a simple question, I need to return an html <form> using a php echo''; I can perfectly return the function by typing:

echo'
<form method="post" action="passwordResetUser.php">
    <input type="password" type="password" name="passwordResetUser" placeholder="Select new password" class="form-control1">
    <input type="password" type="password" name="passwordResetUserRetype" placeholder="Retype the new password" class="form-control1">
              
    <button type="submit" name="rP006" >Reset my password.</button>
</form>';

But I would like to include <?php include('errors.php'); ?> before the first <form> so the result would be like this:

echo'
<form method="post" action="passwordResetUser.php">
    <?php include('errors.php'); ?>
    <input type="password" type="password" name="passwordResetUser" placeholder="Select new password" class="form-control1">
    <input type="password" type="password" name="passwordResetUserRetype" placeholder="Retype the new password" class="form-control1">
              
    <button type="submit" name="rP006" >Reset my password.</button>
</form>';

But that just throws an error, to point out that I am learning PHP by myself and those things got me crazy. Thanks in advance and sorry for maybe the obnoxious question.

:)

jhonroger
  • 37
  • 8
  • You can't throw PHP constructs in the middle of an echo. Break out of it, include the errors, then restart the echo. Or don't even echo it out. End the PHP block before where the echo is, print the HTML normally, then you can use the PHP include as you have it. – aynber Apr 13 '21 at 14:22
  • 1
    "*But that just throws an error*" and the error is? – biesior Apr 13 '21 at 14:24
  • BTW while you're echoing something that means you already within PHP block, why do you open it again? – biesior Apr 13 '21 at 14:25
  • 1
    Also please [read how to use variables within strings in PHP](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – biesior Apr 13 '21 at 14:28
  • @biesior 503 internal server error :) – jhonroger Apr 13 '21 at 14:28

1 Answers1

0

You can do this with ob_start, something like that:

ob_start();
include('errors.php');
$errors = ob_get_contents();
ob_end_clean();

echo'
<form method="post" action="passwordResetUser.php">
    ' . $errors . '
    <input type="password" type="password" name="passwordResetUser" placeholder="Select new password" class="form-control1">
    <input type="password" type="password" name="passwordResetUserRetype" placeholder="Retype the new password" class="form-control1">
              
    <button type="submit" name="rP006" >Reset my password.</button>
</form>';
Giacomo M
  • 4,450
  • 7
  • 28
  • 57