简体   繁体   English

如何在 WordPress 中创建自定义表单?

[英]How can I create a custom form in WordPress?

I have a WordPress site and would like to create a form (input → database).我有一个 WordPress 网站,想创建一个表单(输入 → 数据库)。

I've seen two tutorials:我看过两个教程:

They're both very similar.他们都非常相似。 Creating the front end with HTML and JavaScript, and then processing the information to the database using PHP.使用 HTML 和 JavaScript 创建前端,然后使用 PHP 处理信息到数据库。

My problem is that whenever I submit the form, it shows a 404 page which says:我的问题是,每当我提交表单时,它都会显示一个 404 页面,上面写着:

Oops!哎呀! That page can't be found.找不到那个页面。

Now my problem is (or I want to know):现在我的问题是(或者我想知道):

  1. Where do I need to put the process.php file?我需要把process.php文件放在哪里? (I'm using FileZilla ). (我正在使用FileZilla )。 I've tried several places in the public_html/wp-content folder.我在public_html/wp-content文件夹中尝试了几个地方。

  2. Why aren't the name and email fields being validated?为什么不验证姓名和电子邮件字段? (no alert for empty name field, etc.) (空名称字段等没有警报)

Form structure:表格结构:

Your Name: [TEXT], Your Email: [TEXT], Sex: [*male *female], Your Age:[TEXT], [Submit Button]您的姓名:[TEXT],您的电子邮件:[TEXT],性别:[*男 *女],您的年龄:[TEXT],[提交按钮]

Form page:表格页面:

<form name="myForm" method="POST" onsubmit="return form_validation()" action="../process.php">
    Your Name: <input id="customer_name" name="customer_name" type="text" />
    Your Email: <input id="customer_email" name="customer_email" type="text" />
    Sex: <input name="customer_sex" type="radio" value="male" />Male <input name="customer_sex" type="radio" value="female" />Female
    Your Age: <input id="customer_age" name="customer_age" type="text" />
    <input type="submit" value="Submit" />
</form>

<script type="text/javascript">
    function form_validation() {
        /* Check the Customer Name for blank submission */
        var customer_name = document.forms["myForm"]["customer_name"].value;
        if (customer_name == "" || customer_name == null) {
            alert("Name field must be filled.");
            return false;
        }

        /* Check the Customer Email for invalid format */
        var customer_email = document.forms["myForm"]["customer_email"].value;
        var at_position = customer_email.indexOf("@");
        var dot_position = customer_email.lastIndexOf(".");
        if (at_position < 1 || dot_position < at_position + 2 || dot_position + 2 >= customer_email.length) {
            alert("Given email address is not valid.");
            return false;
        }
    }
</script>

File process.php (not edited):文件process.php (未编辑):

<?php
    $customer_name = $_POST["customer_name"];
    $customer_email = $_POST["customer_email"];
    $customer_sex = $_POST["customer_sex"];
    $customer_age = $_POST["customer_age"];

    $conn = mysqli_connect("Database Host", "Database Username", "Database  Password", "Database Name");
    if(!$conn) {
        die(‘Problem in database connection: ‘ . mysql_error());
    }

    $query = "INSERT INTO ‘Database Name’.’Table Name’ ( ‘customer_name’, ‘customer_email’, ‘customer_sex’, ‘customer_age’ ) VALUES ( $customer_name, $customer_email, $customer_sex, $customer_age )";
    mysqli_query($conn, $query);

    header("Location: my-site.com/success"); // Redirects to success page
?>

To answer question one: WordPress provides action and filter hooks for developers to add their custom PHP code or functions.回答问题一:WordPress 为开发人员提供了 action 和 filter hook,以添加他们的自定义 PHP 代码或函数。 You should look into that, because using plugins that produce snippets will not help in your case, because it causes your PHP code to execute without even loading your form, so you will be seeing your success page instead.您应该考虑一下,因为使用生成片段的插件对您的情况没有帮助,因为它会导致您的 PHP 代码在不加载表单的情况下执行,因此您将看到您的成功页面。

To learn more about action and filter hooks visit here .要了解有关操作和过滤器挂钩的更多信息,请访问此处

Alternatively to us action/filter hooks, you can upload your PHP file into the theme folder.除了我们的 action/filter hooks,您还可以将 PHP 文件上传到主题文件夹中。 However, there's a flaw to that.然而,这有一个缺陷。 Your file may be lost when WordPress updates.当 WordPress 更新时,您的文件可能会丢失。


To answer question two: There is an easier way to validate your form if using JavaScript.回答问题二:如果使用 JavaScript,有一种更简单的方法来验证您的表单。 All you need to do is add the word 'required' within the input tag.您需要做的就是在输入标签中添加“必需”一词。 You can also use input type 'email' together with the required keyword to validate you email.您还可以使用输入类型“电子邮件”和所需的关键字来验证您的电子邮件。 See the example below.请参阅下面的示例。

<form name="myForm" method="POST" action="../process.php">
    Your Name: <input id="customer_name" name="customer_name" type="text" required/>
    Your Email: <input id="customer_email" name="customer_email" type="email" required/>
    Sex: <input name="customer_sex" type="radio" value="male" />Male <input name="customer_sex" type="radio" value="female" />Female
    Your Age: <input id="customer_age" name="customer_age" type="text" />
    <input type="submit" value="Submit" />
</form>

If you still want to use your JavaScript function, try using document.getElementById('customer_name') and document.getElementById('customer_email') instead of document.forms .如果您仍想使用 JavaScript 函数,请尝试使用document.getElementById('customer_name')document.getElementById('customer_email')而不是document.forms Also make sure you close your script tag at the end.还要确保在最后关闭脚本标记。 See below for an example.请参阅下面的示例。

<script type="text/javascript">
    function form_validation() {
        /* Check the Customer Name for blank submission */
        var customer_name = document.getElementById('customer_name').value;
        if (customer_name == "" || customer_name == null) {
            alert("Name field must be filled.");
            return false;
        }

        /* Check the Customer Email for invalid format */
        var customer_email = document.getElementById('customer_email').value;
        var at_position = customer_email.indexOf("@");
        var dot_position = customer_email.lastIndexOf(".");
        if (at_position < 1 ||
            dot_position < at_position + 2 ||
            dot_position + 2 >= customer_email.length) {

            alert("Given email address is not valid.");
            return false;
        }
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM