How to Get a PHP Value from an HTML Form

How to get input field value using PHP

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

Get value from HTML input form with PHP

You need to assign a name to the input element. In your situation, you could use the same name as your id:

<input id='bet' name='bet' type='text' value='100' />

To get the specific data for the 'bet' input field use:

echo $_POST['bet'];

On your server to view all of the post data use the code:

// Wrapping the output in the pre block makes the POST data easier to read
echo '<pre>';
print_r($_POST);
echo '</pre>';

PHP can't retrieve value from HTML form

Thank you for helping me. I have think of an solution that uses ajax again to retrieve these updated value and send it to test.php.

UpdateProfile.php

function submitEditProfileForm(){
$.ajax({
type:"POST",
url: "test.php",
data: {
profileCode:$('#profileCode').val(),
profileName:$('#profileName').val(),
profileDesc:$('#profileDesc').val()
},
success: function(data){
alert(data);
}
});
}

test.php

if(isset($_POST['profileCode']) && isset($_POST['profileName']) && isset($_POST['profileDesc'])){
$profileCode= $_POST['profileCode'];
$profileName= $_POST['profileName'];
$profileDesc= $_POST['profileDesc'];
echo $profileCode.",".$profileName.",".$profileDesc;

Obtain Date value from HTML form and pass it in PHP

Change

<form action="insert.php">

to

<form action="insert.php" method="post">

The default HTTP method of a form is GET. You must specify POST since you are accessing it using $_POST. Then you should be able to get the value of the Data field in $Data.

If it doesn't fix the errors, try the following:
1 Check the datatype of Data column in your MySQL table, and make sure that your $Data is in correct format.

For example: If your Data column is a DATE column, $Data should be in YYYY-mm-dd format.

Other Date - Time types and format is given here:

Date and Time Types                          Description
-----------------------------------------------------------------
DATE A date value in CCYY-MM-DD format
-----------------------------------------------------------------
TIME A time value in hh:mm:ss format
-----------------------------------------------------------------
DATETIME A date and time value inCCYY-MM-DD hh:mm:ss format
-----------------------------------------------------------------
TIMESTAMP A timestamp value in CCYY-MM-DD hh:mm:ss format
-----------------------------------------------------------------
YEAR A year value in CCYY or YY format
-----------------------------------------------------------------

Extra Read: Methods GET and POST in HTML forms - what's the difference?http://jkorpela.fi/forms/methods.html

Make a PHP variable the value of a form Input

Try this:

?><!-- exit out of php into html-->
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>

<!--Here is where I need to make my PHP variable the value:-->
<input type = "text" name = "referer" style = "display: none" value = "<?=$variable?>">

<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
<?php // enter back into php

The <?= ?> is a php short tag


Also, if you still want to use echo, try this:

//note: I changed the quotes
echo "
<div id = 'login'>
<form action = 'process.php' method = 'POST'>
Name: <input type = 'text' name = 'name' required>

<input type = 'text' name = 'referer' style = 'display: none' value = '$variable'>

<input type = 'submit' name = 'submit' value = 'Enter'>
</form>
</div>
";

See this Q/A for more info

How to get value from HTML form button in php and then writing to php text

First, you can't process a PHP code inside html. So save for file as .php and leave your action attribute blank since you want to process the POST request within the same page. And a type of button for your input element will not going to submit your form. Changed it to submit

<form name="calculator" method="post" action="">
<input type="submit" name="one" value="someString">

Second, PHP needs to know or should have a reference of what specific input field you're trying to get value from. So the name attribute will be the reference. But you MUST know the exact name you're trying to pull or it will throw you an undefined index error, PHP will not know that for you.

$string = ""; //declare your variable as global to be used later
if(!empty($_POST)){
$string = $_POST['one']; //assuming you know that you are getting the value of input field `name=one`
}

From there, you can now use the variable string to display in your HTML input/textarea.

<textarea><?php echo $string; ?></textarea>
<input type="text" value="<?php echo $string;?>">

Note:

This is for PHP only, this kind of scenario is mostly done via javascript, but it depends on your preference or purpose though

How to send the PHP value back to the HTML form?

You should call setcookie() before you output any HTML, so move the PHP code above the rest.

To set the form input:

<?php 
if (isset($result)) {
$bill = $result;
} else {
$bill = '';
}
?>
Total Bill Value: <input type="text" name="Bill" value="<?php echo $bill; ?>">


Related Topics



Leave a reply



Submit