I have a front end form and want to retrieve user input vale using $_POST
in a variable. I had found similar threads here, but none of it couldn't solve my problem.
After accepting the variable in $data
, I tried to print using echo $data
. Nothing is shown. (But when I use other form input such as drop down or check box, $data
contains data. I require the button type as mentioned here and wont be able to change).
HTML Form
<html>
<body>
<form action="test_general.php" method="post" >
<input type="submit" name="btn" value="but1">text1</button>
<button type="submit" name="btn" value="but2">text2</button>
</form>
</body>
<html>
PHP processing code
<html>
<?php
$data = $_POST['value'];
echo $data;
?>
<html>
You need to get the post value btn
. The name of the element is the key of the post value you want.
<?php
$data = $_POST['btn'];
echo $data;
?>
Your buttons have the same name "btn". Try changing one of them and then read it on postback as $_POST['btn']
Try it like this:
<html>
<body>
<form action="test_general.php" method="post" >
<input type="text" name="text" value="but1" placeholder="text1" />
<input type="submit" name="btn" value="but2">
</form>
</body>
</html>
This code will only show you what was filled in after clicking the submit button.
<html>
<?php
if(isset($_POST['btn']))
{
$data = $_POST['text'];
echo $data;
}
?>
</html>