Inserting Data from “Yes” / “No” checkboxes into database table in WordPress
up vote
0
down vote
favorite
So I'm creating a custom form in WordPress which asks the user a series of yes and no questions.
I'm using a Check boxes as my inputs, and ternary operators to check whether or not the yes checkbox is set, and return and integer depending on if its set or not. I'm able to do a var dump and return the correct integers but I can't seem to get them inserted into the Database.
Here is a sample from the form I created (I only add the first 3 questions of the form since it is quite long, all of the other inputs are the same format.)
HTML FORM
<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>
Table Creation
<?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);
}
INSERT FUNCTION
<?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!";
};
php wordpress
add a comment |
up vote
0
down vote
favorite
So I'm creating a custom form in WordPress which asks the user a series of yes and no questions.
I'm using a Check boxes as my inputs, and ternary operators to check whether or not the yes checkbox is set, and return and integer depending on if its set or not. I'm able to do a var dump and return the correct integers but I can't seem to get them inserted into the Database.
Here is a sample from the form I created (I only add the first 3 questions of the form since it is quite long, all of the other inputs are the same format.)
HTML FORM
<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>
Table Creation
<?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);
}
INSERT FUNCTION
<?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!";
};
php wordpress
1
Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
– muka.gergely
yesterday
I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
– ajwerth
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So I'm creating a custom form in WordPress which asks the user a series of yes and no questions.
I'm using a Check boxes as my inputs, and ternary operators to check whether or not the yes checkbox is set, and return and integer depending on if its set or not. I'm able to do a var dump and return the correct integers but I can't seem to get them inserted into the Database.
Here is a sample from the form I created (I only add the first 3 questions of the form since it is quite long, all of the other inputs are the same format.)
HTML FORM
<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>
Table Creation
<?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);
}
INSERT FUNCTION
<?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!";
};
php wordpress
So I'm creating a custom form in WordPress which asks the user a series of yes and no questions.
I'm using a Check boxes as my inputs, and ternary operators to check whether or not the yes checkbox is set, and return and integer depending on if its set or not. I'm able to do a var dump and return the correct integers but I can't seem to get them inserted into the Database.
Here is a sample from the form I created (I only add the first 3 questions of the form since it is quite long, all of the other inputs are the same format.)
HTML FORM
<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>
Table Creation
<?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);
}
INSERT FUNCTION
<?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!";
};
php wordpress
php wordpress
asked yesterday
ajwerth
437
437
1
Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
– muka.gergely
yesterday
I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
– ajwerth
yesterday
add a comment |
1
Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
– muka.gergely
yesterday
I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
– ajwerth
yesterday
1
1
Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
– muka.gergely
yesterday
Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
– muka.gergely
yesterday
I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
– ajwerth
yesterday
I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
– ajwerth
yesterday
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416973%2finserting-data-from-yes-no-checkboxes-into-database-table-in-wordpress%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you tried echoing $_POST[‘q-one-yes’] or the results of the isset() function? (I mean: are you sure that you get the values you expect?)
– muka.gergely
yesterday
I actually just solved the issue haha, the problem was I missed a column in the create table function. I was doing a var_dump and getting the correct values prior to solving the issue that's why I was so confused.
– ajwerth
yesterday