Page 1 of 1

Handling an array with unknown number of $_POST elements

Posted: Mon Apr 03, 2006 9:22 am
by mattcooper
This is in addition to a thread I started earlier... I'm really stuck!

I have developed a nice piece of JavaScript that creates new form elements:

Code: Select all

function addT(){ 
var root = document.getElementById('container'); 
var clone = root.getElementsByTagName('div')[0].cloneNode(true); 
var txt=clone.getElementsByTagName('input'); 
txt[0].value='URL';txt[1].value='label'; 
var but=document.createElement('input'); 
but.setAttribute('type','button'); 
but.setAttribute('value','Trash'); 
but.onclick=function(){remT(this.parentNode)} 
clone.insertBefore(but,txt[1].nextSibling) 
root.appendChild(clone) 
renameID() 
} 
function remT(p){ 
p.parentNode.removeChild(p); 
renameID(); 
} 
function renameID(){ 
var divs=document.getElementById('container').getElementsByTagName('div'); 
for(var i=0;i<divs.length;i++){ 
divs[i].getElementsByTagName('input')[0].name='link'+(i+1); 
divs[i].getElementsByTagName('input')[1].name='label'+(i+1); 
} 
}
Each time this function is called, new textfields for "URL" and "Label" are added dynamically, which are then submitted to the form handler for processing, outputting and insertion (eventually) to the database.

How do I handle the posted data? I've used the generic form handling script from the PHP manual to perform a var_dump() on all of the submitted information, and it's all there...

I can't figure out how to pair up the resultant $_POST data correctly (link1 with URL1, link2 with URL2 etcetera) and then: 1> echo them out; 2> Pass their value to a hidden field and; 3> Place the values into the database for output later (after form submission). An example of the code running in it's present state can be seen here: http://www.sageinteractive.co.uk/cms/in ... ation2.php

This is for a navigation builder where the user can add links to a "toolbar", and I need to be able to show a preview of the end result using the data submitted from the previous page. It is the most complex part of an app that I am developing currently, and I really need some help to sort out the code that 1> Provides the preview and; 2> Inserts the information into the database in such away that it can be easily accessed and manipulated in the final output.

Thanks in advance :)

Posted: Mon Apr 03, 2006 9:26 am
by feyd
you could use a regex to extract the number from the name, creating array elements with that data.

Posted: Mon Apr 03, 2006 9:55 am
by mattcooper
Feyd, I'm rubbish with arrays as I can't understand them - a very abstract principle that my brain simply will not visualise at the moment!

Assuming that the results of the post are (as above) "link1","link2","url1" and "url2" - although there is no current limit to how many there can be (and bearing in mind the fact that I have never used regex!) - how the h*ll do Igo about this? I understand as far as testing for a number in the $_POST data, but don't understand how to get them into an array with regex.

How about using substr() to leave just the number from each $_POST and echoing out the values from there? On the hop, I think that would work but I'm not sure.

Am I thinking correctly, or is there a batter way (one that I'll understand, preferably!)

Cheers, Feyd.

Posted: Mon Apr 03, 2006 10:03 am
by JayBird
i would do it something like this

change these lines...

Code: Select all

divs[i].getElementsByTagName('input')[0].name='link'+(i+1);
divs[i].getElementsByTagName('input')[1].name='label'+(i+1);
...to...

Code: Select all

divs[i].getElementsByTagName('input')[0].name='link['+(i+1)+']';
divs[i].getElementsByTagName('input')[1].name='label['+(i+1)+']';
Then, on the script that processes the values, use something like this

Code: Select all

foreach($_POST['link'] as $k=>$v) {

    echo $v;
    echo "<br />";
    echo $_POST['label'][$k];

}
Something like that should work

Posted: Mon Apr 03, 2006 10:06 am
by feyd
an untested example of what I was talking about

Code: Select all

$links = array();

foreach($_POST as $name => $value)
{
	if(preg_match('#^(link|url)(\d+)$#i', $name, $match))
	{
		if(is_null($links[$match[2]]))
		{
			$links[$match[2]] = array();
		}
		
		$links[$match[2]][strtolower($match[1])] = $value;
	}
}