Hello

I am developing a website using ajax, php,javascript and html for a grocery. Product code is entered in a textbox 'txt_code' and upon clicking on 'Retrieve' button, the product details, quantiy and price is dispalyed(this is an ajax code). The product price, product code and product descripion are retrieved from the database using the 'txt_code'.

The problem is that i can change the product quantity but once i retrieve another product, the value of all the 'txt_quantity' become null.

Can any one please help?

Thank you.

Please find below the code that is used to retrieve the data from the database and is used in the ajax part.

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 else{
mysql_select_db("rubric grocery", $con);
$code=$_POST['txt_code'];

$rs=mysql_query("SELECT * FROM stock WHERE ProductCode ='".$code."'");
if(mysql_num_rows($rs)< 1) { 

echo "p";
 
}
else{

	$s=mysql_fetch_array($rs);
	$des = $s['ProductDescription'];
	$price = $s['ProductPrice'];
	$code1 = $s['ProductCode'];
	$i;
	 echo "<tr><td width='209'><input type='text' size='1' maxlength='2' name='txt_quantity' id='txt_quantity ".$i."'></td>
	  <td width='203'><input type='text' maxlength='8' name='txt_code1' id='txt_code1' disabled='true' value='".$code1."'></td>
	  <td width='475'><input type='text' size='65' disabled='true' value ='".$des."'></td>
	  <td width='172'><input type='text' disabled='true' id='txt_price' value ='".$price." '></td></tr>
	  ";
	
mysql_close($con);
}
}
?>

The ajax part that i used to display the product details.

function opening(){

	var once = new XMLHttpRequest();
	var code = document.forms[0].txt_code.value;
	var params = "txt_code="+code;
	
	var url = "product_ajax.php";
	
	once.open('POST', url, true);
	once.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	once.send(params);
	once.onreadystatechange = function()
	
		{					
			if (once.readyState==4 && once.status==200 ){
				var x=once.responseText;
											
					if (x.trim() == "p"){
						alert("Product is non existant");	
						}
					else{
					
					document.getElementById('nxtProduct').style.visibility="visible";
						
					
					var y=document.getElementById('t').innerHTML;					
					document.getElementById('t').innerHTML = y + x; 
					document.getElementById('nxtProduct').scrollTop = 10000; 
				//i have done this part because i thought that maybe the quantity is overwrite due to the fact that the 'txt_quantiy' is same for all.	
				var i=document.getElementById('t'). rows.length;
				var div_node = document.getElementById('txt_quantity');
				div_node.id = 'txt_quantity' + i;
			       var m=document.getElementById(div_node.id).value;
			     
			 					
					}// end else						
			}//end if	
	    }//end function
	
}//end OPENING function

You are not setting the variable $i in PHP code line 23, but you are using it on line 24

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.