5 Replies - 8953 Views - Last Post: 13 December 2010 - 05:32 PM

#1 camilahlah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-December 10

Getting value of textboxes inside a DIV using AJAX

Posted 11 December 2010 - 07:24 PM

I need to get the values of all textboxes that I've generated through dropdown list using AJAX. I've been getting only the last value of the textbox, I can' seem to loop it. Anyway, here's my code:


<?
include('connect.php');

$tag = $_REQUEST['tag'];
$ProjectName  = $_REQUEST['ProjectName'];
$Description = $_REQUEST['Description'];
$textboxes = $_REQUEST['textboxes'];

if($tag=='A')
{

	//$insert = mysql_query("INSERT INTO tProject (ProjectName, Description, Pump) VALUES ('$ProjectName','$Description', '$textboxes')") or die (mysql_error());
	echo "Saved!";
}


?>
<html>
<head>
	
	<script type="text/javascript" src="ajaxhandler.js">

	</script>

</head>
<body>

<form method = "POST">

Project Name: 
	<input type="text" id="ProjectName"><br>

Description:
	<input type="text" id="Description"><br>

Pump:
<select name="boxes" onchange="Pump(this.value);">
	<option value="">choose</option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	<option value="6">6</option>
	<option value="7">7</option>
	<option value="8">8</option>
	<option value="9">9</option>
	<option value="10">10</option>
</select>
<div id='textboxes'>
</div>
<div id='textboxes1'>
</div>

<input type="button" onclick=chpage('AddP.php?tag=A') value="Save">
<input type="button" value="Clear">

</form
</body>
</html>




My AJAX code:

function chpage(url){ 
	var xmlHttp = getXMLHttp();
  
	        if ( url == "AddP.php?tag=A") 
		{
			var x= document.getElementById('ProjectName').value;
			var y= document.getElementById('Description').value;
		  	cn = document.getElementById( "textboxes" ).childNodes;
 			for (var i = 0; i < cn.length; i++) 
  			{  
    				var a = cn[i].value;
    				if(a==null)
    				{
    				}
    				else
    				{
    					var strURL = url + "&ProjectName=" + x + "&Description=" + y + "&textboxes=" + a;
    					
    				}
  			}
			
		}	 	

		else {
			var strURL =url;
		}
 
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
        HandleResponse(xmlHttp.responseText);
        document.getElementById('loading').style.visibility = "hidden";
      
    }
  }
	document.getElementById('loading').style.visibility = "visible";
	xmlHttp.open("GET", strURL, true);
	xmlHttp.send(null);
}


Any help would be very much appreciated. Thank you. =)

Is This A Good Question/Topic? 0
  • +

Replies To: Getting value of textboxes inside a DIV using AJAX

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,049
  • Joined: 08-June 10

Re: Getting value of textboxes inside a DIV using AJAX

Posted 12 December 2010 - 02:09 AM

the problem is line #11. the value is undefined ("textboxes" is an empty <div>) thus the condition on line #12 evaluates to true
Was This Post Helpful? 0
  • +
  • -

#3 camilahlah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-December 10

Re: Getting value of textboxes inside a DIV using AJAX

Posted 12 December 2010 - 07:24 PM

Yup, at first loop it is empty but on the second or third loop the data is there. That's why I put the condition if(a==null). Anyway, my problem is how to pass it to the client side and loop it as well. Cause the only data that it gets is the last data executed after the loop.Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,049
  • Joined: 08-June 10

Re: Getting value of textboxes inside a DIV using AJAX

Posted 13 December 2010 - 09:36 AM

what second and third loop? I dont see any.
Was This Post Helpful? 0
  • +
  • -

#5 camilahlah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-December 10

Re: Getting value of textboxes inside a DIV using AJAX

Posted 13 December 2010 - 05:23 PM

try to change this:

	                    {

                        var strURL = url + "&ProjectName=" + x + "&Description=" + y + "&textboxes=" + a;
                         
                            }




into this:

	                    {
                                     alert(a);
                            }


Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,049
  • Joined: 08-June 10

Re: Getting value of textboxes inside a DIV using AJAX

Posted 13 December 2010 - 05:32 PM

Id hazard a guess at undefined
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1