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

时间:2016-10-09 13:38:44

标签: php wordpress forms

我有一个Wordpress网站,想要创建一个表单(输入 - >数据库)。

我见过2个教程:

他们都非常相似。使用html和js创建前端,然后使用php将信息处理到数据库。

我的问题是,每当我提交表单时,它会显示一个404页面,上面写着“糟糕!无法找到该页面。”

现在我的问题是(或者我想知道):

1)我需要在哪里放置process.php文件? (我正在使用fileZilla)。 我在public_html/wp-content文件夹中尝试了几个地方。

2)为什么没有验证名称和电子邮件字段? (没有空名称字段的警告等)

谢谢,如果我错过了什么,请告诉我。)

  

表格结构:

您的姓名:[文字],您的电子邮件:[文字],性别:[*男*女],您的年龄:[文字],[提交按钮]

表单页面

<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;
    }
}

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
?>

1 个答案:

答案 0 :(得分:1)

回答问题一:WordPress为开发人员提供Action和Filter Hook,以添加他们的自定义PHP代码或函数。你应该研究一下,因为使用生成片段的插件对你的情况没有帮助,因为它可以让你的PHP在没有加载表单的情况下执行,所以你会看到你的成功页面。要详细了解操作和过滤器挂钩visit here

使用Action / Filter Hooks的替代方法,您可以将您的php文件上传到主题文件夹。然而,这有一个缺陷。 WordPress更新时,您的文件可能会丢失。

回答问题二:如果使用JavaScript,可以更轻松地验证表单。您只需在输入标记中添加“必需”一词即可。您还可以使用输入类型“email”和必需的关键字来验证您的电子邮件。见下面的例子。

<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>

如果您仍想使用JavaScript函数,请尝试使用document.getElementById('customer_name')和document.getElementById('customer_email')而不是document.forms。还要确保在最后关闭脚本标记。例如,见下文。

<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>