wordpress表单调用_在WordPress中设计多页表单:多页处理

wordpress表单调用

This is part three in a series on creating a multi-page form in WordPress, complete with its own database table. I strongly recommend you skim parts one and two if you haven’t yet, if nothing else to get familiar with our methodology and format.

这是在WordPress中创建多页表单的系列文章的第三部分,并带有自己的数据库表。 我强烈建议您略过第一第二部分,如果没有其他内容,请熟悉我们的方法和格式。

In part one of the series, we created a shortcode, made a post/page for our form, and created the basic functions that we’re using for this process. In part two, we created the database table, inserted the inputs from the first page of our form, and handled the data from page two. In part three, we’re going to update (not insert) the data into the row we started for this form and show how we can make an optional version of the form based upon previous data entries.

在本系列的第一部分中,我们创建了一个简码,为表单创建了一个帖子/页面,并创建了用于此过程的基本功能。 在第二部分中,我们创建了数据库表,从表单的第一页插入了输入,并处理了第二页的数据。 在第三部分中,我们将把数据更新(而不是插入)到我们为此表格开始的行中,并说明如何根据先前的数据输入来制作表格的可选版本。

步骤1:从表单的第二页获取POST数据 (Step 1: Get POST Data from Page Two of Form)

If you don’t already have it, head on over to the second article in this series and get the code at the end of the article. We’ll continue to build on that from here on out.

如果还没有,请继续阅读本系列的第二篇文章 ,并在文章末尾获得代码。 从现在开始,我们将继续以此为基础。

Starting with your “Start Page 3 of Form” comment, we’re going to delete the echo statements that display our page two form inputs and replace that with our UPDATE statement.

从“表单的第3页开始”注释开始,我们将删除显示两个表单输入页面的echo语句,并将其替换为UPDATE语句。

We don’t want to use the $wpdb->insert function again because that would create a new row. Instead, we use the $wpdb->update function and throw in the $form_id number to tell WordPress which row to update:

我们不想再次使用$wpdb->insert函数,因为那样会创建新行。 相反,我们使用$wpdb->update函数并输入$form_id号来告诉WordPress要更新的行:

[sourcecode language=”php”]

[源代码语言=“ php”]

// Start Page 3 of Form elseif( $page == 2 ) {

//开始表格elseif($ page == 2)的第3页{

$gender = $_POST[‘gender’]; $age = $_POST[‘age’]; $education = $_POST[‘education’]; $income = $_POST[‘income’]; $page = $_POST[‘page’]; $form_id = $_POST[‘form_id’];

$ gender = $ _POST ['gender']; $ age = $ _POST ['age']; $ education = $ _POST ['education']; $ income = $ _POST ['income']; $ page = $ _POST ['page']; $ form_id = $ _POST ['form_id'];

$page_two_table = ‘shopping_preferences’; $page_two_inputs = array( ‘gender’ => $gender, ‘age’ => $age, ‘education’ => $education, ‘income’ => $income, ‘page’ => $page ); $page_two_where = array( ‘id’ => $form_id );

$ page_two_table ='购物偏好'; $ page_two_inputs = array('gender'=> $ gender,'age'=> $ age,'education'=> $ education,'income'=> $ income,'page'=> $ page); $ page_two_where = array('id'=> $ form_id);

$insert_page_two = $wpdb->update($page_two_table, $page_two_inputs, $page_two_where);

$ insert_page_two = $ wpdb-> update($ page_two_table,$ page_two_inputs,$ page_two_where);

};// End Page 3 of Format

}; //结束格式的第3页

[/sourcecode]

[/源代码]

First off, we use the ELSEIF to test for the page number. Since page two has been completed, we want to insert that form data.

首先,我们使用ELSEIF测试页码。 由于第二页已经完成,因此我们想插入该表单数据。

Next, we grab the POST data from the form and assign it to variables. Too easy, right?

