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

我正在我的第一个网站上工作!它是一个单页设计,在页面末尾有一个简单的联系表单(两个输入字段、一个文本区域和一个复选框和提交按钮)!页面已准备就绪……唯一缺少的是联系表单的 PHP。当提交联系表单时,我想在同一页面上显示一条消息(已发送消息)!我的问题:为此我需要将整个 index.html 转换为 index.php 吗?这似乎是表单的action属性保持在同一页面的唯一途径!或者最好为此目的还添加 Ajax?所有教程都使用 index.php...



侃侃尔雅
浏览 216回答 2
2回答

慕慕森

我会说是的,让它成为一个 .php 文件。PHP 文件中可以包含纯 HTML。您的 index.php 可能如下所示:<?phpif ( isset( $_POST['submitted'] ) ) {&nbsp; &nbsp; $message = 'Your email has been sent!';&nbsp; &nbsp; // Process your form and send email here}?><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $message ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<div class='message'>$message</div>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <!--The rest of your body...-->&nbsp; &nbsp; </body></html>

潇湘沐

当您在页面中用 PHP 语言编写一行时,您必须将 page.html 转换为 page.php显示消息尝试使用这个echo&nbsp;"<script>&nbsp;alert('message&nbsp;delivered&nbsp;');&nbsp;window.location.href='index.php';</script>"&nbsp;;
打开App,查看更多内容
随时随地看视频慕课网APP