fawad
fawad

Reputation: 1353

Pass html variables to PHP using "ID" not "Name"

I have HTML code like below

<input type = "textarea" id="sentence1"> Here is my sentence </textarea>
<input type="hidden" name="sentence2" value="This is another sentence">

PHP:

$_POST['sentence1']  //shows nothing
$_POST['sentence2'] // works fine

I want to get the value of sentence1 also But i have such a scattered code that for textarea i can't change "id" to "name" otherwise i'll have to make lot of changes in different files.

I have to transfer both sentences to PHP so please help me how can i do that?

Upvotes: 1

Views: 16110

Answers (6)

manixrock
manixrock

Reputation: 2543

I'm assuming you've corrected the <input type="textarea"> mistake. If I understand your needs correctly, you know that the form only passes the value of the name attribute, and not the id, but you can't change that easily.

My suggestion would be to add a script to either change the value of the name attribute into that of the id, or copy the entire attribute so it's available under both the name and the id:

<script>  
var inputs = document.getElementsByTagName('input');
for (var i=0; i<inputs.length; i++)  
    inputs[i].parentNode.appendChild(inputs[i].cloneNode(false));  
</script>

and add it at the end of your pages. do the same for textareas.

Upvotes: 1

Marian
Marian

Reputation: 6257

But i have such a scattered code that for textarea i can't change "id" to "name" otherwise i'll have to make lot of changes in different files. Then probably that's where you should start. (Note: A input/textarea element may have both a name and an id attribute.)

Upvotes: 0

Anax
Anax

Reputation: 9372

Change

<input type = "textarea" id="sentence1"> Here is my sentence </textarea>

into

<textarea id="sentence1" name="sentence1"> Here is my sentence </textarea>

and your PHP will work.

Upvotes: 3

Symen Timmermans
Symen Timmermans

Reputation: 907

Your code is wrong. You should start using a browser with HTML validation, that would've caught it.

<textarea name="sentence1" id="sentence1"> Here is my sentence </textarea> 

<input type="hidden" name="sentence2" value="This is another sentence" />

Upvotes: 3

Stefan Gehrig
Stefan Gehrig

Reputation: 83622

You can't (at least not using plain HTML). The W3C recommendation regarding form handling and processing states

A control's "control name" is given by its name attribute. The scope of the name attribute for a control within a FORM element is the FORM element.

and

A form data set is a sequence of control-name/current-value pairs constructed from successful controls

See here.

You can use Javascript to add the name-attribute dynamically to all form elements without the respective attribute.

Upvotes: 0

Gordon
Gordon

Reputation: 316969

There is no input type="textarea". You cannot use the id attribute for the name attribute. This is not how it will get transmitted and there is no way to change that from plain HTML. Form <input> elements are transmitted with their name and value attributes.

Please refresh you knowledge about HTML Form elements and attributes

Upvotes: 5

Related Questions