Monday, February 4, 2008

Retrieving $_POST Values From Dynamically Generated Form

Howdy, this morning I was having an argument with my php $_POST values array. It was not showing me the values I wanted.

I looked on the web for howto's, tutorials and tips and what not. Nothing actually made sense.

The answer to my problem was very simple though; in the javascript part of my page, I used appendChild to dynamically add form fields to my form. I used a hidden input type to track how many fields I added, but THEY WOULDN'T SHOW in my $_POST array.

My mistake was, that I only added "id" values to these form fields; php also expects a "name" value (the green javascript line) in order for the value to show up in the $_POST array. Problem solved.

My javascript:

input = document.createElement("input");
input.type = "text";
input.id = "some_name_" + (parseInt(row_number) + 1);
input.name = "some_name_" + (parseInt(row_number) + 1);
input.value = "";
form.appendChild(input); //make it part of the form

My php code:

print_r($_POST);



0 comments: