Connecting Tech Pros Worldwide Forums | Help | Site Map

Tab key to create rows

Member
 
Join Date: Oct 2007
Posts: 103
#1: Dec 4 '07
Is it possible to create row on client side when tab key is pressed in the last cell of the first row of the table

table is something like this

cell1| cell2| cell3 |cell4| cell5|
____|____|_____|____|____ |_


in the first cell select box is given second select box and remaining columns text boxes

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#2: Dec 4 '07

re: Tab key to create rows


hi ...

it should be possible ... please post how you have your first line created, is it a table-row with textboxes?

kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#3: Dec 7 '07

re: Tab key to create rows


[HTML]<table id="tbl">
<tr><thead><th>continent</th><th>country</th> <th>longitude</th>
<th>latitude</th>
<th>time zone</th>
</thead> </tr>
<td><select name="mcontinent" id="mcontinent" tabindex="11">
<option selected>--- Selectcontinenet ---</option>
//shown from continenet master
</select></td>

<tr> <td><select name="mcountrycode" id="mcountrycode" tabindex="1" >
<option selected>--- Selectcountry ---</option>
//option selected from country master table
</select></td>
<td><input type="text" name="mlong" id="mlong"></td>
<td><input type="text" name="mlat" id="mlat" ></td>
<td><input type="text" name="mtime" id="mtime" ></td>
</tr>
</table>[/HTML]
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#4: Dec 8 '07

re: Tab key to create rows


hi ...

have a look at the following example:

[HTML]<script type="text/javascript">
function add_row() {
var table = document.getElementById('tbl');
var row = table.getElementsByTagName('tr')[0];
var new_row = row.cloneNode(true);

table.appendChild(new_row);
}

function add_row_event(e) {
var val = typeof window.event != 'undefined' ? window.event.keyCode : e.keyCode;

if (val == 9) {
add_row();
}
}
</script>

<table id="tbl">
<thead>
<th>continent</th>
<th>country</th>
<th>longitude</th>
<th>latitude</th>
<th>time zone</th>
</thead>
<tr>
<td>
<select name="mcontinent" id="mcontinent" tabindex="11">
<option selected>--- Selectcontinenet ---</option>
</select>
</td>
<td>
<select name="mcountrycode" id="mcountrycode" tabindex="1" >
<option selected>--- Selectcountry ---</option>
</select>
</td>
<td>
<input type="text" name="mlong" id="mlong"></td>
<td>
<input type="text" name="mlat" id="mlat" ></td>
<td>
<input type="text" name="mtime" id="mtime" onkeypress="add_row_event(event);">
</td>
</tr>
</table>
[/HTML]
kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#5: Dec 10 '07

re: Tab key to create rows


Thanks
this works
prefectly in mozilla bt not in IE 6 it doesnt append any row
and how do i name the rows coulmns created in the array
something like
<input type="text" name="mlong[i]" id="mlong[i]"></td>
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#6: Dec 10 '07

re: Tab key to create rows


first ... small adaptation to make it work with IE, it seems that that silly IE don't fire onkeypress with tab-key ... so we use the keydown-event. next we need to use to append to the tbody and we have to use tr with idx 1 instead. here is the script:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function add_row(idx) {
  3.     var table   = document.getElementById('tbl');
  4.     var row     = table.getElementsByTagName('tr')[idx];
  5.     var new_row = row.cloneNode(true);
  6.  
  7.     table.getElementsByTagName('tbody')[0].appendChild(new_row);
  8. }
  9.  
  10. function add_row_event(e) {
  11.     var val;
  12.  
  13.     if (typeof window.event != 'undefined') {
  14.         val = window.event.keyCode;
  15.         idx = 1;
  16.     } else {
  17.         val = e.keyCode;
  18.         idx = 0;
  19.     }
  20.  
  21.     if (val == 9) {
  22.         add_row(idx);
  23.     }
  24. }
  25. </script>
  26.  
  27. <table id="tbl">
  28.     <thead>
  29.         <th>continent</th>
  30.         <th>country</th>
  31.         <th>longitude</th>
  32.         <th>latitude</th>
  33.         <th>time zone</th>
  34.     </thead>
  35.     <tr>
  36.         <td>
  37.             <select name="mcontinent" id="mcontinent"  tabindex="11">
  38.                 <option selected>--- Selectcontinenet ---</option>
  39.             </select>
  40.         </td>
  41.         <td>
  42.             <select name="mcountrycode" id="mcountrycode" tabindex="1" >
  43.                 <option selected>--- Selectcountry ---</option>
  44.             </select>
  45.         </td>
  46.         <td>
  47.             <input type="text" name="mlong" id="mlong"></td>
  48.         <td>
  49.             <input type="text" name="mlat" id="mlat" ></td>
  50.         <td>
  51.             <input type="text" name="mtime" id="mtime" onkeydown="add_row_event(event);">
  52.         </td>
  53.     </tr>
  54. </table>
  55.  
