I have decided to build my own contact form plugin in WordPress that posts the data to the database, displays the data in the WP dashboard and sends a copy of the form to the admins email address. Everything is working except for when the form is submitted - it doesn't display in the WP admin or the database. This worked until recently but once I changed the code in order to get the jquery validation working - it stopped posting the form values.
I have included all the code below - if anyone could help me fix this it would be much appreciated.
Plugin Settings Code
<?php
/*
Plugin Name: Contact Us Form Plugin
Plugin URI: iphonerepair.ie
Description: Contact us form with validaton - sends email and posts responses to database and WP dashboard.
Version: 1.0
*/
if(!defined('WPINC'))
{
die;
}
// create table at plugin activition
register_activation_hook( __FILE__, 'jms_create_db' );
function jms_create_db()
{
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name=$wpdb->prefix.'contactusform';
$sql="CREATE TABLE $table_name(
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT NULL,
name varchar(50) DEFAULT NULL,
telno varchar(20) DEFAULT NULL,
email varchar(75) DEFAULT NULL,
town varchar(75) DEFAULT NULL,
device varchar(75) DEFAULT NULL,
message text,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
//adding plugin to admin menu
add_action('admin_menu', 'jms_menu');
function jms_menu()
{
add_menu_page(__('Contact Form','jms'), __('Contact Form','jms'),
'administrator', 'jms-contact-form', 'jms_settings_page', 'dashicons-email');
function jms_settings_page()
{
global $wpdb;
$table_name=$wpdb->prefix.'contactusform';
$client_msg = $wpdb->get_results(
"
SELECT *
FROM $table_name
"
);
require_once(plugin_dir_path(__FILE__).'settings-page.php');
}
}
function cf_jms()
{
ob_start();
require_once(plugin_dir_path(__FILE__).'form.php');
return ob_get_clean();
}
add_shortcode( 'jms_contact_form', 'cf_jms' );
//if you want to have both logged in and not logged in users submitting, you have to add both actions!
add_action( 'admin_post_add_foobar', 'jms_admin_add_foobar' );
add_action( 'admin_post_nopriv_add_foobar', 'jms_admin_add_foobar' );
function jms_admin_add_foobar() {
global $wpdb;
$data = array(
'time' => current_time('mysql'),
'name' => sanitize_text_field( $_POST['name']),
'telno' => sanitize_text_field( $_POST['telno']),
'email' => isset( $_POST['email'] ) ? sanitize_email( $_POST['email']) : null,
'town' => sanitize_text_field( $_POST['town']),
'device' => sanitize_text_field( $_POST['device']),
'message' => sanitize_text_field( $_POST['message'])
);
$table_name = $wpdb->prefix.'contactusform';
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
// send Email for admin
wp_mail(
get_option( 'admin_email' ),
'Instant Qoute/Callback Form',
'Time : ' . $data['time'] .
'Name : ' . $data['name'] .
'Tel No : ' . $data['telno'] .
'Email : ' . $data['email'] .
'Town : ' . $data['town'] .
'Device : ' . $data['device'] .
'The message: ' . $data['message'],
$headers
);
if ( $wpdb->insert( $table_name, $data ) ) {
$_SESSION['message'] = "";
} else {
$_SESSION['message'] = "";
}
//redirect back to where user was comming
wp_redirect( $_SERVER['HTTP_REFERER'] );
//request handlers should die() when they complete their task
}
?>
Validation Code
<script src="//code.jquery.com/jquery-latest.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.form/3.32/jquery.form.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.11.1/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.contactusform').validate({
rules: {
name: {
required: true
},
telno: {
required: true,
number: true
},
email: {
required: true,
email: true
},
town: {
required: true
},
device: {
required: true
},
message: {
required: true
},
},
messages: {
name: {
required: "Please enter your full name."
},
telno: {
required: "Please enter your phone number."
},
email: {
required: "Please enter your email address."
},
town: {
required: "Please enter your town."
},
device: {
required: "Please select your device."
},
message: {
required: "Please enter your message."
},
},
submitHandler: function (form) {
$("#simple-msg").html("Sending...");
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax({
type: "POST",
url: formURL,
data: postData,
success:function(data, textStatus, jqXHR) {
$("#simple-msg").html('<p>Thanks for your request - we will be in touch soon!</p>');
},
error: function(jqXHR, textStatus, errorThrown) {
$("#simple-msg").html('<p>Message failed to send. Please try again!</p>');
}
});
}
});
});
</script>
Form Code
<form name='message' id='message' class="contactusform" enctype="multipart/form-data" action="<?=admin_url()?>admin-post.php">
<?php
if(isset($_SESSION['message']))
{
echo $_SESSION['message'];
unset($_SESSION['message']);
}
?>
<input type="hidden" name="action" value="add_foobar">
<input type="hidden" name="data" value="foobarid">
<label>Full Name:</label>
<input type="text" name="name" value="" required="">
<label>Phone Number:</label>
<input type="text" name="telno" id="telno">
<label>Email Address:</label>
<input type="email" name="email" value="" required="">
<label>Town:</label>
<input type="text" name="town" value="" required="">
<label>Device:</label>
<select name="device" value="" required="">
<option selected="selected" value=""></option>
<option value="Not Sure">Not Sure</option>
<option selected="selected" value=""></option>
<option value="iPhone 3G">iPhone 3G</option>
<option value="iPhone 3GS">iPhone 3GS</option>
<option value="iPhone 4G">iPhone 4G</option>
<option value="iPhone 4S">iPhone 4S</option>
<option value="iPhone 5">iPhone 5</option>
<option value="iPhone 5C">iPhone 5C</option>
<option value="iPhone 5S">iPhone 5S</option>
<option value="iPhone 6">iPhone 6</option>
<option value="iPhone 6 Plus">iPhone 6 Plus</option>
<option selected="selected" value=""></option>
<option value="MacBook">MacBook</option>
<option value="MacBook Pro">MacBook Pro</option>
<option value="MacBook Air">MacBook Air</option>
<option selected="selected" value=""></option>
<option value="iMac">iMac</option>
<option selected="selected" value=""></option>
<option value="iPad 1">iPad 1</option>
<option value="iPad 2">iPad 2</option>
<option value="iPad 3">iPad 3</option>
<option value="iPad 4">iPad 4</option>
<option value="iPad Air">iPad Air</option>
<option value="iPad Air 2">iPad Air 2</option>
<option value="iPad Mini 1">iPad Mini 1</option>
<option value="iPad Mini 2">iPad Mini 2</option>
<option value="iPad Mini 3">iPad Mini 3</option>
<option selected="selected" value=""></option>
<option value="iPod Classic">iPod Classic</option>
<option value="iPod Mini">iPod Mini</option>
<option value="iPod Nano">iPod Nano</option>
<option value="iPod Shuffle">iPod Shuffle</option>
<option value="iPod Touch">iPod Touch</option>
</select>
<label>Message:</label>
<textarea name="message" cols="30" rows="4" value="" required=""></textarea>
<input class="submit2" type='submit' id='submit' value='Send Message' />
</form>
<div id='simple-msg'></div>
Code For Showing Data In WP Admin
<div class="wrap">
<h3><?php esc_attr_e( 'Instant Quote/Callback Requests', 'jms' ); ?></h3>
<table class="widefat">
<thead>
<tr>
<th><?php esc_attr_e( 'Time', 'jms' ); ?></th>
<th><?php esc_attr_e( 'Name', 'jms' ); ?></th>
<th><?php esc_attr_e( 'Telephone Number', 'jms' ); ?></th>
<th><?php esc_attr_e( 'Email Address', 'jms' ); ?></th>
<th><?php esc_attr_e( 'Town', 'jms' ); ?></th>
<th><?php esc_attr_e( 'Device', 'jms' ); ?></th>
<th><?php esc_attr_e( 'Message', 'jms' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach($client_msg as $client): ?>
<tr>
<td><?php esc_attr_e($client->time,'jms');?></td>
<td><?php esc_attr_e($client->name,'jms');?></td>
<td><?php esc_attr_e($client->telno,'jms');?></td>
<td><?php esc_attr_e($client->email,'jms');?></td>
<td><?php esc_attr_e($client->town,'jms');?></td>
<td><?php esc_attr_e($client->device,'jms');?></td>
<td><?php esc_attr_e($client->message,'jms');?></td>
</tr>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire