-1

I'm relatively new to html and php. I'm trying to use values chosen in an html form in php.

I've found lots of examples, how to read out the html form values in php when there is only one form name like vehicle[].
But I would like to differentiate between types of input (vehicle or accommodation) and still have only one submit button. I've tried the following example, but it doesn't work ...

The html snippet from test.html:

<form action="form.php" method="POST">
  <input type="checkbox" name="vehicle[]" value="Bike"> I have a bike<br>
  <input type="checkbox" name="vehicle[]" value="Car" checked> I have a car<br>
  <input type="checkbox" name="accomodation[]" value="House"> I have a house<br>
  <input type="checkbox" name="accomodation[]" value="Yurt"> I have a yurt<br>
  <input type="submit" value="Submit">
</form>

The php snippet from form.php:

<?php
  if(isset($_POST['submit']))
  {
    if(!empty($_POST['vehicle'])) 
    {
      foreach($_POST['vehicle'] as $vehiclecheck)
      {
        echo $vehiclecheck;
        echo "<br>";
      } 
    }
    if(!empty($_POST['accomodation'])) 
    {
      foreach($_POST['accomodation'] as $accomodationcheck)
      {
        echo $accomodationcheck;
        echo "<br>";
      } 
    }

  }
?>

Is there a simple way to get what I want? Or do I need a workaround?

Thanks!

Senjuti Mahapatra
  • 2,570
  • 4
  • 27
  • 38
  • 1
    I'm not sure I understand the question, what doesn't work? – Epodax Dec 17 '15 at 10:37
  • Never name anything "submit" in a form. It will block JavaScript submission. Give the input a name and test it: `` `if(isset($_POST['subbut']))` – mplungjan Dec 17 '15 at 10:41

2 Answers2

2
 <input type="submit" value="Submit" name="submit">  //name attribute missing here
Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15
2

Try this one. For sake of simplicity, it's just one file form.php

<?php
if (!empty($_POST)) {
    if(!empty($_POST['vehicle'])) {
        foreach($_POST['vehicle'] as $vehiclecheck) {
            var_dump($vehiclecheck);
        } 
    }
    if (!empty($_POST['accomodation'])) {
        foreach($_POST['accomodation'] as $accomodationcheck) {
            var_dump($accomodationcheck);
        }
    }
}
?>
<form action="form.php" method="POST">
    <input type="checkbox" name="vehicle[]" value="Bike"> I have a bike<br>
    <input type="checkbox" name="vehicle[]" value="Car" checked> I have a car<br>
    <input type="checkbox" name="accomodation[]" value="House"> I have a house<br>
    <input type="checkbox" name="accomodation[]" value="Yurt"> I have a yurt<br>
    <input type="submit" value="Submit">
</form>

Few notes:

1.) Try following coding style guides, for PHP it's PSR-2 http://www.php-fig.org/psr/psr-2/.

2.) In your example you tried to check whether post field called 'submit' is not empty. In fact, this field was not present and was always empty)

3.) Using var_dump is better way of simplistic debugging compared to echo, because it could be better set up, shows variable types, you don't need to write a new line explicitly etc.

4.) There's aslo a great source of how to get better in PHP, try it http://www.phptherightway.com/