doushan6161 2013-07-16 19:16
浏览 51

PHP CRUD,使用Javascript(PostLink)将href $ _GET链接转换为$ _POST

I have a MySQL database with a PHP front end. Once the user's records are queried from the database, they are displayed in a table. I created a CRUD where the user can SELECT (to view additional details), DELETE, or EDIT. The CRUD uses Javascript up to the point that the client sends the query to the DB, then redraws the index page to reflect the changes in the DB.

I currently use a Javascript $_GET, but want to change it to a $_POST to hide the MySQL table id. I want to use something like jQuery PostLink. I only had a class in Java, I understand Javascript, but I am not thge best JS programmer.

Here is the script:

    <script src="http://postlink.googlecode.com/svn/trunk/jquery.postlink.js" type="text/javascript" ></script>

    <script type="text/javascript">
      $(document).ready(function() {
        $('a').postlink();
      });
    </script>

Here is the PHP code that draws my table:

    foreach ($query as $row){
            $id =  $row['document_id'];
            echo ('<tr>');
            echo ('<td>' . $row [clientName] . '</td>');
            echo ('<td>' . $row [documentNum] . '</td>');
            echo "<td><a href='editDocument.php?tableau={$id}'>Edit</a>";
            echo " / ";
            echo "<a href='#' onclick='deleteDocument( {$id} );'>Delete</a></td>";
            // this calls Javascript function deleteDocument(id) stays on same page
            echo ('</tr>');   
    } //end foreach

I do not want to have to add form tage (<form> </form>) either. I want to call a script (like jQuery PostLink) that will work with the code that I have and convert href $_GET link into a $_POST. I want it to redirect to editDocument.php and have $_POST[tableau]=$id. I have been (unsuccessfully) trying to do this by "class:"

echo "<td><a class='postlink' href='editDocument.php?tableau={$id}'>Edit</a>";

I am having 2 problems with PostLink:

  1. The example given converts all <a> to a $_POST (including the DELETE link).
  2. The lack of documentation and sample code for PostLink.

Any thoughts???

Thank you in advance.

  • 写回答

4条回答 默认 最新

  • donglin6109 2013-07-16 19:42
    关注
    1. Change the code to select only the link you are interested in. If this is the HTML code:

      echo <a class='postlink' href='editDocument.php?tableau={$id}'>Edit</a>;
      

      You can change the Javascript snippet to this:

      $(document).ready(function() {
          $('.postLink').postlink();
      });
      
    2. The plugin does not seem to be very used and is very old (2010). I would proceed with caution.

    I would actually suggest to change your approach, if you really want to hide the table ID. Even if you change the code to fire a POST with javascript, anyone can just look at the network requests with some browser developer tool like Firebug.

    One approach would be to change the MySQL table to store an extra key and change the PHP code to pass and accept the key as input. But the approach chosen depends on your specific implementation and use case.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题