Trouble passing PHP variable "back" to calling page

Hi,
I have 2 pages. Page-1.php generates an AJAX xmlhttp request to call Page-2.php.

Page-2.php calls a MySql database and successfully retrieves some rows. This is code snippet:
[php]$result = $mysqli->query($sql);
$num = $result->num_rows;

$field[] = " ";
for ($r=1; $r<=$num; $r++)
{
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
$field[fname][$r] = $row[“fname”];
$field[lname][$r] = $row[“lname”];
}[/php]
NOW, I return to Page-1.php.

The code here do display the 2 fields:

<td> <font face="Arial, Helvetica, sans-serif"><?php echo $field[fname][1]; ?></font> </td> <td> <font face="Arial, Helvetica, sans-serif"><?php echo $field[lname][1]; ?></font> </td>

BUT nothing comes back.
I am missing something really dumb, please help.
Thanks

EDIT: Please use code tags. astonecipher

Post your JS page for the Ajax.

Here is the code for Page-1. Note that it is in progress but should work as is.

<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","AjaxGetUser.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">person 1</option>
<option value="2">person 2</option>
<option value="3">person 3</option>
</select>
</form>
<br />....................................................................all of the above code works as it should.

<?php echo "Row count: " . $num ."</br>"; ?>............................this does not return $num only a blank.  WHY??????

…If I can remedy the above line then I can fix the whole thing.

Person info will be listed here-1.

…the table headers below do appear but none of the detail lines below that.


                        <table id="mysqlTable">
                        <tr>
                        <td>
                        <font face="Arial, Helvetica, sans-serif">Row Count</font>&nbsp;
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif">Season</font>&nbsp;
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif">Comp Number</font>&nbsp;
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif">First Name</font>&nbsp;
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif">Last Name</font>&nbsp;
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif">Initials</font>&nbsp;
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif">Digital Class</font>&nbsp;
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif">Print Class</font>&nbsp;
                        </td>
                        </tr>
                        
                        <tr>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[season][1]; ?></font>&nbsp;
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[compnum][1]; ?></font>
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[fname][1]; ?></font>
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[lname][1]; ?></font>
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[initials][1]; ?></font>
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[compclass][1]; ?></font>
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[printclass][1]; ?></font>
                        </td>
                        </tr>
                        
                        <tr>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[season][2]; ?></font>&nbsp;
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[compnum][2]; ?></font>
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[fname][2]; ?></font>
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[lname][2]; ?></font>
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[initials][2]; ?></font>
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[compclass][2]; ?></font>
                        </td>
                        <td>
                        <font face="Arial, Helvetica, sans-serif"><?php echo $field[printclass][2]; ?></font>
                        </td>
                        </tr>                                   
                                                                                                         
                        </table>
<div id="txtHint2"><b>Person info will be listed here.</b></div>                        
 
</body>
</html>

Is this all on the same 1 page and then the database calls are on page 2? Just want to make sure I am understanding. If the code below is on one page, the php needs to be above the html.

The php code cannot parse if the page has already been interpreted.

The HTML and xmlhttp request call are all on one and the same page.
The call to MySQL is on the second page which is all php code.

I am now starting to think my problem is a timing issue.
ie Do my<?php echo fields on page-1 get updated only once when the page is first loaded?
If so then that is my problem.

How to solve that??

Thanks

Yes your echo fields will only load once. However, you could name your table cells ( I won’t fuss about using tables in this instance.), and then you could use ajax calls to update the cells in the table.

Thanks for all your help with this.

I am stuck with how to write back to my HTML page to a specific field or table cell.
You mentioned that it would need to be named! I can do that part, but what is the PHP echo syntax to write to any specific field ideally in a loop as I move through a series of rows returned by MySql.

But , I need just a very simple example to get started.
I have searched a lot on the web forums for this.

Thanks

Here is a working example using PDO

[php]

function read()
{


	try {
		$pdo = new PDO($this->CLhostname, $this->CLusername, $this->CLpassword); //////////////////////////////////
		$sth = $pdo->query("SELECT * FROM `Email_Contacts`  ORDER BY chapter_num ASC");   
		$sth->setFetchMode(PDO::FETCH_OBJ); 
	        
		echo  '<h2 style="margin-left: 30%;">Members List</h2>' .
		      '<div id="member_select style="width=80%; height: 10%; overflow: auto; margin: 5px; white-space: nowrap;">';
				
		echo '<table border="1"><tr><th>Last Name</th><th>First Name</th><th>Chapter</th><th>Email</th>'
						<th>Phone Number</th><th>Address</th><th>City</th><th>State</th><th>Zip</th></tr>';	
		while($row = $sth->fetch()) { 
			echo	'<tr>
					
					<td id=""lname">' . $row->lname . '</td>  
		    		<td id=""fname">' . $row->fname . '</td>  
		    		<td id=""ch_num">' . $row->chapter_num . '</td>
				    <td id=""email">' . $row->email . '</td>
				    <td id=""ph_num">' . $row->phone . '</td>
				    <td id=""address">' . $row->address . '</td>
				    <td id=""city">' . $row->city . '</td>
				    <td id=""state">' . $row->state . '</td>
				    <td id=""zip">' . $row->zip . '</td>
				    <td><a href="?view=member&update=' . $row->ID . '" name="update">Update</a></td>
				    <td><a href="?view=member&remove=' . $row->ID . '" name="remove">Delete</a></td>

				    </tr>';
		}
		
		echo '</table>
				
				</form></div>';
	} catch(PDOException $e) {  
		echo "Error fetching Members: " . $e->getMessage(); 
	} 
	
}

[/php]

Thanks very much for your sample code. I appreciate it.

However I am looking for the following:

In my page-1, I would have this sort of code:

row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2
............. ............. Notice that all my cells have names! ....................................Now that my cells are named I hope to write to them in my php page as follows: page-2




echo… print or write $my_mysql_ whatever_variable to cell12, etc,etc,etc…this the line I really need help with.

Notice that cell12 and the other 3 cells already exist and just need to be populated.

Thanks again.

If your table size depends on what is in the database it is best to have php create it. If however you know the size, naming the cells lets you use AJAX to decide what goes in each cell.

Sponsor our Newsletter | Privacy Policy | Terms of Service