我已经为此工作了几个星期,但没有成功。我已经解决了代码中的一个又一个问题,但似乎没有一个更正可以解决我的核心问题。表单没有向数据库中插入任何内容,我也不知道为什么。在这个特定的示例中,在转到以下URL时提交结果:(myurl)/?dname=吉隆坡(&;年龄=56岁;动作=课后。主页是加载的内容。我是从网上的示例和教程中拼凑出来的。
我是ajax和WordPress插件的新手,所以我可能遗漏了一些明显的东西。请帮我知道我哪里做错了。
排队:
add_action(
\'plugins_loaded\',
array ( B5F_SO_13498959::get_instance(), \'plugin_setup\' )
);
class B5F_SO_13498959
{
private $cpt = \'post\'; # Adjust the CPT
protected static $instance = NULL;
public $plugin_url = \'\';
public function __construct() {}
public static function get_instance()
{
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
/**
* Regular plugin work
*/
public function plugin_setup()
{
$this->plugin_url = plugins_url( \'/\', __FILE__ );
add_shortcode(\'the_content\', array($this, \'show_form\'));
add_action( \'wp_enqueue_scripts\', array( $this, \'enqueue\' ) );
add_action( \'wp_ajax_postlesson\', array( $this, \'postlesson\' ) );
add_action( \'wp_ajax_nopriv_postlesson\', array( $this, \'postlesson\' ) );
}
public function enqueue()
{
//Include Javascript library
wp_enqueue_script(\'lessonupload\', "{$this->plugin_url}demo.js" , array( \'jquery\' ));
// including ajax script in the plugin Myajax.ajaxurl
wp_localize_script( \'lessonupload\', \'MyAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\')));
}
表格:
public function show_form(){
?>
<form type="post" name="submitlesson" action="" id="submitlesson">
<input type="text" name="dname" id="dname" />
<input type="age" name="age" id="age" />
<input type="hidden" name="action" value="postlesson" />
<input type="submit" />
</form>
<div id="feedback">
</div>
<?php
}
jQuery:
jQuery(document).ready(function($)
{
$(\'#postlesson\').submit(ajaxSubmit);
function ajaxSubmit() {
var submitlesson = $(this).serialize();
$.ajax({
type: "POST",
url: MyAjax.ajaxurl,
data: submitlesson,
success: function(data) {
$("#feedback").html(data);
}
});
}
return false;
});
php:
public function postlesson(){
global $wpdb;
$name = $_POST[\'dname\'];
$age = $_POST[\'age\'];
if($wpdb->insert(\'wp_demo\',array(
\'dname\'=>$name,
\'age\'=>$age
))===FALSE) {
echo "error";
}
else {
echo "success";
}
die();
}
}
SO网友:1991wp
看看这个例子。我的表单有一个字段(文本字段)和提交按钮:
<?php
global $wpdb; //Declare to interact with the database
?>
<form method="post" action="<?php echo $_SERVER[\'PHP_SELF\'];?>">
<h3>Add New Client</h3>
<table class="form-table">
<tr class="form-field">
<th scope="row"><label for="cname"><?php _e(\'Client Name\'); ?> </label></th>
<td><input name="client_name" type="text" id="client_name" value="" aria-required="true" autocapitalize="none" autocorrect="off" /></td>
</tr>
<input type="button" onclick="add_client();" value="add" />
</table>
</form>
使用Ajax将此值传递到PHP页面:
<script type="text/javascript">
function add_client(){
var url = \'<?php echo admin_url().\'add.php\';?>\';
jQuery.ajax({
url: url,
type: "POST",
success: function(mge){
if(mge == 0){
alert(\'Error\');
} else {
location.reload();
jQuery(\'#c_res_vl\').text(\'Client added successfully\');
}
}
});
}
</script>
此PHP代码用于检查数据库中是否存在数据,如果存在,则覆盖。创建一个名为
add.php
并添加此代码:
<?php
if(!empty($_POST[\'client_name\']) && isset($_POST[\'client_name\'])) {
$client_name = $_POST[\'client_name\'];
}
//global $wpdb;
if(isset($client_name) && !empty($client_name)){
$query = $wpdb->get_row("SELECT * FROM `wp_clients` WHERE `client_name`=\'".$client_name."\'");
if (!empty($query->client_id)) {
echo \'Client already exists\';
}else{
if($client_name != \'None\'){
$sql = $wpdb->prepare("INSERT INTO `wp_clients`(`client_name`) VALUES (\'".$client_name."\')");
if ($wpdb->query($sql)) {
echo "New client created successfully";
} else {
echo "Error: " . $sql . "<br>" . $wpdb->error;
}
}else {
echo \'Please enter the client name\';
}
}
}
?>