Jump to content

PHP MySQL AJAX help


Darkmatter5

Recommended Posts

Here are snippets of  3 seperate files. The first code is of the file search.php, the second is of the file select_searchclients.js and the third is of the file get_searchclients.php.  In search.php I have 2 seperate ways to find and display client info from a database.  First by text boxes and second by a list complied via a query.  I want both query results to display at the div labeled "clientresults".

 

search.php

<head>
<script src="library/select_searchclients.js"></script>
</head>
<body>
<td bgcolor="#FF0000">
<form id="editclient" name="editclient" method="post" action="">
<label>First Name:<input name="FirstName" type="text" id="FirstName" tabindex="1" /></label><br>      
<label>Last Name:<input name="LastName" type="text" id="LastName" tabindex="2" /></label><br>
<label>Company Name:<input name="CompanyName" type="text" id="CompanyName" tabindex="3" /></label></span></form>
</td>
<td width="431" rowspan="2" bgcolor="#F3F9B7">
<div align="center">
<?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='showClient(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';
?>
</div></td></tr>
<form><div align="center">
<input name="searchclients" type="submit" id="searchclients" onClick="showClient(this.value)" value="Click to search by search parameters"></div></form></td></tr>
<tr><td colspan="2" bgcolor="#F3F9B7"><div class="style6" id="clientresults">SEARCH RESULTS WILL APPEAR HERE</div></td>

 

select_searchclients.js

// JavaScript Document
var xmlHttp

function showClient(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
	alert ("Browser does not support HTTP Request")
	return
}
var url="http://byrndb01/byrndb/library/get_searchclients.php"
url=url+"?r1="+str
url=url+"?FirstName="+str
url=url+"?LastName="+str
url=url+"?CompanyName="+str
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("clientresults").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;
}

 

get_searchclients.php

<?php
$clientid=$_GET["r1"];
$FirstName=$_GET["FirstName"];
$LastName=$_GET["LastName"];
$CompanyName=$_GET["CompanyName"];

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

IF ($clientid != NULL)
{
$query="SELECT ClientID, FirstName, LastName, CompanyName, HomePhone, WorkPhone
		FROM byrnjobdb.clients
		WHERE ClientID=$clientid";
$result=mysql_query($query);

echo "<table border='1'>
	  <tr>
	  <th>ClientID</th>
	  <th>First Name</th>
	  <th>Last Name</th>
	  <th>Company Name</th>
	  <th>Home Phone</th>
	  <th>Work Phone</th>
	  </tr>";

while($row=mysql_fetch_array($result))
{
	echo "<tr>";
	echo "<td><a href='addclient.php' target='mainFrame'>" . $row['ClientID'] . "</a></td>";
	echo "<td>" . $row['FirstName'] . "</td>";
	echo "<td>" . $row['LastName'] . "</td>";
	echo "<td>" . $row['CompanyName'] . "</td>";
	echo "<td>" . $row['HomePhone'] . "</td>";
	echo "<td>" . $row['WorkPhone'] . "</td>";
	echo "</tr>";
}
echo "</table>";
}
ELSE
{
$query="SELECT ClientID, FirstName, LastName, CompanyName, HomePhone, WorkPhone
		FROM byrnjobdb.clients
		WHERE FirstName=$FirstName, LastName=$LastName, CompanyName=$CompanyName";
$result=mysql_query($query);

echo "<table border='1'>
	  <tr>
	  <th>ClientID</th>
	  <th>First Name</th>
	  <th>Last Name</th>
	  <th>Company Name</th>
	  <th>Home Phone</th>
	  <th>Work Phone</th>
	  </tr>";

while($row=mysql_fetch_array($result))
{
	echo "<tr>";
	echo "<td>" . $row['ClientID'] . "</td>";
	echo "<td>" . $row['FirstName'] . "</td>";
	echo "<td>" . $row['LastName'] . "</td>";
	echo "<td>" . $row['CompanyName'] . "</td>";
	echo "<td>" . $row['HomePhone'] . "</td>";
	echo "<td>" . $row['WorkPhone'] . "</td>";
	echo "</tr>";
}
echo "</table>";
}

include 'closedb.php';
?>

 

With the code like this I get an error at div "clientresults" saying "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/byrndb/library/get_searchclients.php on line 35".  Also with the code like this am I passing the textboxes through the javascript and into the get_searchclients.php file correctly?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/103417-php-mysql-ajax-help/
Share on other sites

I entered the code suggested and here is the error it reports.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?FirstName=2254?LastName=2254?CompanyName=2254' at line 3

 

Obviously depending on which client I select from the list the "2254" changes as it's their table ID.  It seems as though r1 is being passed to get_searchclients.php as "2254?FirstName=2254?LastName=2254?CompanyName=2254".  It seems like the javascript is wrong.  Before if I have the code like this

 

select_searchclients.js

function showClient(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
	alert ("Browser does not support HTTP Request")
	return
}
var url="http://byrndb01/byrndb/library/get_searchclients.php"
url=url+"?r1="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

 

It would return the appropriate result.  So how can I pass r1(list selection), FirstName(text entered), LastName(text entered) and CompanyName(text entered) through the javascript correctly.  I just want them to populate the variables in get_searchclients.php so I can use them later.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/103417-php-mysql-ajax-help/#findComment-529602
Share on other sites

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.