2013-02-22 59 views
1

我正在一個網站上工作,我需要將&訪問用戶數據插入到數據庫中。我已經創建了一個自定義模板,可以獲取數據,但是當我嘗試插入它時,顯示帶有錯誤消息「對象對象」的警告框。在向數據庫插入數據時,在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'); 

感謝。

+0

你只是想提醒的對象,而不是一個字符串。請改用'console.log()'。更好的是,顯示'errorThrown.message'而不是整個'errorThrown'對象。 – JJJ 2013-02-22 17:23:26

+0

感謝您的回覆,但我正在學習WordPress的階段。所以如果你能更具體地說我應該在哪裏使用它,它會更有幫助。再次感謝您的寶貴回覆 – 2013-02-22 17:26:33

+2

您只有一個地方顯示了'errorThrown'。當然你可以自己找到它,並用'console.log(errorThrown.message)'代替它。 (或者如果你不知道如何使用控制檯,alert(errorThrown.message)')。 – JJJ 2013-02-22 17:28:42

回答

0

在你ajax功能,您不包括/發送這action應該是你註冊使用add_action被調用,這是addCustomer,這裏有一個例子

var newCustomerForm = jQuery(this).serialize(); 
jQuery.ajax({ 
    type:"POST", 
    url: "/wp-admin/admin-ajax.php", 
    data: newCustomerForm + '&action=' + addCustomer, // <== 
    success:function(data){ 
     jQuery("#feedback").html(data); 
    }, 
    error: function(errorThrown){ 
     alert(errorThrown); 
    } 
}); 
+0

感謝您的回覆 – 2013-02-22 18:07:08

+0

不客氣:-) – 2013-02-22 18:07:28

相關問題