因此,我正在WordPress中创建一个自定义表单,该表单询问用户一系列的是和否问题。
我使用复选框作为输入,三元运算符检查是否设置了yes复选框,并根据是否设置了return和integer进行检查。我可以进行var转储并返回正确的整数,但似乎无法将它们插入数据库中。
这里是我创建的表单的示例(由于表单很长,我只添加表单的前3个问题,其他所有输入均采用相同的格式。)
HTML表单
<form action="">
<div class="questions-main">
<div id="question-1" class="question">
<h4>Did you ever lose time from work or school due to gambling?</h4>
<input type="checkbox" id="q-one-yes" name="q-one-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-one-no" name="q-one-no" value="no" class="messageCheckbox">No</input>
</div>
<div id="question-2" class="question">
<h4>Has gambling ever made your home life unhappy?</h4>
<input type="checkbox" id="q-two-yes" name="q-two-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-two-no" name="q-two-no" value="no" class="messageCheckbox">No</input>
</div>
<div id="question-3" class="question">
<h4>Did gambling affect your reputation?</h4>
<input type="checkbox" id="q-three-yes" name="q-three-yes" value="yes" class="messageCheckbox">Yes</input>
<input type="checkbox" id="q-three-no" name="q-three-no" value="no" class="messageCheckbox">No</input>
</div>
</div>
</form>
表格创建
<?php
global $aw_db_version;
$aw_db_version = "1.0";
function database_function() {
global $wpdb;
global $aw_db_version;
$tablename = $wpdb->prefix . "form_submission";
$charset_collate = $wpdb->get_charset_collate();
$sql="CREATE TABLE $tablename (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time_stamp datetime NULL,
question_one int(1) NULL,
question_two int(1) NULL,
question_three int(1) NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . "/wp-admin/includes/upgrade.php");
dbdelta($sql);
add_option( 'aw_db_version', $aw_db_version);
}
插入功能
<?php
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
if(isset($_POST)){
global $wpdb, $table_prefix;
$table_name = $wpdb->prefix . 'form_submission';
$timestamp = current_time( 'mysql' );
$q_1 = isset($_POST['q-one-yes']) ? 1 : 0;
$q_2 = isset($_POST['q-two-yes']) ? 1 : 0;
$q_3 = isset($_POST['q-three-yes']) ? 1 : 0;
var_dump($q_1, $q_20);
$wpdb->insert( $table_name, array(
'time_stamp' => $timestamp,
'question_one' => $q_1,
'question_two' => $q_2,
'question_three' => $q_3,
) );
echo "Working";
}else{
echo "Something's Gone Wrong Mate!";
};