-2

My problem is when an option selected it works fine but when it is not selected it shows error "Undefined index: keyword"

  1. How can i select an default value for selected option ?

  2. How to not show error even if no option is selected ? just insert empty in database

or

simply how to prevent that error if nothing is selected ?

<select class="selectpicker btn btn-warning "  name="keyword"  style="font-size:17;">
      <option disabled="disabled" selected="selected" style="display:none; ">SELECT TOPIC</option>


      <option  >Anime</option>
      <option >WTF</option>
      <option >Did you know</option>

  </select>


<?php if(isset($_POST['sub'])){


$post_keywords = $_POST['keyword'];


}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • If I post an answer, will that one also not be accepted as correct? Please start accepting answers that will mark the question(s) as solved. That informs Stack/members that no further answers are needed. – Funk Forty Niner Jan 02 '17 at 12:53
  • The answer to this is simpler than you think. – Funk Forty Niner Jan 02 '17 at 12:53
  • wow fred you are worried about getting accepted before answering . good you are so helpful . tho i can see your only job here is to down vote and mark as duplicate . we are not here to find out if it is new question or not . we are here to find some help but we can not unless people like you exist – Jannatul Ferdous Jan 02 '17 at 16:56
  • With your track record; put yourself in my shoes. And yes, it is a duplicate. – Funk Forty Niner Jan 02 '17 at 17:06
  • lol u r not even qualified to come under my shoes ...hahaah – Jannatul Ferdous Jan 03 '17 at 04:29

2 Answers2

1

You can try this code.

<select class="selectpicker btn btn-warning "  name="keyword"  style="font-size:17;">                 
      <option >Anime</option>
      <option >WTF</option>
      <option >Did you know</option>      
  </select>
<?php if(!empty($_POST['keyword'])){
$post_keywords = $_POST['keyword'];
}
else{
$post_keywords = "";
}
Saleem Khan
  • 150
  • 1
  • 9
0

If you have select default value as selected option then remove disable attributes in first option tag.

For insert empty in databese.

<?php
 if(empty($_POST['keyword'])){
   $keywords = 'defult value';
 } else {
   $keywords = $_POST['keyword'];
 }
?>
Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34
  • ok it does insert value in database but i am still getting this error "Notice: Undefined index: keyword in C:\xampp\htdocs******* on line 217" where i have $post_keywords = $_POST['keyword']; – Jannatul Ferdous Jan 02 '17 at 14:47
  • If You have remove this error. Please remove disable attributes in first select option tag. – Pravin Vavadiya Jan 03 '17 at 04:42