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>

Recommended Answers

All 10 Replies

In the line

document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

you are changing the inner html of the table row with id=txtHint

When you are adding a new row by clicking on 'more produce' button you are adding a new row again with id=txtHint.

Hence response changes the value of first row always.

You will require to have different ids for different rows and should pass the row number as the parameter to the js funtions as well as php.

For example you can create rows with ids like txtHint0, txtHint1 ..... and pass 0, 1 as parameter to js function and so on

thats true but even when i did not give it a unique name it still would one display the data on the first product still. that is the reason i gave it a unique name but i dont know how to grab the new name to display it back in it

Add a new parameter to the function showPrice() to indicate row number. Make corresponding changes in the php code where options are generated to have the rowid parameter.
Also change xmlhttp.onreadystatechange so that rowid is available in the handler function
Make change in the blah() function so that corresponding row ids are maintained when new produce is added

The changed code snippet is as follows,

<?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, 1);'>";
    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">
String.prototype.replaceAll = function(pcFrom, pcTo){
	var i = this.indexOf(pcFrom);
	var c = this;
 
	while (i > -1){
		c = c.replace(pcFrom, pcTo); 
		i = c.indexOf(pcFrom);
	}
	return c;
}

var xmlhttp;
 
function showPrice(str, rowid)
{
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=function(){
	if (xmlhttp.readyState==4)
	{
		document.getElementById("txtHint"+rowid).innerHTML=xmlhttp.responseText;
	}
}
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
 
 
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;
	var optionStr = "<?=produce();?>";
	optionStr = optionStr.replaceAll("showPrice(this.value, 1)", "showPrice(this.value, "+id+")");
	//Creates the name tag	
	$("#newitem").append("<tr><td><fieldset><legend><select name='produce'><option>--Select--</option>"+optionStr+"</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="txtHint1">
  </tr>
</table>
    </fieldset>
    </td>
  </tr>
</tbody></table>
</form><input name="name" type="button" onClick="blah();  return false;" value="more produce" />
</body>
</html>

I cant test it for correctness, though I have tried with some data and hope this helps you

awesome thanks for the help.

so if i am going to send more values through i use a comma like this (this.value, 1, something) but make sure that i call it within the ajax function like this (str, rowid, whatisit). and if i want to call that part of the value i can +whatisit. i am trying to understand how it is working. you know trying to learn instead of just getting answers.

i dont understand what this is doing here

String.prototype.replaceAll = function(pcFrom, pcTo){
	var i = this.indexOf(pcFrom);
	var c = this;
 
	while (i > -1){
		c = c.replace(pcFrom, pcTo); 
		i = c.indexOf(pcFrom);
	}
	return c;
}

also idk if this should be in a new thread but would there be a reason why this is not working in chrome. Well the ajax part that is. i thought since i had the return new XMLHttpRequest(); in there it should work

thanks

Yes you are right about sending more values by separating them with commas.
Also the php function produce() creates options have the onclick event as onClick='showPrice(this.value, 1); . So in the blah() js function we have to replace 1 with the actual row number so that the for second row it will be like onClick='showPrice(this.value, 2); and so on. Since javascript do not have string function that replaces all occurrences of a string a new function replaceAll is added by
String.prototype.replaceAll = function(pcFrom, pcTo){
....
}
This is way of enhancing existing object with more methods in javascript

awesome. is it really necessary to replace what was printed out in the php before it was then sent through the jquery. it seemed to be working without that new string that was put in place. not bashing your coding or help but just trying to understand.

also would you happen to know why it doesnt work in chrome (the ajax part)? is my ajax not properly written?

thanks

Yes it is necessary to replace the text what was printed out in the php before it was then sent through the jquery.
What is printed out in php will make the blah() function something like shown below in the generated HTML,

function blah() {
var id = document.getElementById("id").value;
	//Creates the name tag	
	var optionStr = "<option value='1' onClick='showPrice(this.value, 1);'>test1</option><option value='2' onClick='showPrice(this.value, 1);'>test2</option><option value='3' onClick='showPrice(this.value, 1);'>test3</option>";
	optionStr = optionStr.replaceAll("showPrice(this.value, 1)", "showPrice(this.value, "+id+")");
	$("#newitem").append("<tr><td><fieldset><legend><select name='produce'><option>--Select--</option>"+optionStr+"</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;
}

So when blah() is called on click of 'more produce' button we need to replace the value 1 from showPrice(this.value, 1) to 2, 3, 4... otherwise each select box will change the data in first row only. Hence the replace funtion is used.

Also it is not working in chrome because onclick event not works for <option> tag in Chrome. There is no issue with the Ajax part. Instead of using
<option onclick="showPrice(this.value, 1);">
use
<select onchange="showPrice(this.value, 1);">
You will also have to make corresponding changes in the blah() function

sry for the late reply but i never got a notification of a new reply.

so i should use the onchange value on the select instead of the onclick on the option

and then shouldn't the blah() function be

function blah() {
var id = document.getElementById("id").value;
	var optionStr = "<?=produce();?>";
	var change =  "showPrice(this.value, "+id+")");
	//Creates the name tag	
	$("#newitem").append("<tr><td><fieldset><legend><select name='produce' onchange='"+change+"'><option>--Select--</option>"+optionStr+"</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;
}

then just take away the onclick within the php function.

Yeah you are right. Actually onclick event on <option> is not supported in Chrome and IE so it is always better to use onchange event on the <select> tag.
Just have to take care with handling the <option>--Select--</option>
in the js function showPrice(str, rowid). Because now selecting this option will also call the showPrice funtion

wow i really learned alot. thanks for all the help.

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.