gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:01:15 26 4
gpt4 key购买 nike

滚动查找工作代码

我正在使用 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>

当用户单击“内容”div 中的“编辑”按钮时,“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]' />";
}

显示内容如下:

初始页面加载:

table display

按“编辑”按钮(任何可用按钮,而不仅仅是与客户端/ID 关联的按钮): attempting an edit shows client ID #1

按任何编辑按钮都会在客户端 ID #1 的表行中显示客户端 ID #2(不在客户端 ID #2 的行中): pressing any edit button shows client ID #2 within client ID #1's table row

我猜我必须在内容 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' ...

在循环内。

然后使用 Javascript document.getElementById('doc_id')。 JS 假设页面上只有一个具有此 id 的元素,因此它将始终返回它找到的第一个元素。

编辑:

您还必须更改 JS 函数才能检索正确的值:

对于编辑按钮,请使用:onclick="edit(event)"

然后在JS中:

function edit(e) {
buttonId = e.currentTarget.id;
//use the button id to find the proper value
}

当然,您必须在“编辑”按钮上设置 id,并使其与您输入的 id 相对应。例如。使用 $i 作为按钮 id,使用 doc_id_$i 作为输入 id。

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

关于php - 如何将 AJAX 处理合并到 PHP/MySQL While 循环(用于异步编辑)中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15023732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com