kind regards
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#7: Dec 10 '07

re: Tab key to create rows


Quote:

Originally Posted by SSG001

...
and how do i name the rows coulmns created in the array
something like

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="mlong[i]" id="mlong[i]">
  2.  

therefore you have to loop through the cloned row's child elements and assign your desired id and name to the input before appending it to the table ...

kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#8: Dec 11 '07

re: Tab key to create rows


is it somwething like this
Expand|Select|Wrap|Line Numbers
  1.      for (i=0;i<=5;i++){
  2.              tdObj = document.createElement("td")[i];
  3.  
  4.     }
  5.  
but how i append it to the row
Member
 
Join Date: Oct 2007
Posts: 103
#9: Dec 11 '07

re: Tab key to create rows


Expand|Select|Wrap|Line Numbers
  1.     var cells = new_row.childNodes;
  2.     for (var i=0;i<cells.length;i++) {
  3.     var cellName = cells[i].name;
  4.     var cellid=cells[i].id;
  5.         if (cellName){
  6.             cells[i].name =cellName + "["+i+"]";
  7.             cells[i].id=cellName+"["+i+"]";
  8. alert(cells[i].name);
  9.         }
  10.  
  11.     }

//but it not alerting anything to see if the naming is proper
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#10: Dec 11 '07

re: Tab key to create rows


hmmm ... do you want something like this?

[HTML]<script type="text/javascript">
function add_row(idx) {
var table = document.getElementById('tbl');
var row = table.getElementsByTagName('tr')[idx];
var new_row = row.cloneNode(true);

apply_names(table, new_row);

table.getElementsByTagName('tbody')[0].appendChild(new_row);
}

function apply_names(table, row) {
var inp = row.getElementsByTagName('input');
var sel = row.getElementsByTagName('select');
var sfx = table.getElementsByTagName('tr').length;
var eles = [];
var ele;

for (var i = 0; i < inp.length; i++) {
eles.push(inp[i]);
}

for (i = 0; i < sel.length; i++) {
eles.push(sel[i]);
}

for (i = 0; i < eles.length; i++) {
ele = eles[i];
ele.setAttribute('name', ele.getAttribute('name') + '_' + sfx);
ele.setAttribute('id', ele.getAttribute('id') + '_' + sfx);
}

alert(row.innerHTML);
}

function add_row_event(e) {
var val;

if (typeof window.event != 'undefined') {
val = window.event.keyCode;
idx = 1;
} else {
val = e.keyCode;
idx = 0;
}

if (val == 9) {
add_row(idx);
}
}
</script>

<table id="tbl">
<thead>
<th>continent</th>
<th>country</th>
<th>longitude</th>
<th>latitude</th>
<th>time zone</th>
</thead>
<tr>
<td>
<select name="mcontinent" id="mcontinent" tabindex="11">
<option selected>--- Selectcontinenet ---</option>
</select>
</td>
<td>
<select name="mcountrycode" id="mcountrycode" tabindex="1" >
<option selected>--- Selectcountry ---</option>
</select>
</td>
<td>
<input type="text" name="mlong" id="mlong"></td>
<td>
<input type="text" name="mlat" id="mlat" ></td>
<td>
<input type="text" name="mtime" id="mtime" onkeydown="add_row_event(event);">
</td>
</tr>
</table>
[/HTML]
kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#11: Dec 11 '07

