2

I have simple PHP form that utilises selec2, but the data selected are never transmited by the POST method. Anyone know the problem? First script

<head>

  <!-- stylesheets -->
  <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">

  <style type="text/css">
  body {
    padding: 40px;
  }
  </style>

  <!-- scripts -->
  <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
  <link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>

  <script>
    $(function(){
      // turn the element to select2 select style
      $('#pbr').select2();
    });
  </script>
</head>

<body>


  <form  action="trazi2.php" method="POST">
  <p>select2 select box:</p>
  <p>
    <select id="pbr" style="width:300px">
      <optgroup label="Alaskan/Hawaiian Time Zone">
        <option value="AK">Alaska</option>
        <option value="HI">Hawaii</option>
      </optgroup>
      <optgroup label="Pacific Time Zone">
        <option value="CA">California</option>
        <option value="NV">Nevada</option>
        <option value="OR">Oregon</option>
        <option value="WA">Washington</option>
      </optgroup>

    </select>
  </p>
  <input type="submit" name="Trazi" value="Treazi" class="btn btn-default btn-primary" onclick="return validate()">
</body>

</html>

Script being triggered by the Submit button trazi2.php

<?

$pbr = $_POST["pbr"];
print $pbr;
?>

Nothing gets passed, anyone knows the problem?

Jure
  • 49
  • 3
  • 7

1 Answers1

3

You need to add name attribute to <select>

When you post a form, they are posted as name attribute being key and value being value of $_POST.

So, any field who does not have name attribute will not be posted.

Corrected code:

<select id="pbr" style="width:300px" name="pbr">
Pupil
  • 23,834
  • 6
  • 44
  • 66