hi all again,
i have made this one script that grabs values from the DB and displays them once you click on the dropdown menu which is fine if you have the first value that is printed. but i also made a new part of it where it prints a new value where you can choose another product. but on this one when you choose a product it changes the value on the 1st one.
i have tried to send through a value to make it print in its own section with no results.
an example is here: Clicky
here is the working code: (warning long)
thanks in advanced
<?php
require_once("functions.php");
connect_to_db();
$buyer = 1;
?>
<?
function produce(){
$result = mysql_query("SELECT * FROM produce WHERE ready='1' ORDER BY id DESC");
if( mysql_num_rows($result) == 0 ) {
echo '<option><strong>No Produce displayed at this time.</strong></option>';
}
else {
while($myrow = mysql_fetch_array($result))
{
echo "<option value='".$myrow[product]."' onClick='showPrice(this.value);'>";
echo $myrow[product];
echo '</option>';
}
}
}
?>
<!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>ajax test</title></head>
<script type="text/javascript" src="media/jquery.js"></script>
<script type="text/javascript">
var xmlhttp;
function showPrice(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var buyer="<?=$buyer?>";
var url="ajax.php";
url=url+"?product="+str;
url=url+"&buyer="+buyer;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function blah() {
var id = document.getElementById("id").value;
//Creates the name tag
$("#newitem").append("<tr><td><fieldset><legend><select name='produce'><option>--Select--</option><?=produce();?></select></legend><table width='200'><tr><td>Ammount</td><td>On Hand</td><td>Cost</td></tr><tr id='txtHint"+id+"'></tr>");
id = (id - 1) + 2;
document.getElementById("id").value = id;
}
</script>
<body>
<form action="produce1.php" method="post" name="myForm">
<input type="hidden" id="id" value="2" />
<table cellpadding="5" cellspacing="5">
<tbody id="newitem"><tr><td><strong>Product</strong></td><td><strong>Amount</strong></td>
<td><strong>Cost</strong></td>
</tr>
<tr><td>
<fieldset>
<legend>
<select name="produce">
<option>--Select--</option>
<?=produce();?>
</select></legend>
<table width="200">
<tr>
<td>Ammount</td>
<td>On Hand</td>
<td>Cost</td>
</tr>
<tr id="txtHint">
</tr>
</table>
</fieldset>
</td>
</tr>
</tbody></table>
</form><input name="name" type="button" onClick="blah(); return false;" value="more produce" />
</body>
</html>