0

I need to setup this website to A(save the input to a dataset) and B(display the immediate input to the same page. I got it all set up, but everytime I click submit, my page displays my .php file. How do I fix this?

    <!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="utf-8" />
    <title>Newsletter Signup</title>
    <link rel="stylesheet" type="text/css" href="style.css" />

    <script language="javascript">
        function display() {
            var name = document.getElementById("name").value;          
            var email = document.getElementById("email").value;
            var result = name + '! We will start sending our newsletter to ' + email;
            document.getElementById('spanResult').textContent = result;
             
        }
    </script>
</head>

<body>
    <div id="body">
        <form id="Signup" action="membersList.php" method='post'>
            <h2>Sign-Up</h2>
            <br />
            Name: <input type="text" name="name" id="name">
<br />
            E-mail: <input type="text" name="email" id="email">
            <br />

             <input type="submit" name="submit" onclick="display()" value="submit"/>
            
        </form>
        <br />
        <h3>Welcome </h3> <span id='spanResult'></span>
       
    </div>
</body>
</html>

my php file

<?php
if(isset($_POST['submit']))
{
    $xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><member></member>");

    $xml->addChild('name', $_POST['name']);
    $xml->addChild('email', $_POST['email']);

    $asXML = $xml->asXML();
    $file = fopen("data.xml","w+");
    fwrite($file,$asXML);
    fclose($file);
    print_r(error_get_last());

    if(file_exists('./data.xml'))
    {
        $myXML = file_get_contents('./data.xml');
        $xml = new SimpleXMLElement($myXML);
        $xmlpretty = $xml->asXML();

        // pretty print the XML in browser
        header('content-type: text/xml');
        echo $xmlpretty;
    }

}
?>

Any sort of advice to improve the code will also be appreciated.

3
  • Is PHP installed on your server? Commented Sep 23, 2020 at 22:56
  • What server are you running this on? It sounds like it isn't setup to correctly process .php files. Commented Sep 23, 2020 at 22:57
  • if you see the literal PHP code on the screen, then it means your webserver isn't setup to execute PHP properly. Either that or you didn't access it correctly (e.g. you used file:// or something to load the HTML page). If the PHP is executing correctly, you should see some XML instead. Commented Sep 23, 2020 at 22:57

1 Answer 1

1

If the code of the PHP file is shown, normally is because your web server is not processing the PHP code. It interprets it as text.

Try creating a info.php file in the root of your web directory, with this content and check if is correctly rendered:

<?php phpinfo(); ?>

Then call it with http://myserver/info.php It should display a long page with information about your php environment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.