php and ajax dynamically created select box onchange event | Newbie | | Join Date: Mar 2007
Posts: 10
| |
i want to basically take some information for the product and let the user
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|text|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 - 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;
-
-
}
- 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);
-
?>
|  | Lives Here | | Join Date: Jan 2007 Location: India (West-Bengal)
Posts: 2,451
| | | re: php and ajax dynamically created select box onchange event
Please use Code tags then it will be easy to Solve your Problem.
Kind regards,
Dmjpro.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: php and ajax dynamically created select box onchange event
Leena, welcome to TSDN!
Did you know that you can specify languages in the code tags, e.g. [code=javascript]...[/code.] (remove the dot).
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: php and ajax dynamically created select box onchange event
Give your dynamically created elements ids. For your select, add the onchange when you create the element.
| | Newbie | | Join Date: Mar 2007
Posts: 10
| | | re: php and ajax dynamically created select box onchange event - j=1;
-
function add_rows(obj){
-
-
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+']'));
i was trying the above commented line in add_rows function while creating a select box using createElement function for onchange where am i going wrong
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: php and ajax dynamically created select box onchange event Quote:
Originally Posted by Leena P i was trying the above commented line in add_rows function while creating a select box using createElement function for onchange where am i going wrong Use onchange (lower case "c").
| | Newbie | | Join Date: Mar 2007
Posts: 10
| | | re: php and ajax dynamically created select box onchange event
i tried that but then my add rows button does not create new row some error
but if comment that part then it works
how to give onchage event when the select box is created - 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);
-
selectBox.setAttribute("id",nm);
-
//after this say selectBox.onchange=showmcode(nm.value)........ ??????
-
where nm is the name of the selectbox
-
return selectBox;
-
}
-
i tried this bt giving error property not supported
thanks.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: php and ajax dynamically created select box onchange event
Please use code tags when posting code. Thanks!
You need to assign a function object to the onchange, not the result of the function. See this article.
Try: - selectBox.onchange = function() {
-
showmcode(selectBox.value);
-
}
| | Newbie | | Join Date: Mar 2007
Posts: 10
| | | re: php and ajax dynamically created select box onchange event
i tried that but then my add rows button does not create new row some error
but if comment that part then it works
how to give onchage event when the select box is created - 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);
-
selectBox.setAttribute("id",nm);
-
//after this say selectBox.onchange=showmcode(nm.value)........ ??????
-
where nm is the name of the selectbox
-
return selectBox;
-
}
-
-
i tried this bt giving error property not supported
thanks.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: php and ajax dynamically created select box onchange event
Make sure you use code tags. This is the final friendly request. The last post seems like a repeat of your previous comment.
| | Newbie | | Join Date: Mar 2007
Posts: 10
| | | re: php and ajax dynamically created select box onchange event - selectBox.onchange = function() {
-
showmcode(selectBox.value);
-
}
i did this but now it add a record but once i select value from dynamically added select box the value of m_name is not shown in corresponding text box there and my static rows um and m_name disappears which i had selected and where onchange event it had executed ajax script to display m_name and um
thanks
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: php and ajax dynamically created select box onchange event
Try - selectBox.onchange = function() {
-
showmcode(this.value);
-
}
Don't forget to set the values of the option elements.
| | Newbie | | Join Date: Mar 2007
Posts: 10
| | | re: php and ajax dynamically created select box onchange event
same problem i changes it to this.value
i'm setting the options in the getselectbox function
and then actual database values are passed in test.php program
can't understand what is wrong with my code
thanks
|  | Moderator | | Join Date: Aug 2007 Location: Bowmanville, Ontario
Posts: 329
| | | re: php and ajax dynamically created select box onchange event
Try.. -
selectBox.onchange = function() {
-
showmcode(this.options[this.selectedIndex].value);
-
}
-
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: php and ajax dynamically created select box onchange event Quote:
Originally Posted by Leena P same problem i changes it to this.value
i'm setting the options in the getselectbox function
and then actual database values are passed in test.php program
can't understand what is wrong with my code You're not setting the value of the options. Set the value by using .value or setAttribute, e.g. - optionItem.value = 'someval';
| | Newbie | | Join Date: Mar 2007
Posts: 10
| | | re: php and ajax dynamically created select box onchange event
i writing a code to create select box - 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);
-
selectBox.setAttribute("id",nm);
-
s=new function(){}
-
selectBox.onchange =new function() {
-
showmcode(this.options[this.selectedIndex].value);
-
}
-
return selectBox;
-
}
which is called here - function create_object(objType,nm){
-
if(objType == 'select'){
-
return getSelectBoxDOM(select_opt,nm);
-
//select_opt.onchange = function() {
-
// showmcode(this.options[this.selectedIndex].value);
-
// }
-
}
-
-
obj = document.createElement("input");
-
obj.setAttribute("type",objType);
-
obj.setAttribute("name",nm);
-
obj.setAttribute("id",nm);
-
-
return obj;
-
}
and when i'm adding rows the function create_object is called - 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");
-
tdObj.appendChild(create_object(tabCols[i],col_names[i]+'['+j+']'));\
-
tdObj.appendChild(op.value);
-
trObj.appendChild(tdObj);
-
}
-
obj.firstChild.appendChild(trObj);
-
j++;
-
}
and in test.php i'm assigning values to options
so when the form is loaded select options are filled with database values
[PHP]<?
$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] ?>';
<?
}
?>[/PHP]
now how and where to writea code to give onchange event to the select box
i tried create_object function where objtype="select" and where getselectbox function is called but same error.
i also tried in addrow function just after creating object ..
i'm new to ajax and dom
so please tell me how do do this
i have a static row where the same m_cde select box is there where this function is working prooerly
but when i do it for dynamic rows if i alert then it shows [object]
and with static rows it shows the allthe object created with id and all
also how do i pass the m_cde as arry to the url inthe showmcode function for rows .
thanks
| | Newbie | | Join Date: Mar 2007
Posts: 10
| | | re: php and ajax dynamically created select box onchange event
but i'm assigning values to select box in the main program test.php from the database to the javascript array variable
and creating the select boxes in the test.js
so is it because of that it is creating problem?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: php and ajax dynamically created select box onchange event
Change line 5 in getSelectBoxDOM to: - optionItem.text = options[x];
-
optionItem.value = options[x];
You could use setAttribute if you want instead, e.g. - optionItem.setAttribute("value",options[x]);
| | Newbie | | Join Date: Mar 2007
Posts: 10
| | | re: php and ajax dynamically created select box onchange event
Thanks
my onchange event function works in select option
but then it gives me error in mozilla
Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)"
this is in the add_rows function
when i try to append -
tdObj.appendChild(create_object(tabCols[i],col_names[i]+'['+j+']'));
-
trObj.appendChild(tdObj); }
-
obj.firstChild.appendChild(trObj); //this line the above error occurs
-
am i not setting the attributes in order the way it should for the objects created
and no rows are added dynamically
but in IE 6 it works and even the new rows are inserted
but the value of m_name is not assigned to the corresponding text box in then newly created row like it is shown in static row
how do i pass it in the test1.js in the statechaged1 corresponding to that row cell created
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,126
| | | re: php and ajax dynamically created select box onchange event
hi ...
please have a look at the correct usage of the code-tags:
[CODE=javascript] code goes here [/code]
kind regards
| | Newbie | | Join Date: Mar 2007
Posts: 10
| | | re: php and ajax dynamically created select box onchange event
[quote=Leena P]i want to basically take some information for the product and let the user
enter the the material required to make this product
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: php and ajax dynamically created select box onchange event Quote:
Originally Posted by Leena P Thanks
my onchange event function works in select option
but then it gives me error in mozilla
Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)"
this is in the add_rows function
when i try to append -
tdObj.appendChild(create_object(tabCols[i],col_names[i]+'['+j+']'));
-
trObj.appendChild(tdObj); }
-
obj.firstChild.appendChild(trObj); //this line the above error occurs
-
You're trying to append to a whitespace node. Don't use firstChild. Use a tbody and append to that instead.
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|