re: Tab key to create rows


looks like it's same


Thanks a lot
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#12: Dec 11 '07

re: Tab key to create rows


hi ...

the same as what? :) ... its different because your code tried to set the name/id of table-cells ... the above one sets it for the input-elements ...

kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#13: Dec 11 '07

re: Tab key to create rows


Hi
mean to say this is what i wanted

thanks
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#14: Dec 11 '07

re: Tab key to create rows


ahhh ... i see ... ;) ... do you want to post the form later on to a serverside script?
Newbie
 
Join Date: Mar 2007
Posts: 16
#15: Dec 11 '07

re: Tab key to create rows


Quote:

Originally Posted by gits

hi ...

I am new to Javascript. Could you please help me how to create a new row by clicking the image or any text link. I tried the above code but the add row message comes after every row. This is my same example code

Expand|Select|Wrap|Line Numbers
  1.       <script type="text/javascript">
  2.       function add_row(idx) {
  3.           var table   = document.getElementById('tbl');
  4.           var row     = table.getElementsByTagName('tr')[idx];
  5.           var new_row = row.cloneNode(true);
  6.           table.getElementsByTagName('tbody')[0].appendChild(new_row);
  7.       }
  8.  
  9.       function add_row_event(e) {
  10.       var val;
  11.       if (typeof window.event != 'undefined') {
  12.             val = window.event.keyCode;
  13.             idx = 1;
  14.           } else {
  15.               val = e.keyCode;
  16.               idx = 0;
  17.           }
  18.           if (val == 0) {
  19.               add_row(idx);
  20.           }
  21.       }
  22.       </script>
  23.       <table id="tbl">
  24.           <thead>
  25.               <th>longitude</th>
  26.               <th>latitude</th>
  27.               <th>time zone</th>
  28.          </thead>
  29.           <tr>
  30.               <td>
  31.                   <input type="text" name="mlong" id="mlong"></td>
  32.               <td>
  33.                     <input type="text" name="mlat" id="mlat" ></td>
  34.               <td>    
  35.                  <input type="text" name="mtime" id="mtime">
  36.               </td>
  37.               <td>&nbsp;&nbsp;<a href="#" onClick="add_row_event(event);">Add Row</a> </td>
  38.           </tr>
  39.       </table>
Member
 
Join Date: Oct 2007
Posts: 103
#16: Dec 11 '07

re: Tab key to create rows


yes right
have to actually post the data using to server side using php
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#17: Dec 11 '07

re: Tab key to create rows


in that case we have to adapt the script ... because think i remember that IE makes problems when setting name attribs during runtime ... could you try to post the form and have a look whether it works for you at the moment or not?

kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#18: Dec 12 '07

re: Tab key to create rows


[PHP]//count value shows 1 although i created three rows
<?
if($_POST['submit']){
$ct=count($_POST['mcontinent']);
?> <script>alert('<?=$ct?>');</script> <?
}
?>
<html>
<head>
<script type="text/javascript">
function add_row(idx) {
var table = document.getElementById('tbl');
var row = table.getElementsByTagName('tr')[idx];
var new_row = row.cloneNode(true);

apply_names(table, new_row);

table.getElementsByTagName('tbody')[0].appendChild(new_row);
}

function apply_names(table, row) {
var inp = row.getElementsByTagName('input');
var sel = row.getElementsByTagName('select');
var sfx = table.getElementsByTagName('tr').length;
var eles = [];
var ele;

for (var i = 0; i < inp.length; i++) {
eles.push(inp[i]);
}

for (i = 0; i < sel.length; i++) {
eles.push(sel[i]);
}

for (i = 0; i < eles.length; i++) {
ele = eles[i];
ele.setAttribute('name', ele.getAttribute('name') + '_' + sfx);
ele.setAttribute('id', ele.getAttribute('id') + '_' + sfx);
}

alert(row.innerHTML);
}

