wordpress 表单提交和 404 错误页面 [英] wordpress form submission and the 404 error page

查看:23
本文介绍了wordpress 表单提交和 404 错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 wordpress 模板页面上有以下表单.每次提交表单时我都会收到 404 错误,但我没有在表单中使用任何保留的 workpress 参数名称.

I have the following form on a wordpress template page. I'm getting a 404 error each time i submit the form but I'm not using any of the reserved workpress parameter names in the form.

<?php
/**
 * Template Name: Registration Template
 */
if(isset($_POST['form-submitted'])) 
{
    if(trim($_POST['runner']) === '') {
        $runnerError = 'Please enter runner runner.';
        $hasError = true;
    } else {
        $runner = trim($_POST['runner']);
    }

    if(trim($_POST['racenumber']) === '')  {
        $numberError = 'Please enter a race number.';
        $hasError = true;
    } else {
        $racenumber = trim($_POST['racenumber']);
    }

    $race = trim($_POST['race']);
    error_log($race.' '.$runner.' '.$racenumber);
    $registrationSubmitted = true;
}
get_header();
echo "<pre>GET "; print_r($_GET); echo "</pre>";
echo "<pre>POST "; print_r($_POST); echo "</pre>";
?>

<div id="container">

    <?php 
    if(isset($registrationSubmitted) && $registrationSubmitted == true) 
    {
        echo '<div class="thanks"><p>The runner has been registered.</p></div>';
    }
    else
    {
        $races = // magic array
        $selectRaces = '<select name="race" id="race">';
        foreach($races as $racez)
        {
            $selectRaces .= sprintf('<option value=%d>%s</option>',$race->id,$race->name);
        }
        $selectRaces .= '</select>';

        echo apply_filters('the_content','
            <form action="'.get_permalink().'" id="form" method="POST">
                [one_half last="no"]
                <b>Race Details</b><br/>
                RaceNumber<input type="text" name="racenumber" id="racenumber"/><br/>
                Race'.$selectRaces.'<br/>
                [/one_half]
                [one_half last="yes"]
                <b>Runner Details</b><br/>
                ID<input type="text" name="runner" id="runner"/><br/>
                Firstname<input type="text" name="first" id="first"/><br/>
                Surname<input type="text" name="last" id="last"/><br/>
                Gender<input type="text" name="gender" id="gender"/><br/>
                DOB<input type="text" name="dob" id="dob"/><br/>
                Standard<input type="text" name="standard" id="standard"/><br/>
                Company<input type="text" name="company" id="company"/><br/>
                [/one_half]
                <input type="submit" value="Register Runner"/>
                <input type="hidden" name="form-submitted" id="bhaa-submitted" value="true" />
            </form>');
    }
    echo '</div>';
?>
<?php get_footer(); ?>

我已经自定义了我的 404 页面以转储 $_POST 值,因此我确定正在提交参数值.

I've customised my 404 page to dump the $_POST values so i'm sure the parameter values are being submitted.

[racenumber] => 5
[race] => 2596
[runner] => 5
[first] => 
[last] => 
[gender] => 
[dob] => 
[standard] => 
[company] => 
[form-submitted] => true

谁能解释一下我的isset($_POST['form-submitted'])"中的逻辑没有被执行?

Can anyone explain my the logic in my 'isset($_POST['form-submitted'])' is not being exercised?

生成的html

<form action="http://localhost/registration/" id="form" method="POST">
<div class="one_half">
                <b>Race Details</b><br><br>
                RaceNumber<input name="number" id="number" type="text"><br><br>
                Race<br>
<select name="race" id="race">
<option value="2596" id="2596">4-Mile-M</option>
<option value="2595" id="2595">2-Mile-W</option>
</select>

编辑

我更改了设置选择下拉列表值的代码,以使用递增的 int 值,而不是使用 sprintf 将字符串值映射到 int 值.When the first element is selected the form submits, if the second option is picked i get a 404!

I've changed the code where is set the values of the select dropdown to use an incrementing int value rather than using sprintf to map a string value to an int value. When the first element is selected the form submits, if the second option is picked i get a 404!

$races = // magic array
$selectRaces = '<select name="race" id="race">';
$i=0;
foreach($races as $racez)
{
    $selectRaces .= sprintf('<option value=%d>%s</option>',$i++,$race->name);
}

推荐答案

问题是 WordPress 保留了一些词,并且在提交表单时会抛出该错误:

Problem is that WordPress have some words reserved and it will throw that error when submitting forms:

我在网上发现的一些词是:

Some of the words that I found myself and surfing the web are:

  • 自定义帖子类型名称
  • 分类名称
  • 姓名"
  • 天"
  • 月"
  • 年"
  • 类别"
  • 标题"

因此在创建自定义表单时要小心,并尝试使用一些前缀命名您的输入.就我而言,我有一个名为历史"的自定义帖子类型,并且我以相同的方式命名输入.

So be careful when creating a a custom form and try to name your inputs with some prefix. In my case, I had a custom post type called "history" and I was naming the input the same way.

这篇关于wordpress 表单提交和 404 错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