How to pre select an option from dropdown menu in PHP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm working on a WordPress theme for a listing directory, and I'm trying to pre select an option from a dropdown menu so that when someone opens that page they will get the listings from that option right away.



I tried with jQuery but it didn't work so I'm stuck here right now. Any help is welcome.



Image: https://imgur.com/a/G0Rtny3



EDIT: I found a fix, I made "Producer" the only option, and changed the if statement from !empty($term_ID) to empty($term_ID).



It might not be the best solution but it works for me. Thanks everyone.



<select data-placeholder="<?php echo esc_html__('Select Category','dwt- 
listing');?>" name="l_category" class="allow_clear" id="l_category">

<option value=""><?php echo esc_html__('Select an
option','dwt-listing'); ?>
</option>

<option value="all"><?php echo esc_html__('All Categories','dwt-
listing'); ?>
<?php

//selective
if( isset( $_GET['l_category'] ) && $_GET['l_category'] != "" )
{
$term_ID = $_GET['l_category'];
}
foreach( $listing_cats as $cats )
{
?>
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
<?php
}
?>
</select>









share|improve this question

























  • Does this code set the correct option as selected in the dropdown?

    – RiggsFolly
    Jan 3 at 10:03













  • This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.

    – lordotG
    Jan 3 at 10:47


















0















I'm working on a WordPress theme for a listing directory, and I'm trying to pre select an option from a dropdown menu so that when someone opens that page they will get the listings from that option right away.



I tried with jQuery but it didn't work so I'm stuck here right now. Any help is welcome.



Image: https://imgur.com/a/G0Rtny3



EDIT: I found a fix, I made "Producer" the only option, and changed the if statement from !empty($term_ID) to empty($term_ID).



It might not be the best solution but it works for me. Thanks everyone.



<select data-placeholder="<?php echo esc_html__('Select Category','dwt- 
listing');?>" name="l_category" class="allow_clear" id="l_category">

<option value=""><?php echo esc_html__('Select an
option','dwt-listing'); ?>
</option>

<option value="all"><?php echo esc_html__('All Categories','dwt-
listing'); ?>
<?php

//selective
if( isset( $_GET['l_category'] ) && $_GET['l_category'] != "" )
{
$term_ID = $_GET['l_category'];
}
foreach( $listing_cats as $cats )
{
?>
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
<?php
}
?>
</select>









share|improve this question

























  • Does this code set the correct option as selected in the dropdown?

    – RiggsFolly
    Jan 3 at 10:03













  • This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.

    – lordotG
    Jan 3 at 10:47














0












0








0








I'm working on a WordPress theme for a listing directory, and I'm trying to pre select an option from a dropdown menu so that when someone opens that page they will get the listings from that option right away.



I tried with jQuery but it didn't work so I'm stuck here right now. Any help is welcome.



Image: https://imgur.com/a/G0Rtny3



EDIT: I found a fix, I made "Producer" the only option, and changed the if statement from !empty($term_ID) to empty($term_ID).



It might not be the best solution but it works for me. Thanks everyone.



<select data-placeholder="<?php echo esc_html__('Select Category','dwt- 
listing');?>" name="l_category" class="allow_clear" id="l_category">

<option value=""><?php echo esc_html__('Select an
option','dwt-listing'); ?>
</option>

<option value="all"><?php echo esc_html__('All Categories','dwt-
listing'); ?>
<?php

//selective
if( isset( $_GET['l_category'] ) && $_GET['l_category'] != "" )
{
$term_ID = $_GET['l_category'];
}
foreach( $listing_cats as $cats )
{
?>
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
<?php
}
?>
</select>









share|improve this question
















I'm working on a WordPress theme for a listing directory, and I'm trying to pre select an option from a dropdown menu so that when someone opens that page they will get the listings from that option right away.



I tried with jQuery but it didn't work so I'm stuck here right now. Any help is welcome.



Image: https://imgur.com/a/G0Rtny3



EDIT: I found a fix, I made "Producer" the only option, and changed the if statement from !empty($term_ID) to empty($term_ID).



It might not be the best solution but it works for me. Thanks everyone.



<select data-placeholder="<?php echo esc_html__('Select Category','dwt- 
listing');?>" name="l_category" class="allow_clear" id="l_category">

<option value=""><?php echo esc_html__('Select an
option','dwt-listing'); ?>
</option>

<option value="all"><?php echo esc_html__('All Categories','dwt-
listing'); ?>
<?php

//selective
if( isset( $_GET['l_category'] ) && $_GET['l_category'] != "" )
{
$term_ID = $_GET['l_category'];
}
foreach( $listing_cats as $cats )
{
?>
<option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>
<?php
}
?>
</select>






php wordpress directory-listing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 13:27







lordotG

















asked Jan 3 at 10:00









lordotGlordotG

11




11













  • Does this code set the correct option as selected in the dropdown?

    – RiggsFolly
    Jan 3 at 10:03













  • This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.

    – lordotG
    Jan 3 at 10:47



















  • Does this code set the correct option as selected in the dropdown?

    – RiggsFolly
    Jan 3 at 10:03













  • This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.

    – lordotG
    Jan 3 at 10:47

















Does this code set the correct option as selected in the dropdown?

– RiggsFolly
Jan 3 at 10:03







Does this code set the correct option as selected in the dropdown?

– RiggsFolly
Jan 3 at 10:03















This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.

– lordotG
Jan 3 at 10:47





This code is for the default placeholder "Select Category". I'm trying to set the "Producer" option as default.

– lordotG
Jan 3 at 10:47












2 Answers
2






active

oldest

votes


















0














This example could help



var select = document.getElementById('countryselect');
var option;

for (var i=0, i<select.options.length; i<iLen; i++) {
option = select.options[i];

if (option.value == '4') {
// or
// if (option.text = 'Malaysia') {
option.setAttribute('selected', true);

// For a single select, the job's done
return;
}
}


Here is the link
How to add "selected" in option attribute using Javascript or jQuery?






share|improve this answer































    0














    I have made a little change in your code.
    check this and first in sure that you have got $term_ID or not because your selected option depend on this variable.

    Replace this:-

    <option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>

    With this:-

    <option value="<?php echo esc_attr( $cats->term_id ); ?>" <?php if ($cats->term_id==$term_ID) { echo "selected='selected'"; } ?> ><?php echo esc_html( $cats->name ); ?></option>





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019975%2fhow-to-pre-select-an-option-from-dropdown-menu-in-php%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      This example could help



      var select = document.getElementById('countryselect');
      var option;

      for (var i=0, i<select.options.length; i<iLen; i++) {
      option = select.options[i];

      if (option.value == '4') {
      // or
      // if (option.text = 'Malaysia') {
      option.setAttribute('selected', true);

      // For a single select, the job's done
      return;
      }
      }


      Here is the link
      How to add "selected" in option attribute using Javascript or jQuery?






      share|improve this answer




























        0














        This example could help



        var select = document.getElementById('countryselect');
        var option;

        for (var i=0, i<select.options.length; i<iLen; i++) {
        option = select.options[i];

        if (option.value == '4') {
        // or
        // if (option.text = 'Malaysia') {
        option.setAttribute('selected', true);

        // For a single select, the job's done
        return;
        }
        }


        Here is the link
        How to add "selected" in option attribute using Javascript or jQuery?






        share|improve this answer


























          0












          0








          0







          This example could help



          var select = document.getElementById('countryselect');
          var option;

          for (var i=0, i<select.options.length; i<iLen; i++) {
          option = select.options[i];

          if (option.value == '4') {
          // or
          // if (option.text = 'Malaysia') {
          option.setAttribute('selected', true);

          // For a single select, the job's done
          return;
          }
          }


          Here is the link
          How to add "selected" in option attribute using Javascript or jQuery?






          share|improve this answer













          This example could help



          var select = document.getElementById('countryselect');
          var option;

          for (var i=0, i<select.options.length; i<iLen; i++) {
          option = select.options[i];

          if (option.value == '4') {
          // or
          // if (option.text = 'Malaysia') {
          option.setAttribute('selected', true);

          // For a single select, the job's done
          return;
          }
          }


          Here is the link
          How to add "selected" in option attribute using Javascript or jQuery?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 10:08









          TDKTDK

          961720




          961720

























              0














              I have made a little change in your code.
              check this and first in sure that you have got $term_ID or not because your selected option depend on this variable.

              Replace this:-

              <option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>

              With this:-

              <option value="<?php echo esc_attr( $cats->term_id ); ?>" <?php if ($cats->term_id==$term_ID) { echo "selected='selected'"; } ?> ><?php echo esc_html( $cats->name ); ?></option>





              share|improve this answer




























                0














                I have made a little change in your code.
                check this and first in sure that you have got $term_ID or not because your selected option depend on this variable.

                Replace this:-

                <option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>

                With this:-

                <option value="<?php echo esc_attr( $cats->term_id ); ?>" <?php if ($cats->term_id==$term_ID) { echo "selected='selected'"; } ?> ><?php echo esc_html( $cats->name ); ?></option>





                share|improve this answer


























                  0












                  0








                  0







                  I have made a little change in your code.
                  check this and first in sure that you have got $term_ID or not because your selected option depend on this variable.

                  Replace this:-

                  <option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>

                  With this:-

                  <option value="<?php echo esc_attr( $cats->term_id ); ?>" <?php if ($cats->term_id==$term_ID) { echo "selected='selected'"; } ?> ><?php echo esc_html( $cats->name ); ?></option>





                  share|improve this answer













                  I have made a little change in your code.
                  check this and first in sure that you have got $term_ID or not because your selected option depend on this variable.

                  Replace this:-

                  <option <?php if ($cats->term_id == $term_ID) { ?>selected="selected"<?php } ?> value="<?php echo esc_attr( $cats->term_id ); ?>"><?php echo esc_html( $cats->name ); ?></option>

                  With this:-

                  <option value="<?php echo esc_attr( $cats->term_id ); ?>" <?php if ($cats->term_id==$term_ID) { echo "selected='selected'"; } ?> ><?php echo esc_html( $cats->name ); ?></option>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 10:58









                  F5 BuddyF5 Buddy

                  3584




                  3584






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019975%2fhow-to-pre-select-an-option-from-dropdown-menu-in-php%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith