php 使用 WordPress 后端从表单显示 POST 数据的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10610277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:35:56  来源:igfitidea点击:

Easiest way to display POST data from form, with WordPress backend?

phpformswordpresswordpress-plugin

提问by Ryan

I have a standard HTML form on a WordPress post that the visitor will fill out. It consists of mostly checkboxes. What I would like, is after the form is submitted, a page is then presented to the user showing what they checked. For example, if the visitor checked off Checkbox A, B, and E, (but not C and D) then upon submission they would see the following:

我在 WordPress 帖子上有一个标准的 HTML 表单,访问者将填写该表单。它主要由复选框组成。我想要的是在提交表单后,向用户呈现一个页面,显示他们检查的内容。例如,如果访问者勾选了复选框 A、B 和 E(而不是 C 和 D),那么在提交时,他们将看到以下内容:

You submitted:
Checkbox Value A
Checkbox Value B
Checkbox Value E

Obviously I can do this with just PHP, but my client would like to be able to modify the form options. Therefore I will need an easy to use backend.

显然我可以只用 PHP 来做到这一点,但我的客户希望能够修改表单选项。因此,我需要一个易于使用的后端。

Is there a plugin or user friendly way that this can be created? Or is my best bet to start building it myself?

是否有可以创建的插件或用户友好的方式?或者是我自己开始构建它的最佳选择?

回答by Taha Paksu

the easiest way would be:

最简单的方法是:

<pre>
<?php var_dump($_POST);?>
</pre>

but you can style it like:

但您可以将其样式设置为:

<?php
    foreach($_POST as $key=>$post_data){
        echo "You posted:" . $key . " = " . $post_data . "<br>";
    }
?>

Notethat this might give errors if the $post_datatype can't be displayed as a string, like an array in the POST data. This can be extended to:

请注意,如果$post_data类型无法显示为字符串(如 POST 数据中的数组),则这可能会产生错误。这可以扩展为:

<?php
    foreach($_POST as $key=>$post_data){
        if(is_array($post_data)){
            echo "You posted:" . $key . " = " . print_r($post_data, true) . "<br>";
        } else {
            echo "You posted:" . $key . " = " . $post_data . "<br>";
        }
    }
?>   

回答by Joelmob

You can do something in jQuery.

你可以在 jQuery 中做一些事情。

$.post(
    "post.php",
    {checkbox : 1, checkbox2 : 0, checkbox3 : 0}, // this is the body of the post request
    function(data) {
       alert('page content: ' + data); // look up values in checkboxes and display them
    }
);

Im not sure what kind of data wordpress needs, it might also need some session id to be sent if you need the user to be logged in.

我不确定 wordpress 需要什么样的数据,如果您需要用户登录,它可能还需要发送一些会话 ID。

回答by loretoparisi

If you wanna to show a json encoded list of the POSTarray is simple as

如果你想显示POST数组的 json 编码列表很简单

<?php echo json_encode($_POST);?>