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";


	}


?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.