How to Get Input Field Value Using PHP

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

How do I use get method to input values to form field with PHP

In your HTML, add the input field like this:

<input type="text" name="username" value="<?php echo htmlspecialchars($_GET['username']); ?>" />

Basically, the value attribute of the text field needs to be set to:

<?php echo $_GET['username']; ?>

The code right above this is how you would output a get variable in php whether you are putting it in a text field or not.
To access get variables, always use:

$_GET['variable_name'];

Then you can assign it to variables or pass it as a function parameter.

**However, I strongly do not recommend passing sensitive information like usernames and passwords through GET variables. **

First off, users could change the URL hence changing the variable. They could also accidentally share the URL with someone and that could give someone else access to their account. I would recommend that you create a cookie on their machine that is set to a random ID, and then in a MySQL database, associate that ID with a username so that you know the user can't accidentally share their account or change their username through the URL.

php get input field value and span value

It isn't possible to get the value of a span in your $_POST without changing it to some kind of input.

What you could do however is make a hidden input field:

<input type="hidden" value="some text">

which will not be shown to the user and will appear in your $_POST variable

Building URL from input value using PHP

in your main index.php file you will have this

<form method="GET"
class="p2"
action="submit-folder"
target="_top">
<div class="ampstart-input inline-block relative mb3">
<input type="search"
placeholder="Search..."
name="googlesearch">
</div>
<input type="submit"
value="OK"
class="ampstart-btn caps">
</form>

and inside a folder named "submit-folder" with another index.php file inside yo will have this

  <?php
if (isset($_GET["googlesearch"])) {
header("Location: https://www.siteb.com/#!q=" . $_GET["googlesearch"] . "?parameter1");
}

this is the file that will receive the request then automatically redirect you could have done this in a single file though as

<?php
if (isset($_GET["googlesearch"])) {
header("Location: https://www.siteb.com/#!q=" . $_GET["googlesearch"] . "?parameter1");
exit;
}
?>
<form method="GET"
class="p2"
action="<?php echo $_SERVER['PHP_SELF']; ?>"
target="_top">
<div class="ampstart-input inline-block relative mb3">
<input type="search"
placeholder="Search..."
name="googlesearch">
</div>
<input type="submit"
value="OK"
class="ampstart-btn caps">
</form>

How to get form input value?

Create a Form and add the fields inside it..
In action, attribute add the file name where you want to post the form..

Form.php

<form method="POST" action="submit.php">
<input name="url" value="https://testdomain.com/posts/6412">
<input type="submit" name="submit" value="Submit">
</form>

submit.php

<?php 
if(isset($_POST['submit'])){
$link = $_POST['url'];
}
?>

With AJAX

Form.php

 <form method="POST" id="form" action="submit.php">
<div id="result"></div>
<input name="url" value="https://testdomain.com/posts/6412">
<input type="submit" name="submit" value="Submit">
</form>

<script>
$("#form").submit(function(e) {
e.preventDefault();
$.ajax({
type: "post",
url: $(this).attr("action"),
data: $(this).serialize(),
success: function(response) {
$("#result").html(response);
}
});
});
</script>

submit.php

<?php 
$link = $_POST['url'];
echo $link;
?>

for ajax, you must have to add jquery library before ajax code

How to get a echoed input field value using Php

Here's how I've accomplished a similar task.

echo'<div class="form-group">
<input id="lastedited" name="lastedited" type="text" value="Value Goes Here" class="form-control input-md" readonly>

</div>
</div>';

I set the input as read only. Then on $_POST I can still $_GET the data I need and assign to a variable or manipulate as needed. You'll need to remember to query your database and assign it to a variable, this way you can include that in the form inputs value for when it gets submitted.

Using php to set value of html input field

your code looks correct, you just need to run it as PHP instead of HTML

Unable to set PHP variables as values for input field in form

I managed to figure it out!

DO_CUSTEDIT.php / PHP

$results = array($custID, $custName);
echo json_encode($results, JSON_PRETTY_PRINT);

EDITCUST.php / HTML

<input type="text" id="custID" name="custID" value="" readonly/>
<input type="text" id="custName" name="custName" value="" readonly/>

EDITCUST.php / JS

    <script>
$(document).ready(function() {
$("#subeditcust").on('click',function(e) {
e.preventDefault();
$.ajax( {
url: "lib/do_editcust.php",
method: "post",
data: $("form").serialize(),
dataType: 'json',
success: function(data){
document.getElementById('custID').value = data[0];
document.getElementById('custName').value = data[1];
}
});
});
});
</script>

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


Related Topics



Leave a reply



Submit