Multidimensional array problems in $_POST
Posted on
16th Feb 2014 07:03 pm by
admin
I'm having trouble with a three-dimensional $_POST array. It starts as a two-dimensional array on this side:
Code: <html>
<head>
<title>test</title>
<script type="text/javascript">
var Dom = {
get: function(el) {
if (typeof el === 'string') {
return document.getElementById(el);
} else {
return el;
}
},
add: function(el, dest) {
var el = this.get(el);
var dest = this.get(dest);
dest.appendChild(el);
},
remove: function(el) {
// var el = this.get(el);
// el.parentNode.removeChild(el);
}
};
var Event = {
add: function() {
if (window.addEventListener) {
return function(el, type, fn) {
Dom.get(el).addEventListener(type, fn, false);
};
} else if (window.attachEvent) {
return function(el, type, fn) {
var f = function() {
fn.call(Dom.get(el), window.event);
};
Dom.get(el).attachEvent('on' + type, f);
};
}
}()
};
Event.add(window, 'load', function() {
var i = 2;
Event.add('add-element', 'click', function() {
var el = document.createElement('span');
el.innerHTML = '<input type="text" name="theArray['+i+']['name']" value="">';
i++;
Dom.add(el, 'content');
Event.add(el, 'click', function(e) {
Dom.remove(this);
});
});
});
</script>
</head>
<body>
<p id="add-element">Add Elements</p>
<form action="testPost.php" name="test" method="post">
<input type="text" name="theArray[1]['name']" value="foo" />
<input type="text" name="theArray[2]['name']" value="bar" />
<div id="content"></div>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
(If you're wondering why I only have one element it's because this is for a project where I will need nested lists later.)
Now, onto testPost.php to process the code:
Code: <html>
<head>
<title>asdfdf</title>
</head>
<body>
<ul>
<?php
for ($i=1;$i<=count($_POST["theArray"]);$i++){
echo("<li>".$_POST['theArray'][$i]['name']."</li>");
}
?>
</ul>
</body>
</html>
$_POST itself is an array, so we convert from a two-dimensional to a three-dimensional array. For some reason, only the first letter gets printed; the rest of the string gets truncated.
My only request is that you don't suggest I just use a database or something of the like. This has to be easily portable and I will probably merge this into one document later.
Thank you
99
49
Other php-forum
PHP Array quick help
I need
$_SESSION{'username'} to be like this:
if $_SESSION['username'] = "gay
Date Format
Hi there,
I have a date format like this right now:
Sat, 17 Oct 2009 17:04:00
I ne
session variables not working anymore
hello,
i use sessions to handle authentication within my site. my host has been changing a lot of
IMAGE - SERVER SIDE scripting help...
How to receive pixel data of an image and buid a bitmap file on the server using the HTTP POST metho
Warning message
I've put a website that I was doing live and I'm getting this warning message when I try to add a ne
a dificult string search
Hi I don't know a way around this. I want the user to input a password, but to make it a bit complca
Undefined index on my form
ok im getting Undefined index on this line.. print_r($_REQUEST['form']);
below is the full sc
Displaying image pathname instead of image
Hello
Im trying to upload and then display images from a mysql database - Its only basic and
Got A Free Server :(
So I was given a free server
http://shopping.yahoo.com/p:Compaq%20ProLiant%201500%20Server:1
whats wrong with my code please help!!!
this is the error
Warning: mysql_close(): supplied argument is not a valid MySQL-Link res