wordpress插件使用ajax插入,删除和更新自定义表的数据

时间:2014-07-20 10:08:28

标签: php mysql ajax wordpress-plugin

我正在开发一个wp插件来插入,删除和更新自定义表的数据。现在一切正常,即我可以插入,删除和更新表的数据。但我想要的主要是当我点击(执行)每个动作,如插入,删除或更新,每次我需要刷新页面,以查看我的表字段的实际数据。这是我想要解决的问题。我希望在每次操作后都能看到实时数据更新,而无需刷新页面。请帮我解决这个问题我是wp插件的新手。下面是我的完整插件代码。 提前谢谢。

<?php
/**
* Plugin Name: MNM ADMIN
* Plugin URI: http://mnmdating.com
* Description: This is developed specialy for repoting purpose.
* Version: 1.0
* Author: Varinder Kumar
* Author URI: http://URI_Of_The_Plugin_Author
* License: A "Slug" license name e.g. GPL2
*/

  add_action('admin_menu', 'mnm_admin_actions');
  function mnm_admin_actions() 
  {
      add_options_page( 'My Plugin Options', 'MNM ADMIN', 'manage_options', 'my-unique-    identifier', 'mnm_admin', 'mnm_admin');
  }


  function mnm_admin()
  {   
     include 'mnm_config.php';
     $lastname=$_POST['lastname'];
     $id = substr($lastname, 1);
     $operation = substr($lastname, 0, 1);
     switch ($operation) 
     {
       case "i":
          $result = mysql_query("insert into user(email) values('$id')");
          break;
       case "d":
          $result = mysql_query("DELETE FROM user where id ='".$id."'");
          break;
       case "u":
          $result = mysql_query("UPDATE user SET religion='chamar' WHERE id='".$id."'");
          break;
       default:
          echo "Wrong operation";
  }

 if (!current_user_can( 'manage_options' ) )  {
    wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
 }


  wp_enqueue_style('prefix-style', plugins_url('pl-style.css', __FILE__));

  // Register the script like this for a plugin:
  wp_register_script( 'custom-script', plugins_url( '/js/jquery.min.js', __FILE__ ) );

  // For either a plugin or a theme, you can then enqueue the script:
  wp_enqueue_script( 'custom-script' );


  { ?>
  <script type="text/javascript">

  function insert_function(lastname)
  {
  var lastname = lastname;
  var dataString = "lastname=" + lastname;
  $.ajax({
    type: "POST",  
    url: "",  
    data: dataString,
    success: function (data) {

        alert('Operation Successfull');

    return false;
        //run stuff on success here.  You can use `data` var in the 
       //return so you could post a message.  

    }
  });
  } 
  </script>
  <?php }

  echo '<div class="wrap">
  <div id="response"></div>

    <ul class="dash">
     <li class="fade_hover tooltip" title="Add / Edit Assignment">
        <a href="mnm-admin.php?=<?php ?>">
            <img src="'.plugins_url('images/accept.png', __FILE__).'" alt="" />
            <span>Verified Users</span>
        </a>
    </li>
    <li class="fade_hover tooltip" title="Manage Tutors">
        <!--<span class="bubble">3</span> -->
        <a href="show_tutor.php">
            <img src="'.plugins_url('images/help.png', __FILE__).'" alt="" />
            <span>Un Verified Users</span>
        </a>
   </li>

</ul>

    <div style="clear:both;"></div>
    <div class="widget">
    <div class="widget-head"></div>
    <div class="widget-body">
    <table class="table table-bordered table-condensed table-striped table-primary table-vertical-center checkboxs js-table-sortable">
            <thead>
                <tr>
                 <th style="width: 1%;" class="center">No.</th>
                    <th style="width: 20%;">Name / Email</th>
                    <th style="width: 30%;">Mobile</th>
                    <th style="width: 1%;" class="center">City</th>
                    <th style="width: 20%;" class="center">Photo</th>
                    <th style="width: 15%;" class="center">Date Of Birth</th>
                    <th  class="center">Religion</th>
                    <th style="width: 20%;"class="center">Actions</th>
                    <th style="width: 10%;"class="center">Select</th>
                 </tr>
            </thead>
            <tbody>';

               $select = mysql_query("select * from user") ; 
               while($row= mysql_fetch_array($select))
               { $i++; ?> 
                        <tr class="selectable">
                        <td class="center"><?php echo $row['id']; ?></td>
                        <td class="center"><?php echo $row['email']; ?></td>
                        <td class="center"><?php echo $row['mobile']; ?></td>
                        <td class="center js-sortable-handle"><?php echo 'Una'; ?></td>
                        <td class="center"><img style="margin-right:8px;" src="'.plugins_url('images/delete.png', __FILE__).'"></td>
                        <td class="center form-inline small">
                            <?php echo $row['day'].'-'.$row['month'].'-'.$row['year']; ?>
                        </td>
                        <td class="center"><?php echo $row['religion']; ?></td>
                        <td class="center"> 


                            <a href="#" onclick="insert_function('<?php echo 'd'.$row['id']; ?>')"><img style="margin-right:8px;" src="<?php echo plugins_url('images/delete.png', __FILE__); ?>"></a>


                            <a href="#" onclick="insert_function('<?php echo 'i'.$row['id']; ?>')"><img style="margin-right:8px;" src="<?php echo plugins_url('images/edit.png', __FILE__); ?>"></a>


                            <a href="#" onclick="insert_function('<?php echo 'u'.$row['id']; ?>')"><img style="margin-right:8px;" src="<?php echo plugins_url('images/checked.png', __FILE__); ?>"></a>


                        </td>
                        <td class="center"><input type="checkbox"></td>
                </tr>
            <?php   
            }                   
            echo '</tbody>
        </table>
    </div>
   </div> 
 </div>';
}

0 个答案:

没有答案