· 9 years ago · Jan 12, 2017, 03:16 AM
1//Creates table, displays and validates bid form, and inserts valid bid data into table.
2function bid_form_display(){
3 //$error_msg = '';
4 global $wpdb;
5
6 // get post id for auction from post where auction is inserted. Used to track bids db.
7 $postid = get_the_id();
8 // creates jwp_bids table in database if it doesn't exist
9 $table = $wpdb->prefix . "jwp_bids";
10 $charset_collate = $wpdb->get_charset_collate();
11 $sql = "CREATE TABLE IF NOT EXISTS $table (
12 `id` mediumint(15) NOT NULL AUTO_INCREMENT,
13 `bid_amt` decimal(10,2) NOT NULL,
14 `email` varchar(255) NOT NULL,
15 `bid_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
16 `post_id` mediumint(15) NOT NULL,
17 UNIQUE (`id`)
18 ) $charset_collate;";
19 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
20 dbDelta( $sql );
21
22 //Gets highest bid information and assigns to variables for display in post.
23 $highest_bid_info = $wpdb->get_results(
24 "Select max(bid_amt) AS bid, bid_time, email
25 FROM $table
26 WHERE post_id = $postid", ARRAY_A );
27 $highest_bid_info = array_shift ( $highest_bid_info );
28 $high_bid = $highest_bid_info['bid'];
29 $high_bidder = $highest_bid_info['email'];
30 $bid_time = $highest_bid_info['bid_time'];
31
32 //assign error messages to variables
33 $not_human = "Human verification incorrect.";
34 $bid_invalid = "Invalid or no bid placed. Please enter a valid bid.";
35 $email_invalid = "Invalid or no email entered. Please enter a valid email address.";
36 $insert_failed = "Oops! Sorry. Website malfunction. Please try again.";
37 $bid_posted = "Thank you! Your bid was posted successfully.";
38
39 //sanitize bid form post variables and assign to new variables.
40 $email = htmlspecialchars($_POST["email"], ENT_QUOTES, 'UTF-8');
41 $bid_amount = htmlspecialchars($_POST["bid_amount"], ENT_QUOTES, 'UTF-8');
42 $human = htmlspecialchars($_POST["message_human"], ENT_QUOTES, 'UTF-8');
43
44 //process form entries, generate errors, if no errors insert bid info in db table
45 if(!$human == 0){
46 if($human != 2){
47 generate_error_msg("error", $not_human);
48 } elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
49 generate_error_msg("error", $email_invalid);
50 } elseif(empty($bid_amount)) {
51 generate_error_msg("error", $bid_invalid);
52 } else {
53 //insert the posted form data into the database.
54 $insert_db = $wpdb->insert(
55 $table,
56 array(
57 'email' => $email,
58 'bid_amt' => $bid_amount,
59 'post_id' => $postid,
60 )
61 );
62 if( $insert_db ){
63 generate_error_msg("success", $bid_posted);
64 } else {
65 generate_error_msg("error", $insert_failed);
66 }
67 }
68 }
69
70//function to generate error messages for bid form
71function generate_error_msg($type, $message){
72 global $error_msg;
73
74 if($type == "success") {
75 $error_msg = "<div class='success'>{$message}</div>";
76 } else {
77 $error_msg = "<div class='error'>{$message}</div>";
78 }
79}
80
81//HTML for current status of bids: Highest bid, email of bidder, time of bid.
82 $form_output = '';
83 $form_output .= '<div id="currentbid">';
84 $form_output .= 'The current bid is $' . $high_bid .'.';
85 // Display current bid + $2 as minimum for next bid.
86 $form_output .= 'Your minimum bid is $' . $high_bid . '.';
87 $form_output .= '</div>';
88//HTML for bid form
89 $form_output .= '<div id="bid">';
90 $form_output .= '<?php echo' . $error_msg . '?>';
91 $form_output .= '<form method="post">';
92 $form_output .= '<p><label for="email">Notification email (IMPORTANT!) <span>*</span><br><input type="text" name="email" value="' . $email . '"></label></p>';
93 $form_output .= '<p><label for="bid_amount">Enter your bid<span>*</span> <br><input type="text" name="bid_amount" value="' . $bid_amount . '"></label></p>';
94 $form_output .= '<p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>';
95 $form_output .= '<input type="hidden" name="submitted" value="1">';
96 $form_output .= '<input type="hidden" name="post_id" value="' . $postid . '">';
97 $form_output .= '<p><input type="submit"></p>';
98 $form_output .= '</form>';
99 $form_output .= '</div>';
100
101 return $form_output;
102
103}
104
105add_shortcode('bid_form', 'bid_form_display');
106
107//function to generate error messages for bid form
108function generate_error_msg($type, $message){
109 global $error_msg;
110
111 if($type == "success") {
112 $error_msg = "<div class='success'>{$message}</div>";
113 } else {
114 $error_msg = "<div class='error'>{$message}</div>";
115 }
116}
117
118//Creates table, displays and validates bid form, and inserts valid bid data into table.
119function bid_form_display(){
120 //$error_msg = '';
121 global $wpdb;
122
123 // get post id for auction from post where auction is inserted. Used to track bids db.
124 $postid = get_the_id();
125 // creates jwp_bids table in database if it doesn't exist
126 $table = $wpdb->prefix . "jwp_bids";
127 $charset_collate = $wpdb->get_charset_collate();
128 $sql = "CREATE TABLE IF NOT EXISTS $table (
129 `id` mediumint(15) NOT NULL AUTO_INCREMENT,
130 `bid_amt` decimal(10,2) NOT NULL,
131 `email` varchar(255) NOT NULL,
132 `bid_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
133 `post_id` mediumint(15) NOT NULL,
134 UNIQUE (`id`)
135 ) $charset_collate;";
136 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
137 dbDelta( $sql );
138
139 //Gets highest bid information and assigns to variables for display in post.
140 $highest_bid_info = $wpdb->get_results(
141 "Select max(bid_amt) AS bid, bid_time, email
142 FROM $table
143 WHERE post_id = $postid", ARRAY_A );
144 $highest_bid_info = array_shift ( $highest_bid_info );
145 $high_bid = $highest_bid_info['bid'];
146 $high_bidder = $highest_bid_info['email'];
147 $bid_time = $highest_bid_info['bid_time'];
148
149 //assign error messages to variables
150 $not_human = "Human verification incorrect.";
151 $bid_invalid = "Invalid or no bid placed. Please enter a valid bid.";
152 $email_invalid = "Invalid or no email entered. Please enter a valid email address.";
153 $insert_failed = "Oops! Sorry. Website malfunction. Please try again.";
154 $bid_posted = "Thank you! Your bid was posted successfully.";
155
156 //sanitize bid form post variables and assign to new variables.
157 $email = htmlspecialchars($_POST["email"], ENT_QUOTES, 'UTF-8');
158 $bid_amount = htmlspecialchars($_POST["bid_amount"], ENT_QUOTES, 'UTF-8');
159 $human = htmlspecialchars($_POST["message_human"], ENT_QUOTES, 'UTF-8');
160
161 //process form entries, generate errors, if no errors insert bid info in db table
162 if(!$human == 0){
163 if($human != 2){
164 generate_error_msg("error", $not_human);
165 } elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
166 generate_error_msg("error", $email_invalid);
167 } elseif(empty($bid_amount)) {
168 generate_error_msg("error", $bid_invalid);
169 } else {
170 //insert the posted form data into the database.
171 $insert_db = $wpdb->insert(
172 $table,
173 array(
174 'email' => $email,
175 'bid_amt' => $bid_amount,
176 'post_id' => $postid,
177 )
178 );
179 if( $insert_db ){
180 generate_error_msg("success", $bid_posted);
181 } else {
182 generate_error_msg("error", $insert_failed);
183 }
184 }
185 }
186
187
188//HTML for current status of bids: Highest bid, email of bidder, time of bid.
189 $form_output = '';
190 $form_output .= '<div id="currentbid">';
191 $form_output .= 'The current bid is $' . $high_bid .'.';
192 // Display current bid + $2 as minimum for next bid.
193 $form_output .= 'Your minimum bid is $' . $high_bid . '.';
194 $form_output .= '</div>';
195//HTML for bid form
196 $form_output .= '<div id="bid">';
197 $form_output .= '<?php echo' . $error_msg . '?>';
198 $form_output .= '<form method="post">';
199 $form_output .= '<p><label for="email">Notification email (IMPORTANT!) <span>*</span><br><input type="text" name="email" value="' . $email . '"></label></p>';
200 $form_output .= '<p><label for="bid_amount">Enter your bid<span>*</span> <br><input type="text" name="bid_amount" value="' . $bid_amount . '"></label></p>';
201 $form_output .= '<p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>';
202 $form_output .= '<input type="hidden" name="submitted" value="1">';
203 $form_output .= '<input type="hidden" name="post_id" value="' . $postid . '">';
204 $form_output .= '<p><input type="submit"></p>';
205 $form_output .= '</form>';
206 $form_output .= '</div>';
207
208 return $form_output;
209
210}
211
212add_shortcode('bid_form', 'bid_form_display');