-1

我正在使用 AJAX 和 PHP 从数据库中检索数据。在数据库中,其中一列包含图像文件夹的路径。我将路径的值保存在名为 $folder 的 PHP 变量中。这可以在 getuser.php 代码中看到。
我希望此变量在 one.php 中可见/可用,以便可以填充使用此变量的图像。我该怎么做。我也尝试过包含php,但没有用。



获取用户.php

   <?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'san', '123');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("holidayNet", $con);

$sql="SELECT * FROM image WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Picture</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
    $folder = $row['FirstName'];
  echo "<tr>";
 echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";


  /*echo "<td>" . $row['Job'] . "</td>";*/
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);


?> 



一个.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Sn Qb</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div><br />

<img src="<?php echo $folder;?>/pic1.jpg" />
<img src="<?php echo $folder;?>/pic2.jpg" />
<img src="<?php echo $folder;?>/pic3.jpg" />
<img src="<?php echo $folder;?>/pic4.jpg" />


</body>
</html> 
4

3 回答 3

2

嘿,您正在 AJAX 调用的 PHP 文件 (getuser.php) 中创建变量名称 $folder。但它在文件名 one.php 中不可用。

只有您从 getuser.php 回显的内容才会在 JS 变量中可用xmlhttp.responseText

因此,您将获得 getuser.php 中回显的所有人员信息,但不会获得 $folder 变量。

由于您已经专门硬编码了 4 张图像。我建议您在 getuser.php 中也回显 img 标签以及所选人员数据库中的其他信息。

while($row = mysql_fetch_array($result))
{
    $folder = $row['FirstName'];
    echo "<tr>";
    echo "<td>" . $row['FirstName'] . "</td>";
    echo "<td>" . $row['LastName'] . "</td>";
    echo "<td>" . $row['Age'] . "</td>";
    echo "<td>" . $row['Hometown'] . "</td>";


    /*echo "<td>" . $row['Job'] . "</td>";*/
    echo "</tr>";
}
echo "</table>";

for($i = 0; $i < 4; $i++)
{
    echo '<img src="'.$folder.'/pic'.($i+1).'.jpg" />';
}

并从 one.php 页面中删除这些图像标签

另一个解决方案:我可以给出的建议是添加一些分隔符来区分这两件事。一个是您要打印的表格和 $folder 变量值。例如:考虑分隔符####

第 1 步:所以现在您的 getuser.php 代码将是

while($row = mysql_fetch_array($result))
{
    $folder = $row['FirstName'];
    echo "<tr>";
    echo "<td>" . $row['FirstName'] . "</td>";
    echo "<td>" . $row['LastName'] . "</td>";
    echo "<td>" . $row['Age'] . "</td>";
    echo "<td>" . $row['Hometown'] . "</td>";


    /*echo "<td>" . $row['Job'] . "</td>";*/
    echo "</tr>";
}
echo "</table>####".$folder;

第 2 步:更改 one.php 以分隔 2 个值

    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
// split is the function which breaks the string with the value provided and then creates an array of the parts.
        var arrResponse = (xmlhttp.responseText).split("####");
        // added || below to validate the index 1 of arrResponse. if xmlhttp.responseText does not have #### it wont break the string and hence wont have the folderName part, in such cases javascript will give undefined value, by adding || we tell if value is present take the present value otherwise take it as an empty string ''
    var folderName = arrResponse[1] || ''
        document.getElementById("txtHint").innerHTML=arrResponse[0];

        // we will fetch all the image tags from your html page
        var arrImgs = document.getElementsByTagName('img');
        var imgCount = arrImgs.length;
        for(i = 0; i < imgCount; i++)
        {
            arrImgs[i].setAttribute('src', folderName+'/pic'+(i+1)+'.jpg');
        }
    }
于 2012-05-05T20:02:28.280 回答
0

你做错的是你把东西混合在一起,特别是 PHP 和 HTML,如果你愿意,我可以更好地解释,但目前我只会尝试解释你的问题的解决方案,我希望这对你。

1/ $folder 将无法从 one.php 访问

2/ 要从 $folder 中获取信息,您最好在 Ajax Query 中获取它,然后您可以使用 Javascript 函数更改属性

3/ 另一个解决方案是,由于您正在为 getuser.php 中的表生成 html 代码,您还可以生成更多包含图像标签的行

我解释得好吗?请如果没有,我准备再试一次:)

祝你好运

于 2012-05-05T19:56:44.327 回答
0

根据您的要求,我会告诉您一个可能的解决方案,使用与您之前相同的方式

首先,你需要改变你的两个文件

1/ 转到 getuser.php 并删除所有回显,将字符串存储在变量中

2/比,在循环之后用你的两个变量($folder和包含html字符串的新变量)创建一个索引数组

3/ 之后,您可以使用“json_encode”函数将数组编码为 json 格式(自 PHP5 起可用)

4/ 你还需要做的就是在 one.php 页面中,当你得到结果时你需要用“eval”函数(一个原生 Javascript 函数)解码你得到的字符串,这个函数将返回一个索引数组(具有相同的索引)并将html字符串放在“txtHint” div中,使用图像标签中其他变量的内容(使用javascript函数)

这足够清楚吗?(对不起,我的英语不是那么好:()

于 2012-05-05T20:21:16.950 回答