Working on this off and on for two weeks! Probably super simple with someone with more experience. I simple have a food search engine. You enter in lets say "blue cheese" it pulls all things with blue cheese from my DB. I click on one of the blue cheese links and it then pulls calories from my DB. I got all that working. But in order for my chart to display I have to select an item from the select box. I have the select box in a while loop pulling from a column in the DB and populating but cant link it.

Ok, so to try and make more sense of my question let me post a link to what I trying to do and what I've done. Thanks!

This is my page what I'm trying to do, just by looking at it hopefully you can get my concept. In the search engine type in "blue cheese". What comes up is 1 oz of blue cheese, 1 cup etc etc.. But in order for my nutrition facts to pop up I have to select one first..

http://simplecalorie.com/search.html

this is that code I use to kinda do this:

http://www.w3schools.com/PHP/php_ajax_database.asp

thanks!

Recommended Answers

All 2 Replies

Since you are filling the Select Box with the Unit of Measeure data from your database why not have the value attribute contain the calories for each of the different unit of measures. That way you can easily update the information on your page using javascript and not have to try and make another database request.

You can use the onchange event, of the select box, to perform the update.

Thats exactly what I want! But when I try to do the onchange event I cant get the javascript to work without having it open on another page. This is the JS I'm using, at the bottom it sends it to test.php to process. I would like to keep it all on one page. Since all my variable from the DB are sent to this page.Is there a way I could simplify this script:

<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","test.php?q="+str,true);
xmlhttp.send();
}
</script>

Here is the full code:

<?php
session_start();
$q=$_GET["q"];
$num = $_GET['num'];
$test = $_GET['oneGram1'];
$_SESSION['oneGram']=$test;

$connect = mysql_connect("", "", "") or die("couldnt connect");
	mysql_select_db("") or die("couldnt connect");
?>
<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","test.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
$weight = mysql_query("SELECT * FROM WEIGHT WHERE NDB_No = $num");
		echo"<form>";
		echo"<select name='users' onchange='showUser(this.value)'>";
		while($row5 = mysql_fetch_array($weight))
	    {
			//$cal = $row5['Gm_Wgt'];
			echo"<option value='$row5[Msre_Desc]'>$row5[Msre_Desc]</option>";
				
			
		}
		echo"</select>";
		echo"<div id='txtHint'><b>Person info will be listed here.</b></div>";
		echo"</form>";
		$_SESSION['num1']=$num;
echo"myyyyy $test <br>";
echo"$num <p>";
$sql1="SELECT * FROM WEIGHT WHERE NDB_No = '$num' AND Seq = 1";
$result1 = mysql_query($sql1);
while($row = mysql_fetch_array($result1))
  {
  $calories = $row['Gm_Wgt']*$test;
  echo"<br>$test";
  echo"<br>Calories = $calories";
  echo "<br />";
  }
?>

<br />


</body>
</html>

And here is test.php:

<?php session_start();
$oneGram = $_SESSION['oneGram'];
$test = $_SESSION['num1'];


?> 
<!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>
<link href="test.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
session_start();
$q=$_GET["q"];
$connect = mysql_connect("", "", "") or die("couldnt connect");
	mysql_select_db("") or die("couldnt connect");

$sql="SELECT * FROM WEIGHT WHERE NDB_No = '$test' AND Msre_Desc = '".$q."'";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
  {
  $calories = $oneGram*$row['Gm_Wgt'];  
  echo"
  <div  class='nutritiondata'>
<table cellpadding='0' cellspacing='0' width='250px'>

<tr><td>
<div class='nutritionheader'>Nutrition Facts</div>
</td></tr>
<tr><td>
<div class='nutritionlinethicker'>
<div class='nutrient'><font color='#000000'>Serving Size: </font><div class='nutrientvalue serving_current'>1 $row[Msre_Desc] </div>
<span style='display:none' class='serving_grams'>243</span>
</div></div>
</td></tr>

<tr><td>
<div class='nutritionline'>

<div class='nutrient'>Calories: <div  id='Calories' class='nutrientvalue'>$calories</div> 
</div></div>
</td></tr><tr><td>
<div class='nutritionlinethick'>
<div class='nutrient'>Total Fat: <div  id='Total_Fat' class='nutrientvalue'>24.154 g</div> 
</div></div>

</td></tr></table>
</div>
  ";
  }

?> 
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.