Php get value from HTML element

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get".

$_GET can also collect data sent in the URL.

Assume we have an HTML page that contains a hyperlink with parameters:


Test $GET


When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and you can then access their values in "test_get.php" with $_GET.

Element values and Data sources – Setting and getting values for the whole form and for individual elements

Each element in HTML_QuickForm2 implements and methods that return its unfiltered and filtered submit value, respectively, and that sets the element's display value. These are not always the same, consider

$text = new HTML_QuickForm2_Element_InputText(
    'testText', array('disabled' => 'disabled')
);
$text->setValue('a value');
echo $text . "\n";
var_dump($text->getValue());
?>

which produces



NULL

as disabled elements cannot have a submit value.

If an element can not have a submit value (e.g. reset button, static element) or does not currently have one (e.g. disabled element, unchecked checkbox) then getValue() will return NULL. The value also passes "intrinsic validation" which ensures that it could possibly come from that element:

$select = HTML_QuickForm2_Factory::createElement('select', 'testSelect',
                                                 array('multiple' => 'multiple'))
              ->loadOptions(array('a' => 'letter A', 'b' => 'letter B', 'c' => 'letter C'));
$select->setValue(array('a', 'z'));
// select will only return values for options that were present in it
var_dump($select->getValue());
?>

will output


array(1) {
  [0]=>
  string(1) "a"
}

On the other hand, some of the elements cannot have a display value (, file uploads), setValue() will be a no-op for such elements and they will only get their values (click coordinates and file upload data, respectively) from submit data sources.

getValue() / getRawValue() also work for Containers and return an array with values of contained elements. setValue() is only implemented for groups, data sources should be used to set the values for the whole form.

Instance of HTML_QuickForm2 contains an array of Data sources - objects storing elements' incoming values. These values may either originate from HTTP request data (submit values) or be provided by the programmer (default values). Data sources implement HTML_QuickForm2_DataSource interface defining a single getValue() method, which receives element name and returns either element value or NULL if such value is not present.

There is also a HTML_QuickForm2_DataSource_Submit interface that defines an additional getUpload() method that receives file upload name and returns either file upload data from $_FILES array or NULL if it doesn't contain this data.

A data source containing submit values will be added to the list automatically if of HTML_QuickForm2 considers the form submitted. You can add another data source to the list using and completely replace the list using .

An element will try to update its value from data sources in the following cases:

  • It is added to the form;
  • Its name is changed;
  • Form data sources list is changed.

To perform that update it gets the data sources array from the form and iterates over it calling getValue() until a non-NULL value is returned. This value is then used as element's value.

Some of the elements (submit buttons, obviously file uploads) will only consider getting their values from instances of HTML_QuickForm2_DataSource_Submit. Some (e.g. checkboxes) will stop iterating over the array as soon as an instance of HTML_QuickForm2_DataSource_Submit is found, even if its getValue() method returns NULL. Static elements will only consider a datasource if it is not an instance of HTML_QuickForm2_DataSource_Submit.

The package contains several classes implementing the HTML_QuickForm2_DataSource interface, but the only one that should be used directly is HTML_QuickForm2_DataSource_Array, other classes are used internally by the package.

An instance of HTML_QuickForm2_DataSource_Array wraps around an array with a structure similar to that of superglobal $_GET / $_POST arrays. This wrapper allows searching for values of elements with complex names:


array(1) {
  [0]=>
  string(1) "a"
}
0

outputs


foo value
bar 2
found a value

Instead of loading data from a database unconditionally and passing it to this data source in an array, it may make sense to create your own data source implementation, which will only perform a query when asked for a value. This way you will probably save a query on form submit, as all values will be found in a submit data source.

A most popular value-related error happens when a programmer uses an element's setValue() method before adding that element to the form:


array(1) {
  [0]=>
  string(1) "a"
}
1

As described above, the element will immediately try to update its value from form's data sources and overwrite whatever was set on the previous step, so the above results in

How to get data from HTML form in PHP?

How to retrieve form data sent via GET. When you submit a form through the GET method, PHP provides a superglobal variable, called $_GET. PHP uses this $_GET variable to create an associative array with keys to access all the sent information ( form data ). The keys is created using the element's name attribute values.

How do you find the value of an element in HTML?

Input Text value Property.
Change the value of a text field: getElementById("myText"). ... .
Get the value of a text field: getElementById("myText"). ... .
Dropdown list in a form: var mylist = document. ... .
Another dropdown list: var no = document. ... .
An example that shows the difference between the defaultValue and value property:.

How to get HTML element ID in PHP?

The DOMDocument::getElementById() function is an inbuilt function in PHP which is used to search for an element with a certain id. Parameters:This function accepts a single parameter $elementId which holds the id to search for. Return Value: This function returns the DOMElement or NULL if the element is not found.

How to get user input value in PHP?

To get input from users, you also have to prompt them to enter something. You can use PHP's `readline() function to get this input from the console.