Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get input field value using PHP

Tags:

php

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?

like image 759
nasty Avatar asked Nov 19 '12 04:11

nasty


People also ask

How can get input field value in php variable?

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.

How can I take input from HTML to php?

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']; ?>

How can get HTML element value in php?

js"></script> </head> <body> <script type="text/javascript"> $. post('test. php',{'data': "value1"}); </script> <? php $data = $_POST['data']; print( "data is: $data" ); ?>


2 Answers

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']; ?> 
like image 55
Jeremy John Avatar answered Oct 01 '22 05:10

Jeremy John


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.

like image 34
Eric Leschinski Avatar answered Oct 01 '22 05:10

Eric Leschinski