Ajax form in plugin admin page is not working
-
I am making a wordpress plugin on localhost. I add an ajax form in the custum admin panel for plugin. But the form not working. When I press on the submit button (“add” button) in my form, it shows an empty page and do nothing.
My plugin code is:
<?php
/* Plugin Name: Eventism
Plugin URI:
Description: Event Management System
Version: 1.0
Author: Ali Azlan
Author URI:
License: GPLv2 or later
*/defined(‘ABSPATH’) or die(“No script kiddies please!”);
//Include Javascript library
wp_enqueue_script(‘eventism’, plugins_url( ‘/script1.js’ , __FILE__ ) , array( ‘jquery’ ));
// including ajax script in the plugin Myajax.ajaxurl
wp_localize_script( ‘eventism’, ‘MyAjax’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’)));wp_register_script( “eventism”, plugins_url( ‘/script1.js’ , __FILE__ ), array(‘jquery’) );
wp_enqueue_script( ‘jquery’ );// include ‘addnewevent.php’;
function eventism_activation() {
eventism_install();
}
register_activation_hook(__FILE__, ‘eventism_activation’);function eventism_deactivation() {
}
register_deactivation_hook(__FILE__, ‘eventism_deactivation’);function eventism_install () {
global $wpdb;/*
* We’ll set the default character set and collation for this table.
* If we don’t do this, some characters could end up being converted
* to just ?’s when saved in our table.
*/$charset_collate = ”;
if ( ! empty( $wpdb->charset ) ) {
$charset_collate = “DEFAULT CHARACTER SET {$wpdb->charset}”;
}if ( ! empty( $wpdb->collate ) ) {
$charset_collate .= ” COLLATE {$wpdb->collate}”;
}$sql1 = “CREATE TABLE eventism_users (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(30) DEFAULT ” NOT NULL,
cell varchar(13) DEFAULT ” NOT NULL,
eventcode varchar(10) DEFAULT ” NOT NULL,
cnic varchar(13) DEFAULT ”,
email varchar(30) DEFAULT ”,
confirm VARCHAR(2) DEFAULT ‘n’ NOT NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY id (id),
UNIQUE INDEXid_UNIQUE(idASC)
) $charset_collate;”;$sql2 = “CREATE TABLE eventism_events (
id mediumint(9) NOT NULL AUTO_INCREMENT,
event varchar(10) DEFAULT ” NOT NULL,
event_date varchar(30) DEFAULT ” NOT NULL,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY id (id),
UNIQUE INDEXid_UNIQUE(idASC)
) $charset_collate;”;require_once( ABSPATH . ‘wp-admin/includes/upgrade.php’ );
dbDelta( $sql1 );
dbDelta( $sql2 );$wpdb->insert(
$table_name=’eventism_events’,
array(
‘event’ => ‘swaik1’,
‘event_date’ => ’10 december 2014′,
)
);}
add_action( ‘admin_menu’, ‘eventism_menu’ );
function eventism_menu() {
add_menu_page( ‘Eventism’, ‘Eventism’, ‘manage_options’, ‘eventism_menu’);
add_submenu_page( ‘eventism_menu’, ‘Eventism’, ‘Eventism’, ‘manage_options’, ‘eventism_menu’, ‘eventism_options_events’ );
}function eventism_options_events() {
if ( !current_user_can( ‘manage_options’ ) ) {
wp_die( __( ‘You do not have sufficient permissions to access this page.’ ) );
}
echo ‘<div class=”wrap”>’;
echo ‘<h1><b>Eventism</b></h1><sub><i> by Azlan</i><hr>
‘;echo ‘<h1>Add new Event</h1>’;
echo ‘
<form type=”post” id=”newevent” action=””>
<b>Event Name: </b>
<input type=”text” name=”eventname” id=”eventname”>
<b>Event Date: </b>
<input type=”date” name=”Event Date” id=”eventdate” placeholder=”yyyy-mm-dd”>
<input type=”submit” value=”Add” id=”add1″>
</form><div id=”feedback”></div><hr>
‘;
echo ‘
<h1>Customer Database</h1>‘;
}function addevent(){
global $wpdb;
$name = $_POST[‘eventname’];
// $date = $_POST[‘eventdate’];
$date = “meridate”;if($wpdb->insert(‘eventism_events’,array(
‘event’=>$name,
‘event_date’=>$date,
))==FALSE){echo “Error”;
}
else {
echo “Event Added Successfully!”;}
die();
}add_action(‘wp_ajax_addevent’, ‘addevent’);
add_action(‘wp_ajax_nopriv_addevent’, ‘addevent’);?>
And my script1.js file code is:
jQuery(document).ready(function(){
jQuery(“#add1”).click(function(){
var eventname = jQuery(“#eventname”).val();
var eventdate = jQuery(“#eventdate”).val();
jQuery.ajax({
type: ‘POST’,
url: MyAjax.ajaxurl,
data: {“action”: “addevent”, “eventname”:eventname},
success: function(data){
alert(data);
}
});
});
});The blog I need help with is: (visible only to logged in users)
-
Hi there,
That site is not hosted here by WordPress.COM. We are unable to provide support for any blogs that are not hosted here.WordPress.com and WordPress.org are completely separate and have different logins, features, run different versions of some themes with the same names, and have separate support forums. http://support.wordpress.com/com-vs-org/
If you don’t have a username account at WordPress.ORG click http://wordpress.org/support/ and register one on the top right hand corner of the page that opens, so you can post to the support forums there and receive advice from WordPress.ORG bloggers.
Resetting your WordPress.ORG password http://codex.wordpress.org/Resetting_Your_Password
WordPress.org support docs are at https://codex.wordpress.org/Main_Page
- The topic ‘Ajax form in plugin admin page is not working’ is closed to new replies.