function add_row_event(e) {
var val;

if (typeof window.event != 'undefined') {
val = window.event.keyCode;
idx = 1;
} else {
val = e.keyCode;
idx = 0;
}

if (val == 9) {
add_row(idx);
}
}
</script>
</head>
<body>
<form method="post">
<table id="tbl">
<thead>
<th>continent</th>
<th>country</th>
<th>longitude</th>
<th>latitude</th>
<th>time zone</th>
</thead>
<tr>
<td>
<select name="mcontinent" id="mcontinent" tabindex="11">
<option selected>--- Selectcontinenet ---</option>
</select>
</td>
<td>
<select name="mcountrycode" id="mcountrycode" tabindex="1" >
<option selected>--- Selectcountry ---</option>
</select>
</td>
<td>
<input type="text" name="mlong" id="mlong"></td>
<td>
<input type="text" name="mlat" id="mlat" ></td>
<td>
<input type="text" name="mtime" id="mtime" onkeydown="add_row_event(event);">
</td>
</tr>
</table>
<input type="submit" name="submit" value"submit">
</form>
</body>
</html>[/PHP]
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#19: Dec 12 '07

re: Tab key to create rows


:) sorry in case i didn't make myself very clear ... we create new rows in the table at the client ... that table contains input-nodes where we set the name dynamically ... after the user filled in some values ... the question is now: should that form be posted back to the server without using ajax? in case it should be simply posted back then we have to consider the following IE-'bug' :) here that means that IE doesn't allow to set the name-attribute at runtime so when posting the form you might miss the variable-names of the posted form-parameters and we may use something like this to work around that problem

kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#20: Dec 12 '07

re: Tab key to create rows


As u see there are two select boxes in the row
so evrytime i'll have to refresh page
so i have read somewhere it can be avoided with ajax
also if we do the posting of this data through ajax then how to go about



thanks
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#21: Dec 12 '07

re: Tab key to create rows


in that case you may create the querystring for the ajax-request for youself out of the lines or use the node-ids ... so we wouldn't have the name-problem here :) ...

are you familiar with ajax and how it works?

kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#22: Dec 12 '07

re: Tab key to create rows


have read lot of theory but no practicals
can u just show me with only one field posting with ajax
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#23: Dec 12 '07

re: Tab key to create rows


hi ...

here you find an example

kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#24: Dec 13 '07

re: Tab key to create rows


Expand|Select|Wrap|Line Numbers
  1. <script>
  2. //set the variables.
  3. function getArticle(id) {
  4.     var cb = function(res) {
  5.         alert(res.responseText);
  6.     };
  7.  
  8.     ajax('getNews.php', id,cb);
  9. }
  10.  
  11. //Make the actual connection.
  12. ajax = function(strURL, sSend) {
  13.     var xmlHttpReq=false;
  14.  
  15.     if (!window.ActiveXObject) ajax.req = new XMLHttpRequest();
  16.     else ajax.req = new ActiveXObject("Microsoft.XMLHTTP");
  17.  
  18.     ajax.req.open('POST',strURL,false);
  19.     ajax.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  20.  
  21.     ajax.req.send(sSend);
  22.     return ajax.req;
  23. }
  24.  
  25.  
  26. //set the query string to be sent
  27. function getquerystring(id) {
  28.     qstr = 'data=' + escape(id);  // NOTE: no '?' before querystring
  29.     return qstr;
  30. }
  31.  
  32. //put the data on the page.
  33. function updatepage(str) {
  34.     document.getElementById("data").innerHTML = str;
  35. }
  36. </script>
  37. <select onchange="getArticle(this.value);">
  38. <option value="1">Train</option>
  39. <option value="2">Airplane Crashes</option>
  40. <option value="3">Mental Health</option>
  41. </select>
  42. <div id="data"></div>

