Roman Bartík
Roman Bartík

Reputation: 67

Form and PHP in one file

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?

Upvotes: 2

Views: 6005

Answers (4)

Mubashar Iqbal
Mubashar Iqbal

Reputation: 385

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.

Upvotes: 0

Therichpost
Therichpost

Reputation: 1815

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>

Upvotes: 1

Brijesh_yadav
Brijesh_yadav

Reputation: 75

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

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">`

Upvotes: 1

D. Pachauri
D. Pachauri

Reputation: 244

echo $plat = "Your gender is ".$pohlavie." and your salary is ".$plat;

Upvotes: 1

Related Questions