【问题标题】:Submiting a web form in wordpress在 wordpress 中提交 Web 表单
【发布时间】:2017-01-26 07:16:37
【问题描述】:

我是 wordpress 的新手,在处理简单的表单布局/提交时遇到了一些困难。 我有一个包含以下代码的页面(从 wp-admin 页面创建):

<div class="container-fluid">
    <form class="form-horizontal" action="" method="POST" >
        <div class="row form-group">
            <div class="col-xs-12">
                <label for="name">Nome</label>
                <input type="text" class="form-control" id="name" name="name" />
            </div>
        </div>
        <div class="row form-group">
            <div class="col-xs-12">
                <label for="residence">Residência</label>
                <input type="text" name="residence" id="residence" />
            </div>
        </div>
        <div class="row form-group">
            <div class="col-sm-6">
                <input class="btn btn-default" type="submit" value="Registar">
            </div>
        </div>
    <form>
</div>

我的问题是我不知道在 action 属性中放入什么以及之后如何处理表单。我试图在模板文件夹中创建一个 php 文件并将其放入操作中,但它根本不起作用。 我还看到了一些可以添加到 functions.php 的操作,但我不认为这是解决问题的最佳方法。我想要的只是提交表单,将其作为元数据存储在数据库中,并将用户重定向到主页。

【问题讨论】:

    标签: php wordpress forms post


    【解决方案1】:

    我的建议是使用action="#",它会在按下提交时将用户重定向到当前页面。然后,您可以检查不显示表单的隐藏字段,而是检查数据并将其存储在数据库中,然后将用户重定向到您的主页。

    您可以将此文件放在 Wordpress 安装中的任何位置(只要您的用户可以访问它)。如果可移植性不是问题,请将其放在根目录中,或者如果可移植性确实重要,请查找making a page template file

    此外,请小心使用属性name="name",因为我似乎记得这会破坏您的 Wordpress 页面处理(Wordpress 使用它来识别它所在的页面)。

    以下是相同页面处理的示例:

    <?php 
    if (isset($_POST["formSubmitted"])) {
        $name = $_POST["formName"];
        $residence = $_POST["formResidence"];
        // do database work here with $name and $residence
        // then output javascript to redirect to main page
        echo '<script>window.location = "http://example.com";</script>';
    } else {
        // we display the form
    ?>
    <div class="container-fluid">
        <form class="form-horizontal" action="#" method="POST" >
            <div class="row form-group">
                <div class="col-xs-12">
                    <label for="name">Nome</label>
                    <input type="text" class="form-control" id="name" name="formName" />
                </div>
            </div>
            <div class="row form-group">
                <div class="col-xs-12">
                    <label for="residence">Residência</label>
                    <input type="text" name="formResidence" id="residence" />
                </div>
            </div>
            <div class="row form-group">
                <div class="col-sm-6">
                    <input class="btn btn-default" type="submit" value="Registar">
                </div>
            </div>
        <input type="hidden" name="formSubmitted" value="1">
        </form>
    </div>
    <?php
    }
    ?>
    

    替代解决方案可能是使用get_stylesheet_directory_uri(),它返回主题样式表所在的目录。你可以这样使用:

    <form action="<?php echo get_stylesheet_directory_uri()."/some_php_file.php"; ?>" method="POST">
    

    然后,这会将数据传递到该文件,您可以从该文件将其添加到数据库中。

    【讨论】:

      【解决方案2】:

      如果您是初学者,为什么不直接使用联系表格 7 或重力表格?

      联系表格 7:http://contactform7.com/

      重力形式:http://www.gravityforms.com/

      【讨论】:

        猜你喜欢
        • 2019-05-20
        • 1970-01-01
        • 1970-01-01
        • 2017-12-21
        • 2013-09-17
        • 1970-01-01
        • 1970-01-01
        • 2011-07-05
        • 2017-09-12
        相关资源
        最近更新 更多