douzhongpi9734 2014-07-20 10:08
浏览 52

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

i am developing a wp plugin to insert, delete and update data of custom table. now everything is working fine ie, i can insert, delete and update data of table. But the main thing that i want is when i click (perform ) every action like insert, delete or update, every time i need to refresh the page to see the actual data of my table fields. This is the problem that i want to resolve. I want to see the live data update after every operation without refreshing the page. Please help me to resolve this problem i am new to wp plugin. below is my complete plugin code. Thanks in advance.

<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)