如何在点击php时从链接发送id值

时间:2013-08-29 04:14:48

标签: php javascript ajax

我在一个页面中有很多forms,有一个按钮可以打开一个隐藏的文本编辑器,目的是根据user_id更新文本文件 所以我遍历所有用户并获得许多这些:

<form action="admin/edittoken.php" method="POST">
    <td><a id="'.$result[0]['user_id'].'" onclick="toggle_visibility(\'feedDiv\');">
        <button onclick="toggle_visibility(\'feed\');" type="button">Feed
        </button><a/></td>
    <td><button class="btn btn-default" type="submit" name="password" >Apply Changes</button></td>
    <td><input name="first_name" class="smallInput" value="'.$result[0]['first_name'].'" type="text" /></td>
</form>

正如您所看到的,当我点击此链接时,每个表单中都有一个我显示id = feedDiv的div

<a id="'.$result[0]['user_id'].'" onclick="toggle_visibility(\'feedDiv\');">

<a>的id是用户ID,使用该数据,我可以提取正确的txt文件进行编辑

我的目标是:

  1. 获取请求显示div(user_id)
  2. 时单击的链接的ID
  3. 将该ID放在php for PDO之间查询并显示正确的txt文件
  4. 这是php

    <div id="feedDiv">
        <form method="POST" action="admin/edittoken.php">
            <textarea id="feed" name="information">
                <?php $user=/ /the a Link Clicked Id Value is the user_id //need help
                here $filename=/ /equal to the PDO Result to get the right text file $handle=f
                open($filename, "r"); $contents=f read($handle, filesize($filename)); fclose($handle);
                echo $contents; ?>
            </textarea>
            <button class="btn  btn-primary yellow" type="submit" name="feed">Send Feed</button>
        </form>
    </div>
    

    到目前为止的Javascript ...

    function toggle_visibility(id) {
        var e = document.getElementById(id);
        if (e.style.display == 'block') e.style.display = 'none';
        else e.style.display = 'block';
    }
    

    JSFIDDLE:http://jsfiddle.net/EC6b4/1/

1 个答案:

答案 0 :(得分:0)

<a id="'.$result[0]['user_id'].'" onclick="toggle_visibility(\'feedDiv\', this.id);">


function toggle_visibility(id, user_id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';

$.post("userdetail.php", { userid: user_id },
  function(data){
    $('#'+id).html(data);
  });

}

userdetail.php包含:

<form method="POST" action="admin/edittoken.php"> 
    <textarea id="feed" name="information">
    <?php
            $user = $_POST['userid']//the a Link Clicked Id Value is the user_id //need help here
            $filename = //equal to the PDO Result to get the right text file
                    $handle = fopen($filename, "r");
            $contents = fread($handle, filesize($filename));
            fclose($handle);
            echo $contents;
        ?>
        </textarea>
        <button class="btn  btn-primary yellow" type="submit" name="feed"  >Send Feed</button>  
        </form>

///// 你的占位符就像

<div id="feedDiv" ></div>