接下来,我们从表单中获取POST数据并将其分配给变量。 太简单了吧?

Then, we start to assign our data to arrays for insertion via $wpdb. We have the table, inputs, and the where info that WordPress requires when updating a row.

然后,我们开始将数据分配给数组,以通过$wpdb进行插入。 我们具有表格,输入以及WordPress更新行时所需的where信息。

Last, we call the $wpdb->update function and we’re good. Well, almost.

最后,我们调用$wpdb->update函数,这很好。 好吧,差不多。

步骤2:评估表格结果 (Step 2: Evaluating the Form Results)

While this is optional, I want to go ahead and give you these tools for future reference. I’m going to be using nested IF statements to evaluate the results of the page two form data. Based upon the person’s gender, we will display different sets of forms.

尽管这是可选的,但我想继续为您提供这些工具,以供将来参考。 我将使用嵌套的IF语句来评估第二页表单数据的结果。 根据人的性别,我们将显示不同的表格集。

Remember that there are three possibilities for gender: nothing, male, and female. So we must have three IF statements.

请记住,性别存在三种可能性:没有,男性和女性。 因此,我们必须具有三个IF语句。

For female respondents, I’m going to have different options compared to males. If the person has listed themselves as “none” for gender, they get all of the options.

对于女性受访者,与男性相比,我将有不同的选择。 如果此人将自己列为“无”性别,则他们将获得所有选择。

(Note: please ignore the obvious sexism in these form options — they’re just here to demonstrate dynamic forms, not to make any kind of distinction or statement about genders.)

(注意:请忽略这些形式选项中明显的性别歧视-它们只是在这里展示动态形式,而不是对性别进行任何形式的区分或声明。)

[sourcecode language=”php”]

[源代码语言=“ php”]

// Start Page 3 of Form elseif( $page == 2 ) {

//开始表格elseif($ page == 2)的第3页{

$gender = $_POST[‘gender’]; $age = $_POST[‘age’]; $education = $_POST[‘education’]; $income = $_POST[‘income’]; $page = $_POST[‘page’]; $form_id = $_POST[‘form_id’];

$ gender = $ _POST ['gender']; $ age = $ _POST ['age']; $ education = $ _POST ['education']; $ income = $ _POST ['income']; $ page = $ _POST ['page']; $ form_id = $ _POST ['form_id'];

$page_two_table = ‘shopping_preferences’; $page_two_inputs = array( ‘gender’ => $gender, ‘age’ => $age, ‘education’ => $education, ‘income’ => $income, ‘page’ => $page ); $page_two_where = array( ‘id’ => $form_id );

$ page_two_table ='购物偏好'; $ page_two_inputs = array('gender'=> $ gender,'age'=> $ age,'education'=> $ education,'income'=> $ income,'page'=> $ page); $ page_two_where = array('id'=> $ form_id);

$insert_page_two = $wpdb->update($page_two_table, $page_two_inputs, $page_two_where);

$ insert_page_two = $ wpdb-> update($ page_two_table,$ page_two_inputs,$ page_two_where);

echo ‘<form method="post" action="’ . $this_page .’"> <label for="location" id="location">How do you like to shop?</label> <select name="location" /> <option value="nothing" selected>Pick Your Favorite</option> <option value="ebay">Online – Ebay</option> <option value="eretailer">Online – Retailers</option> <option value="classifieds">Online – Classifieds</option> <option value="store">Physical Store</option> </select> ‘;

echo'<form method =“ post” action =“'。$ this_page。'”> <label for =“ location” id =“ location”>您如何购物?</ label> <select name =“ location “ /> <option value =” nothing“ selected>选择您的收藏夹</ option> <option value =” ebay“>在线–易趣</ option> <option value =” eretailer“>在线–零售商</ option> < option value =“ classifieds”>在线–分类</ option> <option value =“ store”>实体店</ option> </ select>';

