HTML forms are one of the most common methods of
The GET
method allows the users to effectively pass input parameters between different forms, thus serving as a form of communication between clients and web applications.
However, passing multiple inputs through the GET
method can be a bit tricky if not understood properly. Follow through these steps to effectively pass multiple inputs using the GET
method in HTML.
Create the HTML form.
Add input fields.
Encode the inputs.
Structure the URL.
Specify the form URL.
Receive the parameters on the server-side.
To start with passing inputs, you need a form to gather the inputs from the user and save them into corresponding variables for storage and passing.
You can use the HTML <form>
tag to define the HTML form and set the corresponding method
attribute to GET
. This attribute indicates that the form data will be sent through the parameters passed in the URL.
Next, add the necessary input fields in the form. These fields capture user data and store them in variables.
<input type="text" name="input1"><input type="text" name="input2"><!-- Add more input fields as needed -->
For each input field, use the <input>
tag to declare them as an input field. Moreover, ensure that you specify the appropriate type
and name
attributes for these input fields.
When the user submits the form, the data will be appended to the URL as query parameters. This allows the effective passing of parameters between two forms.
However, to handle special characters properly, you need to encode the value of both input fields before appending the to the URL. This can be done by encodeURIComponent()
function that is available in JavaScript.
After that, you can utilize JavaScript to construct the URL dynamically and append the encoded values into the form's action URL.
var input1Value = encodeURIComponent(input1);var input2Value = encodeURIComponent(input2); // encoding the input valuesvar actionUrl = form.action + '?input1=' + input1Value + '&input2=' + input2Value;form.action = actionUrl; // Specifying the URL
The form.action
refers to the attribute of the HTML form element that specifies the URL where the form data should be submitted.
'?input1=' + input1Value
appends the name of the first input field (input1
) followed by the encoded value (input1Value
) to the URL.
'&input2=' + input2Value
appends an ampersand (&
) followed by the name of the second input field (input2
) and its encoded value (input2Value
) to the URL.
form.action = actionUrl;
This line updates the form's action attribute with the newly constructed URL (actionUrl
).
The final URL should look something like this.
http://example.com/process.php?input1=value1&input2=value2
Where input1
and input2
are the parameters that were submitted by the user with their respective values value
and value2
. All these parameters are passed to the webpage process.php
.
On the server side, you will be using $_GET
to access the input values in the process.php
$input1 = $_GET['input1'];$input2 = $_GET['input2'];// Process the input values as needed
input1 = $_GET['input1'];
$_GET['input1']
retrieves the value of the URL parameter with the name "input1".
The value is then assigned to the variable input1
.
$input2 = $_GET['input2'];
$_GET['input2']
retrieves the value of the URL parameter with the name "input2".
The value is then assigned to the variable $input2
.
After retrieving these values, you can use them in your PHP script for further processing, validation, database operations, or any other tasks required by your application.
Free Resources