-1

I am getting this error when loading the webpage:

Notice: Undefined index: bandselection in /public/sites/www.transport-dealer.com/commercialequipmentfinder/index.php on line 203

Notice: Undefined index: bandselection2 in /public/sites/www.transport-dealer.com/commercialequipmentfinder/index.php on line 204

When i press submit it will show the correct data, is there anyway to make sure the data in the "seleccted"part of the form is already been filtered and shown

HTML:

 <form method="post" action="index.php">
   <label>
     <select name="bandselection" id="bandselection">
       <option value="1990" selected="selected">1990</option>
       <option value="2006">2006</option>
       <option value="2005">2005</option>
       <option value="2004">2004</option>
     </select>
     <select name="bandselection2" id="bandselection2">
       <option value="2015" selected="selected">2015</option>
       <option value="2006">2006</option>
       <option value="2005">2005</option>
       <option value="2004">2004</option>
     </select>
  </label>
  <label>
    <input type="submit" name="Submit" value="Submit" />  
  </label>
</form>

PHP:

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$bandselection = $_POST['bandselection'];
$bandselection2 = $_POST['bandselection2'];
$sql = "SELECT ID, link, link_image, modelyear, brand, city, price, description, country, type FROM semi_trailer_curtainsider  WHERE modelyear BETWEEN  '" . $bandselection ."' AND '" . $bandselection2 ."' AND brand = 'Samro'";
Community
  • 1
  • 1
Bas Mienis
  • 99
  • 1
  • 7
  • 1
    On initial page load your `$_POST` variables are not set, which will cause the issue. Wrap that section in a `if(isset($_POST['bandselection'])){ ... }` to prevent the warnings. – Sean Oct 28 '15 at 22:41
  • Hi, many thanks :) this really helped and sorry that i was not able to find a other post that was the same. i did not know how to phrase the question correct – Bas Mienis Oct 29 '15 at 05:13

1 Answers1

0

That's because the superglobal $_POST is not populated yet when you load the page (because it is an HTTP GET request), you should process the form data in another PHP file by setting the action attribute on the <form> tag like this:

<form action="other_page.pho">

And/Or by checking the $_POST array is populated before processing it:

if(isset($_POST['my_data'])){
      //process form
}
n00dl3
  • 21,213
  • 7
  • 66
  • 76