How to read value from textfield in loop to process with AJAX

I open my web page and It not have alert from my web,but when I move <form> to outside last } behide mysql_close($link); and open my web ,it show alert but not value n1 and alert again but not have any value because form was outside loop.

I want to show table with textfield for edit quantity value in table.when I edit I want to change value in <td width=“14%” bgcolor=“#FFFFCC”><div align=“center”>total</div></td> .In hidden field I keep value for sum with edit value, It think it better than query again.but now I can’t read data from form in loop. :(:sick::confused:

<script>
var req;
function getsum()
{
	if(window.XMLHttpRequest)
	  req=new XMLHttpRequest();
	else if(window.ActiveXObject)
	  req=new ActiveXObject("Microsoft.XMLHTTP");
	else{
		alert("Browser not support");
		return false;
	}
	req.onreadystatechange = statechange;
	
	var n1=document.myform.num1.value;                           
	var n2=document.myform.num2.value;
	
	alert(n1);                     <--------------- Here not show alert or any value
	alert(n2);                     <--------------- Here not show alert or any value
	
	var str=Math.random();
	var querystr="sum.php?pop="+str;
	
//	req.open("GET",querystr,true);
//	req.Send(null);
}

function statechange()
{
	if(req.readyState==4){
		var x=document.getElementById("cell1");
		x.innerHTML=req.responseText;
	}
	else{
		var x=document.getElementByID("cell1");
		x.innerHTML="Please wait...";
	}
}	
</script>
</head>


<body>

<?
$link=mysql_connect("localhost","root","");
if(!$link)
	echo"ERROR MYSQL";
else
{
mysql_select_db("product",$link);

?>

<table width="100%" border="1" cellspacing="0">
  <tr>
    <td width="19%" bgcolor="#FFFFCC"><div align="center">count</div></td>
    <td width="25%" bgcolor="#FFFFCC"><div align="center">name</div></td>
    <td width="28%" bgcolor="#FFFFCC"><div align="center">price</div></td>
    <td width="14%" bgcolor="#FFFFCC"><div align="center">quantity</div></td>
    <td width="14%" bgcolor="#FFFFCC"><div align="center">total</div></td>
  </tr>

<?
$count="";
 for($i=0;$i<$numproduct;$i++)      //loop for show value cookie
{	
	
	$q="select*from product where pid='$cart[$i]'";     // $cart[$i] is id in cookie
	$result=mysql_query($q,$link);

	while($row=mysql_fetch_row($result))
	{
		$count++;
?>


 <tr>

    <td><div align="center"><?=$count?></div></td>
    <td><div align="right"><?=$cart[$i]?></div></td>
    <td><div align="right"><?=$row[4]?></div></td>
   
 <td><form name="myform">
        <input  type="text"name="num1" value="<?=$cartpqty[$i]?>" />         <--------------------- Data I want read for proocess 
        <input type="hidden" name="num2" value="<?=$row[4]?>" />             <----------------------Data I want read for process
	<input type="button" value="sum" name="sum" onclick="getsum()" />
    </form></td>

    <td><div id="answer" align="right"><?=$cartpqty[$i]*$row[4]?></div></td>
  </tr> 
 <?  } ?>
 <?	}
	mysql_close($link);
}
?>