您好我在WordPress
创建弹出插件,因为我通过ajax在数据库中插入数据。我的弹出插件代码一切正常,直到jQuery,但之后数据没有插入数据库。
这段代码现在对我来说很好。谢谢你的帮助
<?php
/*
Plugin Name: SMS POPUP PLUGIN
Description: SMS POPUP PLUGIN
Version: 1.0
License: Plugin comes under GPL Licence.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( version_compare( get_bloginfo( 'version' ), '3.9', '<' ) ) {
wp_die("You must update WordPress to use SMS Factory Bulk SMS Service!");
}
include_once(ABSPATH.'wp-admin/includes/plugin.php');
function sfp_popup() {
global $wpdb, $wnm_db_version;
$sfp_sql = array();
//sms table
$sfp_content = $wpdb->prefix . "sfp_content";
if( $wpdb->get_var("show tables like '". $sfp_content . "'") !== $sfp_content ) {
$sfp_sql[] = "CREATE TABLE ". $sfp_content . " ( sfp_id int(11) NOT NULL AUTO_INCREMENT, sfp_name varchar(256) NOT NULL, sfp_phone int(11) NOT NULL, sfp_pwd varchar(100) DEFAULT NULL, sfp_email varchar(100) DEFAULT NULL, sfp_query varchar(256) DEFAULT NULL, PRIMARY KEY (sfp_id) ) ";
}
if ( !empty($sfp_sql) ) {
require_once(ABSPATH."wp-admin/includes/upgrade.php");
dbDelta($sfp_sql);
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
add_option("wnm_db_version", $wnm_db_version);
}
}
register_activation_hook(__FILE__, 'sfp_popup');
add_action( 'wp_head', 'enque_sfp_script', 100 );
function enque_sfp_script(){
//Include Javascript library
wp_enqueue_script('sfp_script', plugins_url( '/js/demo.js' , __FILE__ ) , array( 'jquery' ));
// including ajax script in the plugin Myajax.ajaxurl
wp_localize_script( 'sfp_script', 'sfpAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php')));
}
function sfp_insert(){
global $wpdb;
$sfp_table = $wpdb->prefix."sfp_content";
$name = $_POST['sf_name'];
$phone = $_POST['sf_phone'];
$message = $_POST['sf_message'];
$wpdb->insert(
$sfp_table,
array(
'sfp_name' => $name,
'sfp_phone' => $phone,
'sfp_query' => $message
),
array(
'%s',
'%d',
'%s'
)
);
die();
return true;
}
add_action('wp_ajax_sfp_insert', 'sfp_insert');
add_action('wp_ajax_nopriv_sfp_insert', 'sfp_insert');
add_action( 'wp_footer', 'sf_display_popup', 100 );
function sf_display_popup(){
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script>
jQuery(document).ready(function($) {
setTimeout(function(){
$('#myModal').modal('show');
}, 3000);
$( "#sf-popup .sfname" ).keyup(function() {
if($(this).val() == '' || $( "#sf-popup .sfphone").val() == ''){
$('#sfpsubmit').attr('disabled','disabled');
} else{
$('#sfpsubmit').removeAttr('disabled');
$( ".sfp_btn" ).click(function() {
$('#myModal').modal('hide');
});
}
});
$( "#sf-popup .sfphone" ).keyup(function() {
var sfp_phone = $(this).val();
if(sfp_phone.length > 10){
$(this).val($(this).val().substr(0,10));
$('#sfpsubmit').removeAttr('disabled');
}
if(sfp_phone == '' || $( "#sf-popup .sfname").val() == '' || $.isNumeric(sfp_phone) == false || sfp_phone.length < 10){
$('#sfpsubmit').attr('disabled','disabled');
} else{
$('#sfpsubmit').removeAttr('disabled');
$( ".sfp_btn" ).click(function() {
$('#myModal').modal('hide');
});
}
});
});
</script>
<style>
.modal.fade .modal-dialog {
-webkit-transform: scale(0.1);
-moz-transform: scale(0.1);
-ms-transform: scale(0.1);
transform: scale(0.1);
top: 300px;
opacity: 0;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
transition: all 0.5s;
}
.modal.fade.in .modal-dialog {
-webkit-transform: scale(1);
-moz-transform: scale(1);
-ms-transform: scale(1);
transform: scale(1);
-webkit-transform: translate3d(0, -300px, 0);
transform: translate3d(0, -300px, 0);
opacity: 1;
}
textarea.col-md-12 {
margin: 25px 0;
}
</style>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="memberModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<?php
echo "<form method='post' action='' name='sf-popup' id='sf-popup' >";
echo "<input type='text' name='sf_name' id='sf_name' placeholder='Name' class='col-xs-12 col-md-6 sfname'>";
echo "<input type='text' name='sf_phone' id='sf_phone' placeholder='98765-43210' class='col-xs-12 col-md-offset-1 col-md-5 sfphone'>";
echo "<textarea name='sf_message' id='sf_message' placeholder='Enter Your Query' class='col-md-12'></textarea>";
echo "</form>";
?>
</div>
<div class="modal-footer">
<?php
echo "<button id='sfpsubmit' class='btn btn-primary sfp_btn' name='sfpsubmit' disabled>Submit</button>";
?>
</div>
</div>
</div>
</div>
<?php
}
?>
这是我的jquery代码
jQuery(document).ready(function(){
jQuery("#sfpsubmit").click(function(){
var name = jQuery("#sf_name").val();
var phone = jQuery("#sf_phone").val();
var message = jQuery("#sf_message").val();
alert(name + phone + message);
jQuery.ajax({
type: 'POST',
url: sfpAjax.ajaxurl,
data: {action: "sfp_insert","sf_name":name,"sf_phone":phone,"sf_message":message},
success: function(data){
alert("success"+data);
}
});
});
});
答案 0 :(得分:0)
试试这个数据:
data: {action: "post_word_count", sf_name:name},
使用此调试功能
add_action('wp_ajax_post_word_count', 'post_word_count');
add_action('wp_ajax_nopriv_post_word_count', 'post_word_count');
function post_word_count(){
echo $_POST['sf_name'];
die();
}
答案 1 :(得分:0)
在您的代码中,您已将脚本排入队列,然后将其本地化..
它应首先注册然后本地化然后入队。
// Register the script
wp_register_script( 'some_handle', 'path/to/myscript.js' );
// Localize the script with new data
$translation_array = array(
'some_string' => __( 'Some string to translate', 'plugin-domain' ),
'a_value' => '10'
);
wp_localize_script( 'some_handle', 'object_name', $translation_array );
// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );
我没有测试过您的方法,但这个方法对我有用。