2

I am just tottaly newbie with PHP and I am now learning how to combine PHP with HTML form. I wanted it in one file, so I tried this:

<?php    
if(isset($_POST['button'])){ //check if form was submitted
  $pohlavie = $_POST['gender']; //get input text
  $plat = $_POST['salary']; //get input text
  $plat = "Your gender is ".$pohlavie." and your salary is ".$plat;
}    
?>

<center><h1>TAXES</h1></center>
<form action="" method="post">
Name: <input type="text" name="name"><br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="male"> Male<br>
Salary: <input type="number" name="salary"><br>
<button type="submit" name="button" formmethod="post">Calculate DPH</button>
</form>

Unfortunately, it's literally doing nothing after submitting. Can you please help me a little bit?

Roman Bartík
  • 67
  • 1
  • 2
  • 7

4 Answers4

1
echo $plat = "Your gender is ".$pohlavie." and your salary is ".$plat;
D. Pachauri
  • 244
  • 2
  • 8
1

Trying the using the following line in place of your current form line:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">`
  • 2
    The action to `PHP_SELF` is not needed as the submit will send a HTTP POST request to the current destination. This doesn't fix OPs "problem" as they forgot to `echo` the result. – ʰᵈˑ Apr 11 '17 at 10:50
1

here is:

 <?php    
 if(isset($_POST['button'])){ //check if form was submitted
 $pohlavie = $_POST['gender']; //get input text
 $plat = $_POST['salary']; //get input text
 echo "Your gender is ".$pohlavie." and your salary is ".$plat;
 }    
 ?>

<center><h1>TAXES</h1></center>
<form action="" method="post">
Name: <input type="text" name="name"><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="male"> Male<br>
Salary: <input type="number" name="salary"><br>
<button type="submit" name="button" formmethod="post">Calculate DPH</button>
</form>
Therichpost
  • 1,759
  • 2
  • 14
  • 19
0

Always follow best coding practices and use recommended features. Here the code with little modifications:

<?php
if (filter_has_var(INPUT_POST, "button")) { //check if form was submitted
  $pohlavie = filter_input(INPUT_POST, 'gender'); //get input text
  $salary = filter_input(INPUT_POST, 'salary'); 
  echo $plat = "Your gender is ".$pohlavie." and your salary is ".$salary;
}    
?>

<center><h1>TAXES</h1></center>
<form action="" method="post">
Name: <input type="text" name="name"><br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="male"> Male<br>
Salary: <input type="number" name="salary"><br>
<button type="submit" name="button" formmethod="post">Calculate DPH</button>
</form>

You can also use different type of sanitize filters based on your needs. See here: http://php.net/manual/en/filter.filters.sanitize.php

Also see this post to get more knowledge regarding filter_input: When to use filter_input()

Hope it will help you to learn and follow best practices in PHP.

Community
  • 1
  • 1
Mubashar Iqbal
  • 398
  • 7
  • 18