我正在使用自定义 php 页面模板向我的 WordPress 添加表单
下面是我的 WordPress 页面模板 php 代码:
<?php
/*
Template Name: Form
*/
?>
<?php get_header(); ?>
<div id="content">
<h1 class="page-title"><?php the_title(); ?></h1>
<div class="entry">
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.
</div> <!--end .entry-->
</div> <!--end #content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
我在我的页面中添加了以下显示表单的代码
<form action="http://127.0.0.1:4001/wordpress/script" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
这将在新页面中显示表单数据,而不是在同一页面上显示结果。 如何在同一页面上显示结果?
我想要这样的东西:
最佳答案
从表单
中删除action
属性:
<form method="post">
更新
现在,要在表单提交后保留字段中的数据,只需将它们打印在各自的字段上即可:
Name: <input type="text" name="fname" value="<?php if (isset($_POST["fname"])) echo $_POST["fname"]; ?>" />
Age: <input type="text" name="age" value="<?php if (isset($_POST["age"])) echo $_POST["age"]; ?>" />
关于WordPress 的 php 表单处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17840640/