0

I am trying to add a php code in my html file, but whenever i do, my file instead of outputting my expected outcome, it instead presents the source code

again, i am creating a server for my project at school and i am a total noob in programming as i learn it all by myself without formal education about it. i tried to save the file as both .html and .php, although .html presents the output, but not the desired one. now i have two files below and since i am a noob in programming, my concept was to have users input the form in the .html file and output in on an identical one but in .php

this is the first file, saved in ABM11.html

    <form action="AMB11.php" method="post"><table border="0">
        <tr>
            <td>Name</td>
            <td align="center"><input type="text" name="StudentName" size="30"></td>
        </tr>
        <tr>
            <td>Subject</td>
            <td align="center"><input type="text" name="Subject" size="30"></td>
        </tr>
        <tr>
            <td>Final Grade</td>
            <td align="center"><input type="text" name="FinalGrade" size="30"></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="submit"></td>
        </tr>
    </table>
    </form>

this is the second file, saved as ABM11.php

<?php
$studentname = $_POST['StudentName'];
$subject = $_POST['Subject'];
$finalgrade = $_POST['FinalGrade'];

echo $studentname '<\br>';
echo $subject '<\br>';
echo $finalgrade '<\br>';
?>

basically i just want users to answer a form then have that data be posted on the same page perpetually, not have my noob source code presented

  • 3
    Possible duplicate of [PHP code is not being executed, instead code shows on the page](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – Qirel Feb 11 '19 at 10:52
  • As Johannes suggested below, you also have some syntax errors in your PHP - you're not concatenating the strings. – Qirel Feb 11 '19 at 10:58
  • How are you opening the html file? directly opening it or via browsing your web server? Check it with the url, if it's `http://` or `file:///` – Cid Feb 11 '19 at 11:47

2 Answers2

0

If you combine variables and strings in an echo, you need to add dots to connect them. So instead of this

echo $studentname '<br>';

you need to write it as

echo $studentname.'<br>';

And similar in all other lines.

(and no backslashes, by the way!)

And also ("sourcecode displayed...") make sure to open/call the php file via a server (for example XAMPP, via localhost), not simply as a file on your computer.

Johannes
  • 64,305
  • 18
  • 73
  • 130
0

it seems , the problem with your web server , weather you are using apache or nginx . its not running properly or its not configured properly on your local machine so rather then executing php code its just showing that file on the browser .

You can debug like , create one simple php file just like echo "yourname "; and put it to your www directory and try to run it form browser if the server is proper configured then it must print and if its not prited then its mistake with your local server configuration .

and also on your source code there is one mistake of echo and .

Wrong : echo $finalgrade '<\br>'; True : echo $finalgrade .'<\br>';

You must concat br with .

that is mistake 110% but as you said in your question the code is showing on the page is not only because of this , its surely configuration problem

if . is the only mistake then it wont show you the source code on the page but you will get any php error if thats the only issue .so check your config well first.

Thanks Akshay Champavat