Jump to content

Help with AJAX/PHP output


Darkmatter5

Recommended Posts

To test out some code I have the 3 following pages of code. I can't get the ?r2 parameter I pass to the getTest.php file to pass the right data. I get the following unexpected result of "clientid=3132?r2=ClientID" from ?r1. Can anyone explain why?

 

test.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Byrn & Associates, Inc. online Job database</title>
<style type="text/css">
<!--
.style1 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
}
body {
background-color: #006699;
}
.style3 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 12px;
}
.style7 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style4 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-weight: bold;
}
-->
</style>
<script src="library/test.js"></script>
</head>

<body>
<table width="720" border="0">
  <tr>
    <td colspan="4"><span class="style4">Edit a client record - <span class="style3">Search by selecting from the list</span> </span><span class="style1">below</span></td>
  </tr>
  <tr>
    <td colspan="4"><div align="left"><span class="style7">
      <?php
        include 'library/dbconfig.php';
	include 'library/opendb.php';

	$query="SELECT ClientID, FullName
			FROM byrnjobdb.clients
			ORDER BY FullName ASC";
	$result=mysql_query($query);
	echo "<select name='Client' method='get' onChange='showClientLST(this.value)'>";
	echo "<option>---Select---</option>";
	while ($row=mysql_fetch_array($result))
		{
		$r1=$row['ClientID'];
		$r2=$row['FullName'];
		echo "<option value='$r1'>$r2</option>";
		}
	echo "</select>";

	include 'library/closedb.php';
	?>
    </span></div></td>
  </tr>
  <tr>
    <td width="120"> </td>
    <td width="250"><div align="center">Current record data</div></td>
    <td width="150"><div align="center">Data to update</div></td>
    <td width="200"><div align="center">Function status</div></td>
  </tr>
  <tr>
    <td>ClientID:</td>
    <td><div id="ClientID"></div></td>
    <td> </td>
    <td rowspan="14"><div id="results"></div></td>
  </tr>
  <tr>
    <td>First name:</td>
    <td><div id="FirstName"></div></td>
    <td><div align="center"><input name='FirstName' type='text' id='FirstName'/></div></td>
  </tr>
  <tr>
    <td>Last name:</td>
    <td><div id="LastName"></div></td>
    <td><div align="center"><input name='LastName' type='text' id='LastName'/></div></td>
  </tr>
  <tr>
    <td>Company name:</td>
    <td><div id="CompanyName"></div></td>
    <td><div align="center"><input name='CompanyName' type='text' id='CompanyName'/></div></td>
  </tr>
  <tr>
    <td>Contact Title:</td>
    <td><div id="ContactTitle"></div></td>
    <td><div align="center"><input name='ContactTitle' type='text' id='ContactTitle'/></div></td>
  </tr>
  <tr>
    <td>Address:</td>
    <td><div id="Address"></div></td>
    <td><div align="center"><input name='Address' type='text' id='Address'/></div></td>
  </tr>
  <tr>
    <td>City:</td>
    <td><div id="City"></div></td>
    <td><div align="center"><input name='City' type='text' id='City'/></div></td>
  </tr>
  <tr>
    <td>State:</td>
    <td><div id="State"></div></td>
    <td><div align="center"><input name='State' type='text' id='State'/></div></td>
  </tr>
  <tr>
    <td>Zip code:</td>
    <td><div id="ZipCode"></div></td>
    <td><div align="center"><input name='ZipCode' type='text' id='ZipCode'/></div></td>
  </tr>
  <tr>
    <td>Home phone:</td>
    <td><div id="HomePhone"></div></td>
    <td><div align="center"><input name='HomePhone' type='text' id='HomePhone'/></div></td>
  </tr>
  <tr>
    <td>Work phone:</td>
    <td><div id="WorkPhone"></div></td>
    <td><div align="center"><input name='WorkPhone' type='text' id='WorkPhone'/></div></td>
  </tr>
  <tr>
    <td>Extension:</td>
    <td><div id="Extension"></div></td>
    <td><div align="center"><input name='Extension' type='text' id='Extension'/></div></td>
  </tr>
  <tr>
    <td>Fax phone:</td>
    <td><div id="FaxPhone"></div></td>
    <td><div align="center"><input name='FaxPhone' type='text' id='FaxPhone'/></div></td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><div id="Email"></div></td>
    <td><div align="center"><input name='Email' type='text' id='Email'/></div></td>
  </tr>
  <tr>
    <td colspan="3"><div align='center'><input name='editclient' type='submit' id='editclient' value='Store new client data' /></div></td>
    <td> </td>
  </tr>
</table>
</body>
</html>

 

test.js

// JavaScript Document
var xmlHttp
var fields=new Array("ClientID","FirstName","LastName");
var i=0;

//while (i<=2) {
function showClientLST(str) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="http://byrndb01/byrndb/library/getTest.php"
	url=url+"?r1="+str
	url=url+"?r2="+fields[i]
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		document.getElementById(fields[i]).innerHTML=xmlHttp.responseText
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml12.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
//	i++;
//}

 

getTest.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$clientid=$_GET["r1"];
$field=$_GET["r2"];

include 'dbconfig.php';
include 'opendb.php';

echo "clientid=" . $clientid . "<br>";
echo "field=" . $field;

include 'closedb.php';
?>
</body>
</html>

 

My ultimate plan is for the divs ClientID, FirstName and LastName to be displayed with the queried data from a database. I wanted to use a while loop to run the dynamic code in test.js to display that data in it's respective div in test.php, but I've commented out the while statement for now until I can fix the parameter problem.

 

Help please!

Link to comment
https://forums.phpfreaks.com/topic/106327-help-with-ajaxphp-output/
Share on other sites

Okay I got it working, but how can I do the while or even a for statement in the test.js file??  Why would the following code not work?

 

// JavaScript Document
var xmlHttp
var fields=new Array("ClientID","FirstName","LastName");

for (i=0;i<=2;i++) {
function showClientLST(str) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="http://byrndb01/byrndb/library/getTest.php"
	url=url+"?r1="+str
	url=url+"&r2="+fields[i]
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		document.getElementById(fields[i]).innerHTML=xmlHttp.responseText
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml12.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.