Current location: Hot Scripts Forums » General Web Coding » JavaScript » AJAX Post an Array trouble


AJAX Post an Array trouble

Reply
  #1 (permalink)  
Old 07-17-07, 05:39 AM
scott2500uk's Avatar
scott2500uk scott2500uk is offline
Coding Addict
 
Join Date: Apr 2006
Posts: 275
Thanks: 2
Thanked 2 Times in 2 Posts
AJAX Post an Array trouble

I have on my page several input boxes which i have in an array:

HTML Code:
    <td><input id="DealerShipBuddySites[]" type="text" size="40"></td>
    <td><input id="DealerShipBuddySites[]" type="text" size="40"></td>
    <td><input id="DealerShipBuddySites[]" type="text" size="40"></td>
    <td><input id="DealerShipBuddySites[]" type="text" size="40"></td>
    <td><input id="DealerShipBuddySites[]" type="text" size="40"></td>
If I do a normal post I get an array in $_POST['DealerShipBuddySites'] perfectly fine. But Im having trouble trying to post that array with AJAX. Below is my ajax code:

Code:
//Browser Support Code
function ajupdateform(){
	grayOut(true, {'zindex':'101', 'bgcolor':'#000000', 'opacity':'70'});
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajupdatediv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	
	var d1 = document.getElementById('DealerShipBuddySites').value;
	var queryString = "DealerShipBuddySites="+d1;
	ajaxRequest.open("POST", "test.php", true);
	ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; Charset=UTF-8');
	ajaxRequest.send(queryString);	
}
With the above Im getting "undefined" in d1 var. Now I know that getElementById is only ment to get one value so Ive seen getElementsByName but I cant get that to work too. Please help

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-17-07, 03:02 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
First I'd like to as why you use brackets in input-element ids? They are unneccessary and might lead to confusion.

document.getElementById will only return the first element if more than one with the specified id exists.

In your case, you'd need to use document.getElementById("DealerShipBuddySites[]") to get a reference to the first input field. As it is now, you should get an error because document.getElementById("DealerShipBuddySites") will return null. And null has no .value property (it might evaluate to undefined tho).

If you use getElementsByName("DealerShipBuddySites[]") (Again, remove the brackets, they are just in the way) with your code you'd get an array containing references to all the input elements (you must also give them a name attribute of course). Then you can loop through the array returned by getElementsByName and append each element's .value properties to the querystring.

I'm not sure why you'd ever want more than one textbox with the same name/id, only radiobuttons benefit from that since they must be grouped by name in order to function properly.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-17-07, 03:08 PM
jfulton's Avatar
jfulton jfulton is offline
Community VIP
 
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
This is completely untested, but should be a step in the right direction...
HTML Code:
<!-- changed ID to NAME...IDs must be unique...1 per page! -->
    <td><input name="DealerShipBuddySites" type="text" size="40"></td>
    <td><input name="DealerShipBuddySites" type="text" size="40"></td>
    <td><input name="DealerShipBuddySites" type="text" size="40"></td>
    <td><input name="DealerShipBuddySites" type="text" size="40"></td>
    <td><input name="DealerShipBuddySites" type="text" size="40"></td>
Code:
//Browser Support Code
function ajupdateform(){
	grayOut(true, {'zindex':'101', 'bgcolor':'#000000', 'opacity':'70'});
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajupdatediv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}


	//---=== NEW CODE BELOW HERE!!!
	var queryString = ""
	var els = document.getElementsByName('DealerShipBuddySites');
	for (var i=0; i<els.length; i++) {
		if (i > 0) {
			queryString += "&";
		}
		queryString += "DealerShipBuddySites=" + els[i].value;
	}


	ajaxRequest.open("POST", "test.php", true);
	ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; Charset=UTF-8');
	ajaxRequest.setRequestHeader("Content-length", queryString.length);
	ajaxRequest.setRequestHeader("Connection", "close");
	ajaxRequest.send(queryString);	
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 07-18-07, 04:59 AM
scott2500uk's Avatar
scott2500uk scott2500uk is offline
Coding Addict
 
Join Date: Apr 2006
Posts: 275
Thanks: 2
Thanked 2 Times in 2 Posts
thanks for the help guys. The reason why I was making inputs into arrays is because on the page there is 450+ input fields. Submiting them all in one would mean right 1000+ lines of codes. Thats why I wanted to split into arrays.

I decided to change all arrays to number elements for example:

DealerShipBuddySites0
DealerShipBuddySites1
etc

then just made DealerShipBuddySites=DealerShipBuddySites0:ealer ShipBuddySites1::etc

then at other end I would use php to explode the string into an array the way I want it.

Once again thanks for the input
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 07-18-07, 03:15 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
450+ input fields? Oh my... What's it used for, and isn't there a more convenient solution?
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 04-06-09, 12:59 AM
sorcererayush sorcererayush is offline
New Member
 
Join Date: Apr 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
I have Fine The Solution

use for $_POST["name"]
query = "name[] =Name1&name[]=name2&name[]=name3"

this will process you Name field into array on php

<?php
print_r($_POST[name]);
?>

Best of Luck
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 04-06-09, 11:30 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by scott2500uk View Post
The reason why I was making inputs into arrays is because on the page there is 450+ input fields.
I think I speak for all of us here when I say, "You're doing it wrong." Without even looking at the application, I can tell you that this isn't a good design. No user interface should have 450 inputs on a page- even the Space Shuttle's pre-flight control and configuration system doesn't do that.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 04-06-09, 04:12 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
You guys realize this thread has been dead for a looong time?
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 04-07-09, 08:03 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by TwoD View Post
You guys realize this thread has been dead for a looong time?
I didn't, but I do now. lol
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 11-30-09, 11:39 AM
kalyson1 kalyson1 is offline
New Member
 
Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Well, I don't care how old it is. It saved me -- I was in quite a technical rut. The posts that helped me the most were the code samples and especially the note by sorcererayush.

People are still looking for help no matter how old the post is, so I am glad it was here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Add / Remove form elements, that post array m_abdelfattah JavaScript 7 06-24-07 10:11 PM
AJAX - Using the POST Method instead of GET mcrob JavaScript 8 02-21-07 05:06 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
Serializing a set of arrays dannyallen PHP 2 10-11-04 04:04 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 07:06 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.