enter the the material required to make this product
1.first page test.php
which takes product code and displays prodcut anme have used
ajax to avoid refreshing of page this works fine
2.now i have created one row with
checkbox|select box|text|text|t ext|text|
where in the select box values are fetched from table here also i have used ajax for getting the m_name automatically withput refreshi works fine (onchange event)
3.after that i have given a add rows button on which rows are added dynamically
with the same like static row i have added
now i wantto use the same function i have used for static select box for avoiding refreshing here how do i pass the onchange event here
and after pressing submit button the dynamic rows created disappears
can anybody help me wot is wrong with this
how do i pass dynamic elements values from the rows to the php along with the static row values
my code is as follows
Expand|Select|Wrap|Line Numbers
- test.php
- <?
- include("../include/connection.php");
- if($_GET)
- {
- $p_cde=$_GET['p_cde'];
- $cnt = count($_GET['m_cde'])+1;
- for ($idx=0;$idx<=$cnt;$idx++) {
- $m_cde= $_GET['m_cde'][$idx] ;
- $m_name=$_GET['m_name'][$idx];
- $um=$_GET['um'][$idx];
- $qty=$_GET['qty'][$idx];
- $overages=$_GET['overages'][$idx];
- }
- }
- ?>
- <html>
- <head>
- <script src="test.js" ></script>
- </head>
- <body><form name="material" method="GET">
- <table id="tbl" border="1">
- <tr><td>
- <select name="p_cde" id="p_cde" onChange="showCode(this.value);">
- <?
- $sqlpr = "select * from fgmaster order by p_cde ";
- $db_query=$DB_site->query($sqlpr);
- while($rspr=$DB_site->fetch_array($db_query)){
- $combopr=$combopr."<option value='".$rspr['p_cde']."'";
- if ($rspr['p_cde']==strtoupper($p_cde)){
- $combopr=$combopr."selected";
- }
- $combopr=$combopr.">".$rspr['p_cde']."-".$rspr['p_name']."</option>\n";
- }
- echo $combopr;
- ?>
- </select>
- </td>
- <td><input type="text" name="p_name" id="p_name" value=""></td>
- <td><input type="text" name="p_pckunit" id="p_pckunit" value=""></td></tr>
- <?
- $sqlmaterial="select * from materialmaster order by m_cde";
- $db_query_material=$DB_site->query($sqlmaterial);
- while($rsmaterial=$DB_site->fetch_array($db_query_material)){
- $option_vals[]=$rsmaterial['m_cde']."-".$rsmaterial['m_name'];
- }
- ?>
- <script>
- select_opt = new Array();
- <?
- for($i=0;$i<count($option_vals);$i++){
- ?>
- select_opt[<?= $i ?>] = '<?= $option_vals[$i] ?>';
- <?
- }
- ?>
- </SCRIPT>
- <tr bgcolor="#CCCCCC" bordercolor="#FFCCFF">
- <th> </th>
- <th >Code</th>
- <th>Material description</th>
- <th>um</th>
- <th>qty</th>
- <th>overages</th>
- </tr>
- <tr>
- <td><input type="checkbox" name="chk" id="chk"></td>
- <td><select name='m_cde' id='m_cde' onChange='showmcode(this.value);'>
- <?
- $sqlmr = "select * from materialmaster order by m_cde ";
- $db_query_mr=$DB_site->query($sqlmr);
- while($rsmr=$DB_site->fetch_array($db_query_mr)){
- $combomr=$combomr."<option value='".$rsmr['m_cde']."'";
- if ($rsmr['m_cde']==strtoupper($m_cde)){
- $combomr=$combomr."selected";
- }
- $combomr=$combomr.">".$rsmr['m_cde']."-".$rsmr['m_name']."</option>\n";
- }
- echo $combomr;
- ?>
- </select></td>
- </td><td><input type="text" name='m_name' id='m_name' value=""></td>
- <td><input type="text" name='um' id='um' value=""></td>
- <td><input type="text" name='qty' id='qty' value=""></td>
- <td><input type="text" name='overages' id='overages' value=""></td>
- </tr>
- <tbody>
- </tbody>
- </table>
- <DIV>
- <input type ="button" value="Add Row" onclick="add_rows(document.getElementById('tbl'));">
- </div>
- <DIV><input type ="submit" value="Submit" name="myfields"></DIV>
- </form>
- </body>
- </html>
Expand|Select|Wrap|Line Numbers
- test.js
- ------------
- // JavaScript Document
- var xmlHttp;
- function showCode(str)
- {
- xmlHttp=GetXmlHttpObject(); //Calls on the GetXmlHttpObject function to create an XMLHTTP object
- if (xmlHttp==null)
- {
- alert ("Browser does not support HTTP Request");
- return;
- }
- var url="testget.php"; //Defines the url (filename) to send to the server
- url=url+"?p_cde="+str; //Adds a parameter (q) to the url with the content of the dropdown box
- url=url+"&sid="+Math.random(); //Adds a random number to prevent the server from using a cached file
- xmlHttp.onreadystatechange=stateChanged ; //Call stateChanged when a change is triggered
- xmlHttp.open("GET",url,true); //Opens the XMLHTTP object with the given url.
- xmlHttp.send(null); //Sends an HTTP request to the server
- alert(url);
- }
- function showmcode(str)
- {
- xmlHttp=GetXmlHttpObject(); //Calls on the GetXmlHttpObject function to create an XMLHTTP object
- alert(str);
- if (xmlHttp==null)
- {
- alert ("Browser does not support HTTP Request");
- return;
- }
- var url="testget.php"; //Defines the url (filename) to send to the server
- url=url+"?m_cde="+str; //Adds a parameter (q) to the url with the content of the dropdown box
- url=url+"&sid="+Math.random(); //Adds a random number to prevent the server from using a cached file
- xmlHttp.onreadystatechange=stateChanged1 ; //Call stateChanged when a change is triggered
- xmlHttp.open("GET",url,true); //Opens the XMLHTTP object with the given url.
- xmlHttp.send(null); //Sends an HTTP request to the server
- alert(url);
- }
- function stateChanged()
- {
- if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
- {
- var ar=xmlHttp.responseText.split("|");
- document.getElementById("p_name").value=ar[0] ;
- document.getElementById("p_pckunit").value=ar[1] ;
- }
- }
- function stateChanged1()
- {
- if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
- {
- var ar=xmlHttp.responseText.split("|");
- document.getElementById("m_name").value=ar[0] ;
- document.getElementById("um").value=ar[1] ;
- }
- }
- function GetXmlHttpObject()
- {
- var xmlHttp=null;
- try
- {
- // Firefox, Opera 8.0+, Safari
- xmlHttp=new XMLHttpRequest();
- }
- catch (e)
- {
- //Internet Explorer
- try
- {
- xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
- }
- catch (e)
- {
- xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- }
- return xmlHttp;
- }
- //FUNCTION FOR DYNAMIC ROW
- j=0;
- function add_rows(obj){
- //tabObj = document.createElement("table");
- //tbodyObj = document.createElement("tbody");
- tabCols = new init_table();
- trObj = document.createElement("tr")
- col_names = new Array("chk","m_cde","m_name","um","qty","overages");
- for(i=0;i<tabCols.length;i++){
- tdObj = document.createElement("td");
- /*
- if(create_object(tabCols[i])=="select")
- {
- mselect=create_object(tabCols[i],col_names[i]+'['+j+']')
- mselect.onChange=showmcode(col_names[i]+'['+j+']');
- tdObj.appendChild(mselect));
- }*/
- tdObj.appendChild(create_object(tabCols[i],col_names[i]+'['+j+']'));
- //added on 04/09/2007 for onchage event on select box
- /* if(tabCols[i]=="select"){
- alert("hi");
- col_names[i]+'['+j+']'="fieldevent('"+fieldname+"')";
- var showmcode=new Function(col_names[i]+'['+j+']'.onchange);
- if(col_names[i]+'['+j+']'.addEventListener){
- col_names[i]+'['+j+']'.addEventListener('change',showmcode,false);
- else if(col_names[i]+'['+j+']'.attachEvent){
- col_names[i]+'['+j+']'.attachEvent('onchange',showmcode);}
- }
- /* this is a sample
- sfield.onchange = "fieldevent('" + fieldname + "')";
- var onChangeHandler = new Function(sfield.onchange);
- if (sfield.addEventListener) {
- sfield.addEventListener('change', onChangeHandler, false );
- } else if (sfield.attachEvent) {
- sfield.attachEvent('onchange', onChangeHandler);
- }
- */
- trObj.appendChild(tdObj);
- }
- obj.firstChild.appendChild(trObj);
- j++;
- }
- function fieldevent(fname){
- alert (fname);
- }
- function init_table(){
- tableProp = new Array();
- tableProp[0] = "checkbox";
- tableProp[1] = "select"; //m_cde
- tableProp[2] = "text"; //m_name
- tableProp[3] = "text"; //um
- tableProp[4] = "text"; //qty
- tableProp[5] = "text"; //overages
- //tableProp[6] = "checkbox";
- return tableProp;
- }
- function create_object(objType,nm){
- if(objType == 'select'){
- return getSelectBoxDOM(select_opt,nm);
- }
- obj = document.createElement("input");
- obj.setAttribute("type",objType);
- obj.setAttribute("name",nm);
- return obj;
- }
- function getSelectBoxDOM(options,nm) {
- selectBox =document.createElement('select');
- for (x=0; x < options.length; x++) {
- optionItem =document.createElement('option');
- optionItem.appendChild(document.createTextNode(options[x]));
- selectBox.appendChild(optionItem);
- }
- selectBox.setAttribute("name",nm);
- return selectBox;
- }
Expand|Select|Wrap|Line Numbers
- testget.php
- ----------------
- <?php
- if($_GET)
- {
- $p_cde=$_GET['p_cde'];
- $con = mysql_connect('localhost', 'localhost', ''); //PHP opens a connection to a MySQL server
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- mysql_select_db("erpmfg", $con);
- $sql="select * from fgmaster where p_cde = '".$p_cde."'";
- $result = mysql_query($sql);
- while($row = mysql_fetch_array($result)){
- $p_name=$row['p_name'];
- $p_pckunit=$row['p_pckunit'];
- echo $p_name."|".$p_pckunit;
- }
- }
- if ($_GET)
- {
- $m_cde=$_GET['m_cde'];
- $sqlmr="select * from materialmaster where m_cde = '".$m_cde."'";
- $result1 = mysql_query($sqlmr);
- while($row = mysql_fetch_array($result1)){
- $m_name=$row['m_name'];
- $um=$row['um'];
- echo $m_name."|".$um;
- }
- }
- mysql_close($con);
- ?>