if ( $gender == "nothing" ) { echo ‘<label for="category" id="category">What do you shop for most?</label> <select name="category" /> <option value="nothing" selected>Pick Your Favorite</option> <option value="ebay">Clothes</option> <option value="eretailer">Shoes</option> <option value="classifieds">Jewelry</option> <option value="classifieds">Cooking Equipment</option> <option value="store">Sports Gear</option> <option value="classifieds">Computers – Desktop</option> <option value="classifieds">Computers – Laptop</option> <option value="classifieds">Computers – Software</option> </select>’; } if ( $gender == 0 ) { echo ‘<label for="category" id="category">What do you shop for most?</label> <select name="category" /> <option value="nothing" selected>Pick Your Favorite</option> <option value="store">Sports Gear</option> <option value="classifieds">Computers – Desktop</option> <option value="classifieds">Computers – Laptop</option> <option value="classifieds">Computers – Software</option> </select>’; } if ( $gender == 1 ) { echo ‘<label for="category" id="category">What do you shop for most?</label> <select name="category" /> <option value="nothing" selected>Pick Your Favorite</option> <option value="store">Sports Gear</option> <option value="classifieds">Computers – Desktop</option> <option value="classifieds">Computers – Laptop</option> <option value="classifieds">Computers – Software</option> </select>’; }

if($ gender ==“ nothing”){echo'<label for =“ category” id =“ category”>您最想买什么?</ label> <select name =“ category” /> <option value =选择了“什么”>选择您喜欢的商品</ option> <option value =“ ebay”>衣服</ option> <option value =“ eretailer”>鞋子</ option> <option value =“ classifieds”>珠宝</ option > <option value =“ classifieds”>烹饪设备</ option> <option value =“ store”>运动装备</ option> <option value =“ classifieds”>计算机–台式机</ option> <option value =“ classifieds “>计算机–笔记本电脑</ option> <option value =” classifieds“>计算机–软件</ option> </ select>'; } if($ gender == 0){echo'<label for =“ category” id =“ category”>您最想买什么?</ label> <select name =“ category” /> <option value =“什么都没选择”>选择您的收藏夹</ option> <option value =“ store”>运动装备</ option> <option value =“ classifieds”>计算机–台式机</ option> <option value =“ classifieds”>计算机–笔记本电脑</ option> <option value =“ classifieds”>计算机–软件</ option> </ select>'; } if($ gender == 1){echo'<label for =“ category” id =“ category”>您最想买什么?</ label> <select name =“ category” /> <option value =“什么都没选择”>选择您的收藏夹</ option> <option value =“ store”>运动装备</ option> <option value =“ classifieds”>计算机–台式机</ option> <option value =“ classifieds”>计算机–笔记本电脑</ option> <option value =“ classifieds”>计算机–软件</ option> </ select>'; }

echo ‘ <input type="hidden" value="3" name="page" /> <input type="hidden" value="’ . $form_id . ‘" name="form_id" />

回声'<input type =“ hidden” value =“ 3” name =“ page” /> <input type =“ hidden” value =“'。$ form_id。'” name =“ form_id” />

<input type="submit" /> </form>’;

<input type =“ submit” /> </ form>';

};// End Page 3 of Form

}; //结束第3页

[/sourcecode]

[/源代码]

As you can hopefully see from the IF statements, we evaluate the value from page two of the form gender input and dynamically serve a customized form. Hopefully your imagination is starting to flare up and you can see how powerful this little system could be for you!

正如您希望从IF语句中看到的那样,我们从性别输入表单的第二页评估值,并动态提供自定义表单。 希望您的想象力开始增强,您可以看到这个小系统对您有多强大!

At this point, we may also want to test to see if we’ve got all of the data we expect in our database tables. So, let me show you a quick way to check.

在这一点上,我们可能还想测试一下,看看我们的数据库表中是否有我们期望的所有数据。 因此,让我向您展示一种快速的检查方法。

步骤3:检查资料库 (Step 3: Check Your Database)

