Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将AJAX处理合并到PHP/MySQL While循环中(用于异步编辑)?_Php_Javascript_Mysql_Ajax_While Loop - Fatal编程技术网

如何将AJAX处理合并到PHP/MySQL While循环中(用于异步编辑)?

如何将AJAX处理合并到PHP/MySQL While循环中(用于异步编辑)?,php,javascript,mysql,ajax,while-loop,Php,Javascript,Mysql,Ajax,While Loop,滚动以查找工作代码 我在一个AJAX编辑平台上工作,在设置文本字段进行编辑时遇到了问题 到目前为止,我的代码是: 下面的Javascript处理初始提交: <head> <script type="text/javascript"> if(window.ActiveXObject) var ajax = new ActiveXObject('Microsoft.XMLHTTP'); else var ajax = new XMLHttpRequest(); functi

滚动以查找工作代码

我在一个AJAX编辑平台上工作,在设置文本字段进行编辑时遇到了问题

到目前为止,我的代码是:

下面的Javascript处理初始提交:

<head>
<script type="text/javascript">
if(window.ActiveXObject) var ajax = new ActiveXObject('Microsoft.XMLHTTP');
else var ajax = new XMLHttpRequest();

function edit()
{
   var doc_id = document.getElementById("doc_id").value;
   ajax.open('GET', 'ajax.php?doc_id='+doc_id, true);
   ajax.onreadystatechange = function()
   {
      if(ajax.readyState == 4)
      {
         document.getElementById('content').innerHTML = ajax.responseText;
      }
   }
   ajax.send(null);
}
</script>
</head>
下面的SQL处理该信息的初始选择查询和显示:

$query = 'SELECT pp.`physician_id`, pp.`physician_first_name`, pp.`physician_last_name`, pp.`featured`, ';
$query.= 'FROM `primary_physicians` AS pp ';
$query.= 'ORDER BY pp.`physician_id` ';

<body>
<div id="container">
  <?php
    $result = mysql_unbuffered_query( $query );
    echo "<table border='1'>"; 
    while ($row = mysql_fetch_assoc($result))
    {   
        echo "<tr>";
        $physician_id = $row['physician_id'];
        echo "<td>" . $row['physician_id'] . "</td>";
        echo "<td><div id='content'><input id='doc_id' type='hidden' value='$physician_id' />" . $row['physician_first_name'] . "<br /><input type='button' value='Edit' onclick='edit();'></div></td>";
        echo "<td>" . $row['physician_last_name'] . "</td>";
echo "</tr>";
    }
    echo "</table>";

?>
</div>
</body>
</html>
当用户单击“content”div中的“Edit”按钮时,“ajax.php”文件处理请求

    $client_id = $_GET['doc_id'];
$client_query = 'SELECT pp.`physician_id`, pp.`physician_first_name`, pp.`physician_last_name`, pp.`featured` ';
$client_query.= 'FROM `primary_physicians` AS pp WHERE pp.`physician_id`=' . $client_id . '';
$client_result = mysql_unbuffered_query( $client_query );

while ($client_row = mysql_fetch_assoc($client_result))
    {
    echo "<input type='text' value='$client_row[physician_first_name]' />";
    }
以下内容如下:

初始页面加载:

按“编辑”按钮可选择任何可用按钮,而不仅仅是与客户端/ID关联的按钮:

按下任何编辑按钮都会在客户端ID 1的表行中显示客户端ID 2,而不是在客户端ID为2的行中:

我猜我必须在content div中设置一些东西,并以某种方式将其与“edit”函数关联起来,但是如果不在while循环中设置脚本,我就无法理解如何做到这一点,我真的不想这样做

工作代码如下

Javascript初始提交和显示:

<head>
<script type="text/javascript">
if(window.ActiveXObject) var ajax = new ActiveXObject('Microsoft.XMLHTTP');
else var ajax = new XMLHttpRequest();

function hello(e)
{
   /* this was once document.getElementById("doc_id").value;*/
   var doc_id = e.currentTarget.id;
   ajax.open('GET', 'ajax.php?doc_id='+doc_id, true);
   ajax.onreadystatechange = function()
   {
      if(ajax.readyState == 4)
      {
         /*this was without the '+doc_id' document.getElementById('content').innerHTML = ajax.responseText; */
         document.getElementById('content'+doc_id).innerHTML = ajax.responseText;
      }
   }
   ajax.send(null);
}
</script>
</head>
PHP/MySQL:

<body>
<div id="container">
  <?php
    $result = mysql_unbuffered_query( $query );
    echo "<table border='1'>"; 
    while ($row = mysql_fetch_assoc($result))
    {   
        echo "<tr>";
        $physician_id = $row['physician_id'];
        echo "<td>" . $row['physician_id'] . "</td>";
            //note change to the 'content' div (addition of $physician_id to make it truly unique; this ties into the javascript above.
        echo "<td><div id='content$physician_id'>";
            //note changes to input id and button id, as well as the 'onclick' function.
            echo "<input id='doc_id_$physician_id' type='hidden' value='$physician_id' />" . $row['physician_first_name'] . "<br /><input type='button' id='$physician_id' value='Edit' onclick='hello(event);'></div></td>";
        echo "<td>" . $row['physician_last_name'] . "</td>";
echo "</tr>";
    }
    echo "</table>";

?>
</div>
</body>

没有更改初始MySQL查询或ajax.php,元素的ID有问题。请记住,id属性在文档中应该是唯一的。您对许多元素使用相同的id:

td><div id='content'><input id='doc_id' type='hidden' ...
当然,您必须在编辑按钮上设置id,并使其与您输入的id相对应。例如,按钮id使用$i,输入id使用doc_id_$i


我还建议您看看jQuery,因为它将有助于实现您在这里尝试实现的许多目标。

使用“class”而不是“id”会起作用吗?而且,我不敢相信我忘了“id”只能用一次。它可以,但仅仅把它改成class是不够的。请看上面我编辑的答案。谢谢,这很有效!我正在上面添加我现在正在使用的代码。
function edit(e) {
    buttonId = e.currentTarget.id;
    //use the button id to find the proper value
}