Guys, im scratching my head around this one.
I have this website that basically contains a few forms that are filled in by the user. The user then can download that information in human readable format (pdf) or machine readable format (xml) but I'm having a slight problem submitting textboxes.
I have a few of them, for instance in the description section, but when i access the $_POST['Desc_Desc_desc'] value, it's empty even though i can see content on the textarea. The weird thing is that when i use firebug to inspect the element, it shows the element as if it had no contents..
Can anyone figure what is causing this strange behavior?
In service_level_library.buttons.prepForSubmit
, the textarea is cloned along with the rest of the form via the DOM cloneNode
method. This copies HTML element attributes, but not DOM properties. (Sometimes DOM element node properties have a corresponding attribute, so updating the property affects the attribute, which can make it appear that DOM properties are being copied.)
While textarea DOM objects have a value
property, the textarea HTML element doesn't have a corresponding value
attribute, so the value
property isn't exposing an attribute. Thus when you clone the node, the (empty) value
attribute is what gets copied, leaving the current value of the element (as accessible via the value
property) behind.
To fix your script, do one of:
- Copy the value after cloning.
- Set the initial value for the textarea, either by assigning to the
defaultValue
property or setting the text content of the node, before cloning. This works because the current value of the cloned node will be set to its initial value, and a deep copy of a textarea will copy its text contents (the source of its initial value).
- Programmatically replace the textarea with an input before cloning (though this would be more involved than the other options),
You say in your question $_POST['Desc_Desc_desc']
, although in the code I see a textarea with name Dep_desc
and id Dep_Desc_Desc
. Then you should write $_POST['Dep_desc']
, i.e. the name
of the <textarea>
instead of the id
.
Also, textarea
s don't have a value
attribute, so in your html you should write the initial content between the opening and the closing tag.
HTML
<textarea name="Dep_desc" id="Dep_Desc_Desc">Initial content</textarea>
PHP
echo "The content of the textarea is ".nl2br(htmlspecialchars($_POST['Dep_desc']));
notes
nl2br
: Respect new lines in the html output replacing the \n
symbol with <br />
.
htmlspecialchars
: Prevent possible XSS
attacks.
I used firebug to analyze what gets sent to your script by
document.getElementById('descriptionForm').submit()
The form data is being sent correctly
Content-Type: application/x-www-form-urlencoded Content-Length: 113
Desc_Desc_name=SO&Desc_Desc_keywords=overflow&Desc_Desc_concept=http%3A%2F%2Fso.com&Desc_Desc_desc=Stack+overflow
- The form action parameter is currently set to "submit.action" which yields a 404.
- The form buttons are defined outside your form tag.
- The form buttons does not activate a submit on your form.
Bottom line: the form does not get submitted to the intended recipient.
I had a similar issue but later I found out that somewhere at the bottom, I had reused the same name for another element which was empty. That wiped off my required element at var_dump, print_r as well. It took a while to get that out.