douyu0852 2019-07-01 20:52
浏览 122
已采纳

对于最终有表单的单页网站:我是否必须将index.html转换为index.php?

I am working on my first Website! Its a Single Page Design with a simple contact form (two input fields, a textarea and a checkbox and submit button) at the end of the page! The page is ready... the only thing that´s missing is the PHP for the contact form. When the contact form is submitted I would like to show a message (message delivered) on the same page! My Question: For this purpose do I need to convert the whole index.html to index.php ?? It seems to be the only way for the action attribute of the form to stay on the same page! Or is it better to add also Ajax for this purpose? All tutorials use the index.php...

Please help!

  • 写回答

2条回答 默认 最新

  • dqsk4643 2019-07-01 20:58
    关注

    I would say yes, make it a .php file. A PHP file can have plain HTML in it. Your index.php could look something like this:

    <?php
    if ( isset( $_POST['submitted'] ) ) {
        $message = 'Your email has been sent!';
        // Process your form and send email here
    }
    ?>
    <html>
        <head>
            ...
        </head>
        <body>
            <?php
                if ( $message ) {
                    echo "<div class='message'>$message</div>";
                }
            ?>
            <!--The rest of your body...-->
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?