0

I'm new on PHP and I want to know if it's possible to write PHP code that's inside HTML, which the last one is inside PHP.

For example in the following code:

$bodyHtml = '
    <div class="container">
        <img src="' . $bodyBackground . '" class="imgBg"/>
        <form method="POST" action="mapa-cardio.php" style="top:150px">         
            <input type="text" name="name" value="'. $name . '" class="transparent-input font-size-name" style="top: 181px;left: 54px;width: 294px;height: 28px;">
            <input type="text" name="data1" value="'. $data1 . '" class="transparent-input font-size-name" maxlength="2" style="top: 181px;left: 468px;width: 19px;height: 28px;">
            <input type="text" name="data2" value="'. $data2 . '" class="transparent-input font-size-name" maxlength="2" style="top: 181px;left: 494px;width: 19px;height: 28px;">
            <input type="text" name="data3" value="'. $data3 . '" class="transparent-input font-size-name" maxlength="4" style="top: 181px;left: 521px;width: 33px;height: 28px;">
            <!-- DADOS ANTROPOMÉTRICOS -->
            <input type="text" name="altura" value="'. $altura . '" class="font-size" style="top: 243px;left: 26px;width: 26px;height: 10px;">
            <input type="text" name="peso" value="'. $peso . '" class="font-size" style="top: 267px;left: 26px;width: 26px;height: 10px;">
            <input type="text" name="perimetroAbdominal" value="'. $perimetroAbdominal . '" class="font-size" style="top: 291px;left: 26px;width: 26px;height: 10px;">
            <!-- HÁBITOS -->
            'if( isset($_POST['tabagismo']) ) {
                $bodyHtml .= '<img src="' . $checkImage . '" class="check" style="top: 181px;left: 54px;width: 15px;height: 15px;">';
            }'

I want to add that php if line to that HTML that's concatenated inside PHP variable, is it possible? I get the following error: PHP Parse error: syntax error, unexpected 'if' (T_IF)

Any help/advices will be greatly appreciated.

  • See [PHP parse/syntax errors; and how to solve them](https://stackoverflow.com/q/18050071/9193372). – Syscall Mar 02 '21 at 08:29

1 Answers1

1

u need to close the first one

$bodyHtml = '
    <div class="container">
        <img src="' . $bodyBackground . '" class="imgBg"/>
        <form method="POST" action="mapa-cardio.php" style="top:150px">         
            <input type="text" name="name" value="'. $name . '" class="transparent-input font-size-name" style="top: 181px;left: 54px;width: 294px;height: 28px;">
            <input type="text" name="data1" value="'. $data1 . '" class="transparent-input font-size-name" maxlength="2" style="top: 181px;left: 468px;width: 19px;height: 28px;">
            <input type="text" name="data2" value="'. $data2 . '" class="transparent-input font-size-name" maxlength="2" style="top: 181px;left: 494px;width: 19px;height: 28px;">
            <input type="text" name="data3" value="'. $data3 . '" class="transparent-input font-size-name" maxlength="4" style="top: 181px;left: 521px;width: 33px;height: 28px;">
            <!-- DADOS ANTROPOMÉTRICOS -->
            <input type="text" name="altura" value="'. $altura . '" class="font-size" style="top: 243px;left: 26px;width: 26px;height: 10px;">
            <input type="text" name="peso" value="'. $peso . '" class="font-size" style="top: 267px;left: 26px;width: 26px;height: 10px;">
            <input type="text" name="perimetroAbdominal" value="'. $perimetroAbdominal . '" class="font-size" style="top: 291px;left: 26px;width: 26px;height: 10px;">';
            <!-- HÁBITOS -->
            if( isset($_POST['tabagismo']) ) {
                $bodyHtml .= '<img src="' . $checkImage . '" class="check" style="top: 181px;left: 54px;width: 15px;height: 15px;">';
            }
Delete
  • 902
  • 7
  • 8
  • Thanks it worked!!!! – João Moreira Mar 01 '21 at 21:53
  • Questions that are about basic PHP syntax errors should not be answered. They should be closed as a duplicate of [PHP Parse/Syntax Errors; and How to solve them?](//stackoverflow.com/questions/18050071) only. If you want to point out their mistake, just post a comment. See: [Should one advise on off topic questions?](//meta.stackoverflow.com/q/276572/1768232). Off-topic questions can be closed and deleted, which could nullify your contribution anyway. – John Conde Mar 02 '21 at 12:26