我正在一个网站上工作,我需要将&访问用户数据插入到数据库中。我已经创建了一个自定义模板,可以获取数据,但是当我尝试插入它时,显示带有错误消息“对象对象”的警告框。在向数据库插入数据时,在WordPress的警报框中获取“对象对象”错误消息
定制模板代码如下:
<?php
/*
Template Name: Customers
*/
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part('content', 'page'); ?>
<?php
if (is_user_logged_in()):?>
<form type="post" action="" id="newCustomerForm">
<label for="name">Name:</label>
<input name="name" type="text" />
<label for="email">Email:</label>
<input name="email" type="text" />
<label for="phone">Phone:</label>
<input name="phone" type="text" />
<label for="address">Address:</label>
<input name="address" type="text" />
<input type="hidden" name="action" value="addCustomer"/>
<input type="submit">
</form>
<br/><br/>
<div id="feedback"></div>
<br/><br/>
<?php
global $wpdb;
$customers = $wpdb->get_results("SELECT * FROM customers;");
echo "<table>";
foreach($customers as $customer){
echo "<tr>";
echo "<td>".$customer->name."</td>";
echo "<td>".$customer->email."</td>";
echo "<td>".$customer->phone."</td>";
echo "<td>".$customer->address."</td>";
echo "</tr>";
}
echo "</table>";
else:
echo "Sorry, only registered users can view this information";
endif;
?>
<script type="text/javascript">
jQuery('#newCustomerForm').submit(ajaxSubmit);
function ajaxSubmit(){
var newCustomerForm = jQuery(this).serialize();
jQuery.ajax({
type:"POST",
url: "/wp-admin/admin-ajax.php",
data: newCustomerForm,
success:function(data){
jQuery("#feedback").html(data);
},
error: function(errorThrown){
alert(errorThrown);
}
});
return false;
}
</script>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
在我的functions.php添加以下代码:预先
/*
Following function recieves data from the user
*/
wp_enqueue_script('jquery');
function addCustomer(){
global $wpdb;
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$address = $_POST['address'];
if($wpdb->insert('customers',array(
'name'=>$name,
'email'=>$email,
'address'=>$address,
'phone'=>$phone
))===FALSE){
echo "Error";
}
else {
echo "Customer '".$name. "' successfully added, row ID is ".$wpdb->insert_id;
}
die();
}
add_action('wp_ajax_addCustomer', 'addCustomer');
add_action('wp_ajax_nopriv_addCustomer', 'addCustomer');
感谢。
你只是想提醒的对象,而不是一个字符串。请改用'console.log()'。更好的是,显示'errorThrown.message'而不是整个'errorThrown'对象。 – JJJ 2013-02-22 17:23:26
感谢您的回复,但我正在学习WordPress的阶段。所以如果你能更具体地说我应该在哪里使用它,它会更有帮助。再次感谢您的宝贵回复 – 2013-02-22 17:26:33
您只有一个地方显示了'errorThrown'。当然你可以自己找到它,并用'console.log(errorThrown.message)'代替它。 (或者如果你不知道如何使用控制台,alert(errorThrown.message)')。 – JJJ 2013-02-22 17:28:42