CodeTalker0110
CodeTalker0110

Reputation: 1

Why does the submit button display my php file?

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.

Upvotes: 0

Views: 937

Answers (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.

Upvotes: 1

Related Questions