[PHP]<?
news_id = request("id")
$strSQL = "select * from news where news_id = " & new_id
$rsdata=mysql_query($strSQL);
echo $rsdata['news_id'];
?>[/PHP]

//is this example will work with IE,mozilla
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#25: Dec 13 '07

re: Tab key to create rows


hi ...

that code should not work ... or is it? the problem is with the cb you don't have it assigned to the onreadystatechange of the XMLHttpRequest-object ...

kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#26: Dec 13 '07

re: Tab key to create rows


Expand|Select|Wrap|Line Numbers
  1. <script>
  2. //set the variables.
  3. function getArticle(id) {
  4.     var cb = function(res) {
  5.         alert(res.responseText);
  6.     };
  7.  
  8.     ajax('getNews.php', id,cb);
  9. }
  10.  
  11. //Make the actual connection.
  12. ajax = function(strURL, sSend) {
  13.     var xmlHttpReq=false;
  14.  
  15.     if (!window.ActiveXObject) ajax.req = new XMLHttpRequest();
  16.     else ajax.req = new ActiveXObject("Microsoft.XMLHTTP");
  17.  
  18.     ajax.req.open('POST',strURL,false);
  19.     ajax.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  20. ajax.req.xmlHttpReq.onreadystatechange = function(cb) {
  21.         if (ajax.req.xmlHttpReq.readyState == 4) {
  22.             cb(ajax.req.xmlHttpReq);
  23.         }
  24.  
  25.  
  26.     ajax.req.send(sSend);
  27.     return ajax.req;
  28. }
  29.  
  30.  
  31. //set the query string to be sent
  32. function getquerystring(id) {
  33.     qstr = 'data=' + escape(id);  // NOTE: no '?' before querystring
  34.     return qstr;
  35. }
  36.  
  37. //put the data on the page.
  38. function updatepage(str) {
  39.     document.getElementById("data").innerHTML = str;
  40. }
  41. </script>
  42. <select onchange="getArticle(this.value);">
  43. <option value="1">Train</option>
  44. <option value="2">Airplane Crashes</option>
  45. <option value="3">Mental Health</option>
  46. </select>
  47. <div id="data"></div>


Code: ( php )
Expand|Select|Wrap|Line Numbers
  1. <?
  2. news_id = request("id")
  3. $strSQL = "select * from news where news_id = " & new_id
  4. $rsdata=mysql_query($strSQL);
  5. echo $rsdata['news_id'];
  6. ?>

will this give me names of the rows properly for posting
Member
 
Join Date: Oct 2007
Posts: 103
#27: Dec 14 '07

re: Tab key to create rows


i have used ajax for posting data from tables created using add row button
but not with tab key.
and the creation of rows was also using createElement method and then appending
the naming is also done for each row and each cell
but this tab key method looks more sleek then clicking button for adding row
how do i post the data from the rows created to server using ajax
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#28: Dec 14 '07

re: Tab key to create rows


your ajax-request is working? ... i wonder because you should have to receive the cb in your ajax-function like this:

Expand|Select|Wrap|Line Numbers
  1. var ajax = function(strURL, sSend, cb) {
  2.     // your code here
  3.     // and later on you simply could use it:
  4.  
  5.     ajax.req.xmlHttpReq.onreadystatechange = cb;
  6.  
  7.     // your further code here :)
  8. }
you could call getArticle it from our add_row-function ... to have it called on tab-press ...

kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#29: Dec 14 '07

re: Tab key to create rows


Expand|Select|Wrap|Line Numbers
  1. <script>
  2. //set the variables.
  3. function getArticle(id) {
  4.     var cb = function(res) {
  5.         alert(res.responseText);
  6.     };
  7.  
  8.     ajax('getNews.php', id,cb);
  9. }
  10.  
  11. //Make the actual connection.
  12.  
  13.  
  14.  
  15. var ajax = function(strURL, sSend,cb) {
  16.     var xmlHttpReq=false;
  17.  
  18.     if (!window.ActiveXObject) ajax.req = new XMLHttpRequest();
  19.     else ajax.req = new ActiveXObject("Microsoft.XMLHTTP");
  20.  
  21.     ajax.req.open('POST',strURL,false);
  22.     ajax.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  23.     ajax.req.xmlHttpReq.onreadystatechange = cb;
  24.         if (ajax.req.xmlHttpReq.readyState == 4) {
  25.             cb(ajax.req.xmlHttpReq);
  26.         }
  27.  
  28.  
  29.     ajax.req.send(sSend);
  30.     return ajax.req;
  31. }
  32.  
  33.  
  34. //set the query string to be sent
  35. function getquerystring(id) {
  36.     qstr = 'data=' + escape(id);  // NOTE: no '?' before querystring
  37.     return qstr;
  38. }
  39.  
  40. //put the data on the page.
  41. function updatepage(str) {
  42.     document.getElementById("data").innerHTML = str;
  43. }
  44. </script>
  45. <select onchange="getArticle(this.value);">
  46. <option value="1">Train</option>
  47. <option value="2">Airplane Crashes</option>
  48. <option value="3">Mental Health</option>
  49. </select>
  50. <div id="data"></div>

//here the updatepage is not cakled anywhere which displays the str
also how do i use this in my example
i have used ajax earlier but as i said different way of cross browser creation
and even the rows creation was using button as i have mentioned
not with tab key
can u just show me how to use it in my example
i also have some select box and text box vales above the table which is created dynamically
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#30: Dec 14 '07

re: Tab key to create rows


hi ...

the following code should work now ...
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. //set the variables.
  3. function getArticle(id) {
  4.     var cb = function(res) {
  5.         alert(res.responseText);
  6.     };
  7.  
  8.     ajax('getNews.php', id,cb);
  9. }
  10.  
  11. //Make the actual connection.
  12. var ajax = function(strURL, sSend, cb) {
  13.     var xmlHttpReq=false;
  14.  
  15.     if (!window.ActiveXObject) {
  16.         ajax.req = new XMLHttpRequest();
  17.     } else {
  18.         ajax.req = new ActiveXObject("Microsoft.XMLHTTP");
  19.     }
  20.  
  21.     ajax.req.open('POST', strURL, false);
  22.  
  23.     ajax.req.setRequestHeader(
  24.         'Content-Type','application/x-www-form-urlencoded'
  25.     );
  26.  
  27.     ajax.req.xmlHttpReq.onreadystatechange = function() {
  28.         if (ajax.req.xmlHttpReq.readyState == 4) {
  29.             cb(ajax.req.xmlHttpReq);
  30.         }
  31.     };
  32.  
  33.     ajax.req.send(sSend);
  34.     return ajax.req;
  35. }
  36.  
  37. //set the query string to be sent
  38. function getquerystring(id) {
  39.     qstr = 'data=' + escape(id);  // NOTE: no '?' before querystring
  40.     return qstr;
  41. }
  42.  
  43. //put the data on the page.
  44. function updatepage(str) {
  45.     document.getElementById("data").innerHTML = str;
  46. }
  47. </script>
  48. <select onchange="getArticle(this.value);">
  49. <option value="1">Train</option>
  50. <option value="2">Airplane Crashes</option>
  51. <option value="3">Mental Health</option>
  52. </select>
  53. <div id="data"></div>
to your next step: could you summarize what your page looks like and what you want to do now exactly?

kind regards
Member
 
Join Date: Oct 2007
Posts: 103
#31: Dec 14 '07

re: Tab key to create rows


Error: ajax.req.xmlHttpReq has no properties

my getNews.php i have tried to alert the but it is not alerting that
also if this works
how do i use it in my dynamic created rows
and post the values


all confused

i have a form wher in i take soem details and then the table creation with one row in which tab key creates required more rows which i have managed with ur code
but now as u said posting part of all the data above the table as well as the table through ajax



my getNews file is
<?
news_id =$_POST("id");
$strSQL = "select * from news where news_id = " & new_id;
$rsdata=mysql_query($strSQL);
echo $rsdata['news_id'];
?>


thanks in advance
Member
 
Join Date: Oct 2007
Posts: 103
#32: Dec 14 '07

re: Tab key to create rows


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function add_row(idx) {
  5.     var table   = document.getElementById('tbl');
  6.     var row     = table.getElementsByTagName('tr')[idx];
  7.     var new_row = row.cloneNode(true);
  8.  
  9.     apply_names(table, new_row);
  10.  
  11.     table.getElementsByTagName('tbody')[0].appendChild(new_row);
  12. }
  13.  
  14. function apply_names(table, row) {
  15.     var inp  = row.getElementsByTagName('input');
  16.     var sel  = row.getElementsByTagName('select');
  17.     var sfx  = table.getElementsByTagName('tr').length;
  18.     var eles = [];
  19.     var ele;
  20.  
  21.     for (var i = 0; i < inp.length; i++) {
  22.         eles.push(inp[i]);
  23.     }
  24.  
  25.     for (i = 0; i < sel.length; i++) {
  26.         eles.push(sel[i]);
  27.     }
  28.  
  29.     for (i = 0; i < eles.length; i++) {
  30.         ele = eles[i];
  31.         ele.setAttribute('name', ele.getAttribute('name') + '_' + sfx);
  32.         ele.setAttribute('id', ele.getAttribute('id') + '_' + sfx);
  33.     }
  34.  
  35.     alert(row.innerHTML);
  36. }
  37.  
  38. function add_row_event(e) {
  39.     var val;
  40.  
  41.     if (typeof window.event != 'undefined') {
  42.         val = window.event.keyCode;
  43.         idx = 1;
  44.     } else {
  45.         val = e.keyCode;
  46.         idx = 0;
  47.     }
  48.  
  49.     if (val == 9) {
  50.         add_row(idx);
  51.     }
  52. }
  53. </script>
  54. </head>
  55.  <body>
  56.  <form method="post">
  57. <table>
  58. <tr><td><select name="dept" id="dept">
  59.     <option selected>--- Select depts---</option></select></td></tr>
  60. <tr><td><select name="study" id="study" >
  61.   <option selected>--- Select studies---</option></select></td></tr>
  62. <tr><td><input type="text" name="nos" id="nos"></td></tr>
  63. </table>
  64. <table id="tbl">
  65.     <thead>
  66.         <th>continent</th>
  67.         <th>country</th>
  68.         <th>longitude</th>
  69.         <th>latitude</th>
  70.         <th>time zone</th>
  71.     </thead>
  72.     <tr>
  73.         <td>
  74.             <select name="mcontinent" id="mcontinent"  tabindex="11">
  75.                 <option selected>--- Selectcontinenet ---</option>
  76.             </select>
  77.         </td>
  78.         <td>
  79.             <select name="mcountrycode" id="mcountrycode" tabindex="1" >
  80.                 <option selected>--- Selectcountry ---</option>
  81.             </select>
  82.         </td>
  83.         <td>
  84.             <input type="text" name="mlong" id="mlong"></td>
  85.         <td>
  86.             <input type="text" name="mlat" id="mlat" ></td>
  87.         <td>
  88.             <input type="text" name="mtime" id="mtime" onkeydown="add_row_event(event);">
  89.         </td>
  90.     </tr>
  91. </table>
  92. <input type="submit" name="submit" value"submit">
  93. </form>
  94. </body>
  95. </html>
//this is my code ofcourse the selct options are filled from database table values
and i want to post this data and save
thanks
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#33: Dec 14 '07

re: Tab key to create rows


Quote:

Originally Posted by SSG001

Error: ajax.req.xmlHttpReq has no properties

sorry i overlooked something ... xmlHttpReq is useless, so try this:

Expand|Select|Wrap|Line Numbers
  1. var ajax = function(strURL, sSend, cb) {
  2.     var xmlHttpReq=false;
  3.  
  4.     if (!window.ActiveXObject) {
  5.         ajax.req = new XMLHttpRequest();
  6.     } else {
  7.         ajax.req = new ActiveXObject("Microsoft.XMLHTTP");
  8.     }
  9.  
  10.     ajax.req.open('POST', strURL, false);
  11.  
  12.     ajax.req.setRequestHeader(
  13.         'Content-Type','application/x-www-form-urlencoded'
  14.     );
  15.  
  16.     ajax.req.onreadystatechange = function() {
  17.         if (ajax.req.readyState == 4) {
  18.             cb(ajax.req);
  19.         }
  20.     };
  21.  
  22.     ajax.req.send(sSend);
  23.     return ajax.req;
  24. }
  25.  
Member
 
Join Date: Oct 2007
Posts: 103
#34: Dec 14 '07

re: Tab key to create rows


Expand|Select|Wrap|Line Numbers
  1. //set the query string to be sent
  2. function getquerystring(id) {
  3.     qstr = 'data=' + escape(id);  // NOTE: no '?' before querystring
  4.     return qstr;
  5. }
  6.  
  7. //put the data on the page.
  8. function updatepage(str) {
  9.     document.getElementById("data").innerHTML = str;
  10. }
  11.  
this is not called anywhere in that function
which actually displays the data
so my program is nt showing any results


where am i going wrong?
and how can i use this in my example
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#35: Dec 14 '07

re: Tab key to create rows


Expand|Select|Wrap|Line Numbers
  1. var ajax = function(strURL, sSend, cb) {
  2.     var xmlHttpReq=false;
  3.  
  4.     if (!window.ActiveXObject) {
  5.         ajax.req = new XMLHttpRequest();
  6.     } else {
  7.         ajax.req = new ActiveXObject("Microsoft.XMLHTTP");
  8.     }
  9.  
  10.     // here you have to create the right querystring
  11.     var strURL = getquerystring(sSend);
  12.  
  13.     ajax.req.open('POST', strURL, false);
  14.  
  15.     ajax.req.setRequestHeader(
  16.         'Content-Type','application/x-www-form-urlencoded'
  17.     );
  18.  
  19.     ajax.req.onreadystatechange = function() {
  20.         if (ajax.req.readyState == 4) {
  21.             cb(ajax.req);
  22.         }
  23.     };
  24.  
  25.     ajax.req.send(sSend);
  26.     return ajax.req;
  27. }
  28.  
and in your cb you should use:

Expand|Select|Wrap|Line Numbers
  1. var cb = function(res) {
  2.     updatepage(res.responseText);
  3. };
Member
 
Join Date: Oct 2007
Posts: 103
#36: Dec 15 '07

re: Tab key to create rows


[HTML]<script type="text/javascript">
function GetXmlHttpObject(){
var xmlHttp=null;
try
{
xmlHttp=new XMLHttpRequest();
}
catch (e){
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
if (!xmlHttp && typeof XMLHttpRequest!='undefined') {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
function getArticle(id){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getNews.php";
url=url+"?&news_id="+id;
url=url+"&sid="+Math.random();
alert(url);
xmlHttp.onreadystatechange=function(){stateChanged ()} ;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("data").innerHTML=xmlHttp. responseText;

}
}
</script>
<form method="GET">
<select name="news_id" id="news_id" onchange="getArticle(this.value);">
<option value="1">-TRain--</option>
<option value="2">-aerolplane--</option>
<option value="3">-Car--</option>
</select>
<div id="data">VALUE is displayed here </div>
</form>[/HTML]


this works fine with me and displays my result also is there any prblem if i use this instead of your code
now can u tell me how do i use this in my example for posting the value odf dynamic rows as well as as data taken before the table creation
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#37: Dec 15 '07

re: Tab key to create rows


as i said some time before ... you simply could call getArticle() from the add_row() method ... but since getArticle() actually receives only one id you should adapt it to your needs ... you would have to retrieve all values of select- and textboxes of a row to build the right query-string for your purpose.

kind regards
Reply