I usually keep the phpMyAdmin tool open during testing, but this is a good time to show you some other methods of querying WordPress if you’re a newbie. So let’s dump the data from our database to see what we have so far stored and check to see that it’s collecting the data we would expect.

我通常会在测试过程中保持phpMyAdmin工具处于打开状态,但是如果您是新手,那么这是个向您展示查询WordPress的其他方法的好时机。 因此,让我们从数据库中转储数据以查看到目前为止已存储的数据,并检查其是否正在收集期望的数据。

We’ll use the $wpdb->select to query the database and display the results. Right below our closing form tag, but above the “END Page 3 of Form” comment, add the following:

我们将使用$wpdb->select查询数据库并显示结果。 在我们的结束表单标签下方,但在“表单的第3页结束”注释上方,添加以下内容:

[sourcecode language=”php”]

[源代码语言=“ php”]

// Let’s check our data $data_check = $wpdb->get_row("SELECT * FROM shopping_preferences WHERE id = ‘$form_id’");

//让我们检查数据$ data_check = $ wpdb-> get_row(“ SELECT * FROM shopping_preferences WHERE id ='$ form_id'”);

echo ‘ <p> id: ‘ . $data_check->id . ‘</p> <p> first_name: ‘ . $data_check->first_name . ‘</p> <p> last_name: ‘ . $data_check->last_name . ‘</p> <p> email: ‘ . $data_check->email . ‘</p> <p> phone: ‘ . $data_check->phone . ‘</p> <p> zip_code: ‘ . $data_check->zip_code . ‘</p> <p> gender: ‘ . $data_check->gender . ‘</p> <p> age: ‘ . $data_check->age . ‘</p> <p> education: ‘ . $data_check->education . ‘</p> <p> income: ‘ . $data_check->income . ‘</p> <p> location: ‘ . $data_check->location . ‘</p> <p> categories: ‘ . $data_check->categories . ‘</p> <p> page: ‘ . $data_check->page . ‘</p> <p> timestamp: ‘ . $data_check->timestamp . ‘</p>’;

回声'<p> id:'。 $ data_check-> id。 </ p> <p> first_name:。 $ data_check-> first_name。 '</ p> <p> last_name:'。 $ data_check-> last_name。 </ p> <p>电子邮件:。 $ data_check->电子邮件。 </ p> <p>电话:。 $ data_check-> phone。 </ p> <p>邮政编码:'。 $ data_check-> zip_code。 </ p> <p>性别:。 $ data_check-> gender。 </ p> <p>年龄:。 $ data_check-> age。 </ p> <p>教育:。 $ data_check-> education。 '</ p> <p>收入:'。 $ data_check->收入。 </ p> <p>位置:。 $ data_check-> location。 </ p> <p>类别:“。 $ data_check-> categories。 </ p> <p>页:“。 $ data_check-> page。 </ p> <p>时间戳:。 $ data_check-> timestamp。 '</ p>';

[/sourcecode]

[/源代码]

We’re listing all the information from our table in this example. Note that the location and categories will be blank, because we haven’t submitted the form for page three just yet. Bear in mind that I did not limit the query, so if you’ve been running lots of tests, you’ll see all the results.

在此示例中,我们列出了表中的所有信息。 请注意,位置和类别将为空白,因为我们尚未提交第三页的表单。 请记住,我没有限制查询,因此,如果您已经运行了很多测试,则将看到所有结果。

In the next article in the series, we will create our “Thank You!” page and update the database with the last bit of information. I will also show you how to query the database and produce a report for your own use to see the questionnaire results.

在本系列的下一篇文章中,我们将创建“谢谢!” 页面并使用最新信息更新数据库。 我还将向您展示如何查询数据库并生成供您自己使用的报告,以查看问卷调查结果。

翻译自: https://www.sitepoint.com/design-a-multi-page-form-in-wordpress-multi-page-processing/

wordpress表单调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值