I have a input field as follows:
<input type="text" name="subject" id="subject" value="Car Loan">
I would like to get the input fields value Car Loan
and assign it to a session. How do I do this using PHP or jQuery?
In order to get an input value, you can use the htmlspecialchars() method and $_REQUEST variable. Note: The htmlspecialchars() method functions by converting special characters to HTML entities. The $_REQUEST variable is a built-in PHP variable that functions by getting data from the input field.
Use PHP's $_POST or $_GET superglobals to retrieve the value of the input tag via the name of the HTML tag. To show the value: <? php echo $_GET['subject']; ?>
js"></script> </head> <body> <script type="text/javascript"> $. post('test. php',{'data': "value1"}); </script> <? php $data = $_POST['data']; print( "data is: $data" ); ?>
Use PHP's $_POST
or $_GET
superglobals to retrieve the value of the input tag via the name of the HTML tag.
For Example, change the method in your form and then echo out the value by the name of the input:
Using $_GET
method:
<form name="form" action="" method="get"> <input type="text" name="subject" id="subject" value="Car Loan"> </form>
To show the value:
<?php echo $_GET['subject']; ?>
Using $_POST
method:
<form name="form" action="" method="post"> <input type="text" name="subject" id="subject" value="Car Loan"> </form>
To show the value:
<?php echo $_POST['subject']; ?>
Example of using PHP to get a value from a form:
Put this in foobar.php:
<html> <body> <form action="foobar_submit.php" method="post"> <input name="my_html_input_tag" value="PILLS HERE"/> <input type="submit" name="my_form_submit_button" value="Click here for penguins"/> </form> </body> </html>
Read the above code so you understand what it is doing:
"foobar.php is an HTML document containing an HTML form. When the user presses the submit button inside the form, the form's action property is run: foobar_submit.php
. The form will be submitted as a POST request. Inside the form is an input tag with the name "my_html_input_tag". It's default value is "PILLS HERE". That causes a text box to appear with text: 'PILLS HERE' on the browser. To the right is a submit button, when you click it, the browser url changes to foobar_submit.php
and the below code is run.
Put this code in foobar_submit.php in the same directory as foobar.php:
<?php echo $_POST['my_html_input_tag']; echo "<br><br>"; print_r($_POST); ?>
Read the above code so you know what its doing:
The HTML form from above populated the $_POST superglobal with key/value pairs representing the html elements inside the form. The echo prints out the value by key: 'my_html_input_tag'. If the key is found, which it is, its value is returned: "PILLS HERE".
Then print_r prints out all the keys and values from $_POST so you can peek as to what else is in there.
The value of the input tag with name=my_html_input_tag
was put into the $_POST and you retrieved it inside another PHP file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With