Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7880773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:13:28+00:00 2026-06-03T04:13:28+00:00

I am retrieving data from database using AJAX and PHP. In database one of

  • 0

I am retrieving data from database using AJAX and PHP. In database one of the columns contains path of an image folder. I am saving the value of path in a PHP variable named as $folder. This can be seen in getuser.php code.
I want this variable to be visible/available in one.php so that my images using this variable could be populated. How would i do this. I have tried including php as well but no use.

getuser.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);


?> 

one.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> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:13:30+00:00Added an answer on June 3, 2026 at 4:13 am

    Hey you are creating a variable name $folder in the PHP file (getuser.php) which is getting called by AJAX. But its not available in the file name one.php.

    Only what you echo from getuser.php will be available in the JS variable xmlhttp.responseText

    So you will get all the Person Info echoed in the getuser.php, but wont get the $folder variable.

    Since you have specifically hardcoded 4 images. I suggest you to echo the img tags too in the getuser.php along with the other information from the database of the person selected.

    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" />';
    }
    

    And remove those image tags from the one.php page

    The other Solution:
    Suggestion which I can give is to add some separator to differentiate between the 2 things. One is the table which you want to print and the $folder variable value.
    For eg: consider separator ####

    Step 1:
    So now your code from the getuser.php will be

    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;
    

    Step 2:
    Changes in the one.php to separate the 2 values

        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');
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with retrieving data from a database using PHP statements. At
Hi im retrieving some data from a database using JSON and ajax. When i
I am retrieving data from a database, where the field contains a String with
I am using a custom Adapter class. And retrieving data from database using cursor.
I'm having trouble retrieving data from my database using Spring Jdbc. Here's my issue:
I'm using the codeigniter framework, I'm retrieving data from the database in the form
For some reason I'm having a problem retrieving data from my database. It leaves
I am retrieving data from an Oracle database and binding the same to a
Currently, I'm retrieving some data from a MySQL database table, but the problem is
Traditionally when using a DbCommand when retrieving data from a sproc, something like the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.