Kilizo
Kilizo

Reputation: 3392

How to send info on HTML elements via POST?

I want to send the properties of HTML elements as data via POST, for example whether an element is visible or not?

Upvotes: 2

Views: 2119

Answers (6)

Tabrez Ahmed
Tabrez Ahmed

Reputation: 2960

Include Hidden inputs using PHP like the following:

<input type="hidden" id="hidden1" name="hidden1" value="<?php if(condition) echo "default"; else echo "default";?>">

This default value can be set by PHP during page load, for transferring extra hidden data from one page load to another. But can also be modified by javascript after page load:

<script type="text/javascript">
document.getElementById("hidden1").value="true";
</script>

Note: PHP can change the value of any hidden or non-hidden element only during the next page load. It doesn't have any control over the HTML it sends to the browser. This is why you need Javascript(Client side scripting).

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270707

You cannot do it with PHP and HTML alone, since the HTML form would only post a form input's name. You would need to add some JavaScript, which at the time the form is submitted, would iterate over all its inputs and modify their values to include the attribute values as well.

Example:

yourform.onbeforesubmit = function() {
  // Loop over form elements and append -visible or -hidden to its value based on CSS style
  // jQuery selectors like .is(":visisble") would help a lot here.
  // This is just a basic example though - it would require an explicit visibility CSS rule on each 
  // input element...
  for (var i=0; i<yourform.elements.length; i++) {
    yourform.elements[i].value = += "-" + yourform.elements[i].style.visibility;
  }
}

Another method would be rather than to modify the values of the inputs themselves, keep a hidden input for each visible user input and set the attributes as the value to the hidden input rather than the visible input.

Upvotes: 2

Sebas
Sebas

Reputation: 21542

You have to define your data definition standard first: what do you want to store, and under what name.

then, imho you have to serialize the result and send it through POST, for finally unserializing it once at the server.

Use JSON serialization for an effective way of proceeding.

Upvotes: 0

abon999
abon999

Reputation: 336

In ajax. Try Prototype Framework, it is really easy to use! http://prototypejs.org/api/ajax/request

Upvotes: 1

RandomWhiteTrash
RandomWhiteTrash

Reputation: 4014

If you want to do that I suppose you will have to create some hidden fields and javascript that would fill them in with information depending on your elements attributes. As far as I know there is no other way.

Upvotes: 0

Pascal
Pascal

Reputation: 8646

You can not do this with PHP. You will need to use Javascript to determine this information and then either send an Ajax Request or add this information to an existing form.

To elaborate a bit more: PHP is executed Server Side and then sent to the Client (Browser). The Server is not aware of the state of the HTML Elements in the Browser.

As far as i can tell you have a form that is submitted anyway? So add a piece of javascript which is called before the form is submitted (onsubmit property of the form) and have it read out the status of the elements (visible, hidden, ...) and set this information to some hidden form fields.

Make sure the javascript that is called before the form is submitted is returning true, otherwise the action gets cancelled.

Upvotes: 1

Related Questions