在WordPress中提交表单时,$_POST返回为空

4

我有一个表单:

<form action="<?php echo the_permalink(); ?>" method="POST">
    <input type="text" name="name" value="" placeholder="Your First and Last Name *" />
    <?php echo $firstnameError; ?>
    <input type="text" name="email" value="" placeholder="Yoyr Email" />
    <?php echo $emailError; ?>
    <br>
    <input type="text" name="company" value="" placeholder="Your Company Name" />
    <input type="text" name="phone" value="" placeholder="Your Phone number" />
    <textarea name="project" rows="4" cols="50" placeholder="Describe the scope of work and the most important features of the project *'"></textarea>
    <?php echo $addressError; ?>
    <br>
    <input type="radio" name="budget" value="1500" />
    <input type="radio" name="budget" value="2500" />
    <input type="radio" name="budget" value="5000" />
    <input type="radio" name="budget" value="10000" />
    <input type="radio" name="budget" value="100001" />
    <input type="radio" name="budget" value="not sure" />
    <input type="hidden" name="submit" value="1" />
    <input type="submit" value="SUbmit" />
</form>

这段代码的作用是跳转到同一页,但当我执行 print_r($_POST); 时,没有任何输出,也就是在 $_POST 中没有值。

可能的原因有哪些?我在SO上研究了一些相关问题,但没有找到想要的答案。


您在action属性中只是忘记了一个i(应该是'action'而不是'acton')。 - JoDev
@JoDev 那是一个打字错误。 - Engineer
5
Stackoverflow非官方规则第一条:不要手打问题,一定要复制/粘贴。我再强调一遍,一定要复制/粘贴。 - itachi
5个回答

5
如果您将名称作为Post值传递,WordPress不会喜欢这样做!请改成:
<input type="text" name="name" value="" placeholder="Your First and Last Name *" />

to

<input type="text" name="thename" value="" placeholder="Your First and Last Name *" />

name 改为 thename,保证能正常工作! ;)


优秀的 ;) WordPress 不喜欢传递名称值,使用自己的命名约定总是很好的做法。M - Marty
在我的情况下,这是解决方案,但我使用的不是“name”,而是“id”作为标识符。 - Matt

4
<form action="<?php the_permalink(); ?>" method="POST">

你不需要使用the_permalink()函数进行回显。

对我来说,这个方法是有效的:

<?php print_r($_POST);?>
<form action="" method="POST">
  <input type="text" name="name" value="" placeholder="Your First and Last Name *" /><?php echo $firstnameError; ?><input type="text" name="email" value="" placeholder="Yoyr Email"/><?php echo $emailError; ?><br>
  <input type="text" name="company" value="" placeholder="Your Company Name"/><input type="text" name="phone" value="" placeholder="Your Phone number"/>
  <textarea name="project" rows="4" cols="50"placeholder="Describe the scope of work and the most important features of the project *'"></textarea><?php echo $addressError; ?><br>
  <input type="radio" name="budget" value="1500" /><input type="radio" name="budget" value="2500" /><input type="radio" name="budget" value="5000" /><input type="radio" name="budget" value="10000" /><input type="radio" name="budget" value="100001" /><input type="radio" name="budget" value="not sure" />
  <input type="hidden" name="submit"value="1"/>
  <input type="submit" value="SUbmit" />
</form>

仍然一样...$_POST中没有结果。 - Engineer
将输入变量的名称更改为独特的名称。有时会与WordPress的保留名称冲突。 - RRikesh
我不知道为什么,但它对我不起作用!我知道这很基础,但就是无法让它工作! - Engineer
你改变了 name 属性吗? - RRikesh
http://wordpress.stackexchange.com/questions/25084/is-name-a-reserved-word-in-urls - RRikesh
我必须按照你说的方式编写我的表单。在action属性内没有任何内容。 - Pablo S G Pacheco

3

更改这个

acton="<?php echo the_permalink(); ?>"

to

action="<?php echo the_permalink(); ?>"

那是一个打字错误...实际上应该是表单中的操作。 - Engineer
1
你不需要回显 the_permalink() - RRikesh

0

我在我的项目中遇到了同样的问题,解决方案在于使用 $_POST,也许你在代码中使用了小写字母,请将其改为大写字母。 将 $_post 改为 $_POST!


-3

解决方案:

由于请求问题,在WordPress网站中应使用

<form action="http://example.com/">...

您可能需要指定 .php 文件.. 例如:
<form action="http://example.com/index.php">...  


//////p.s. echo会自动执行the_permalink() [同样是:echo get_permalink()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接