Jump to content

[SOLVED] Sending a PHP variable to a JS script


mark110384

Recommended Posts

Hey guys I been developing an AJAX portion of my website and I'm nearly there but I have come across a problem, I have 2 DIVs, one that displays the item number and name when the request is sent and another that is populated when the user selects the item, this includes a price and description. How would I send variables from my php script that aren't in the echo string to populate the second DIV? Hope it makes sense the Coding is as follows. Thanks

 

JS

 


//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
	return new XMLHttpRequest();
} else if(window.ActiveXObject) {
	return new ActiveXObject("Microsoft.XMLHTTP");
} else {
	alert("Your browser does not support this function!");
}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest() {

//Check if the search field is empty. If so empty Search Suggest inner HTML.

if (document.getElementById('txtSearch').value == "") {
	document.getElementById('search_suggest').innerHTML = '';
}

else if (searchReq.readyState == 4 || searchReq.readyState == 0) {
	var str = escape(document.getElementById('txtSearch').value);
	searchReq.open("GET", 'searchSuggest.php?search=' + str+'&sid='+Math.random(), true);
	searchReq.onreadystatechange = handleSearchSuggest; 
	searchReq.send(null);
}

}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
if (searchReq.readyState == 4) {
	var ss = document.getElementById('search_suggest')
	ss.innerHTML = '';
	var str = searchReq.responseText.split("\n");
	for(i=0; i < str.length - 1; i++) {

		var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
		suggest += 'onmouseout="javascript:suggestOut(this);" ';
		suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';

		suggest += 'class="suggest_link">' + str[i] + '</div>';
		ss.innerHTML += suggest;
	}
}

}

//Mouse over function
function suggestOver(div_value) {
div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
div_value.className = 'suggest_link';
}
//Click function
function setSearch(value) {
document.getElementById('chart_info').innerHTML =  value + "Price:" + ("<? echo $price; ?>"); 

}

 

PHP

 

<?
require('../functions.php');

$connect = @ConnectToAgents($dbhost, $dbuser, $dbname);

$search = $_GET['search'];
$pubtype = 'Chart';

$sql = "SELECT * FROM products WHERE (Code LIKE '%" . $search . "%' OR Description LIKE '%" . $search . "%') AND Type = '$pubtype' limit 0,20";






	$result = mysql_query($sql);	

	while($myrow = mysql_fetch_array($result)) 

	{

		$chartcode = $myrow['Code'];

		$chartp = $myrow['PPost'];

		$chartdescription = $myrow['Description'];

		$price = $myrow['Price'];

		echo "$chartcode $chartp - $chartdescription \n";


	}


?>

 

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.