How to Get the Values of Input 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 to get the values of input using PHP

By using this name="login[password]" you can get the values in PHP as:

print($_POST['login']['password']);

One more solution, store input array in a variable like:

$post = $_POST['login'];

then, use like:

echo $post['password']

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

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 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.

get value of input element with id or name

Id and Classes are mainly for CSS or JavaScript purpose.Use name for getting the post values $_POST['firstname'].

<input name="firstname" type="text" id="first_name">

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



Related Topics



Leave a reply



Submit