1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help to select pre-selected value for a drop down menu

Discussion in 'Programming' started by toraniz, Feb 18, 2017.

  1. #1
    Can someone help with the below code how to pre-select as NO from a drop down menu :

    <select name="article" id="article" onchange="set_article();">
                    <option value="">Select...</option>
                    <option value="yes"<?php if($row_article["group_name_article"]=="yes"){?>selected="selected"<?php }?>>Yes</option>
          
                    <option value="no"<?php if($row_article["group_name_article"]=="no"){?>selected="selected"<?php }?>>No</option>
          
                    </select>
    Code (markup):
    Thanks in Advance.
     
    Last edited by a moderator: Feb 18, 2017
    toraniz, Feb 18, 2017 IP
  2. mrdo

    mrdo Well-Known Member

    Messages:
    1,552
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    138
    #2
    <select name="article" id="article" onchange="set_article();">
    <option value="">Select...</option>
    <option value="yes">Yes</option>
    <option value="no" selected>No</option>

    </select>
     
    mrdo, Feb 18, 2017 IP
  3. toraniz

    toraniz Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Thanks for your comment,but it did not work as I have other php value included in the code .
     
    toraniz, Feb 18, 2017 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    that makes no sense. Can you be more explicit?

    Check this: https://jsfiddle.net/aywa9jwt/
     
    sarahk, Feb 18, 2017 IP
  5. toraniz

    toraniz Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Sorry ,I am not coder but when I try to save it then the drop down does not get saved into database which is NO in this case .

    I may need to show all codes for better view as below:

    Sorry ,I am not coder but when I try to save it then the drop down does not get saved into database which is NO in this case .
    I may need to show all codes for better view as below:

    <td width="271" align="right" bgcolor="#CCCCCC" class="login_input"><b>Do you Want to Show Article :</b>&nbsp;</td>
         
                <td width="469" colspan="2" bgcolor="#CCCCCC" class="login_input">
         
                <?php
         
                $sq_article=mysql_query("select * from campaign where campaign_id='".$_REQUEST["campaign_id"]."'");
         
                $row_article=mysql_fetch_array($sq_article);
         
                ?>
         
                    <select name="article" id="article" onchange="set_article();">
         
                    <option value="">Select...</option>
         
                    <option value="yes"<?php if($row_article["group_name_article"]=="yes"){?>selected="selected"<?php }?>>Yes</option>
         
                    <option value="no"<?php if($row_article["group_name_article"]=="no"){?>selected="selected"<?php }?>>No</option>
         
                    </select>
         
                </td>
         
             </tr>
         
             <tr>
         
                <td colspan="3" bgcolor="#49A0CD">
         
                    <div id="article_container" style="display:<?php if($row_article["group_name_article"]=="yes"){?>block;<?php }else{?>none;<?php }?>">
         
                     <table width="744">
         
                         <tr>
         
                            <td width="267" align=""><b>Please Select Your Article Group:&nbsp;</b></td>
         
                            <td width="465">
         
                             <select name="article_tracking" id="article_tracking">
         
                                <option value="">Select...</option>
         
                             <?php
         
                             $flag1=0;
         
                                $sq_ar=mysql_query("select * from article where username='".$_SESSION["username_session"]."' group by group_id");
         
                                while($sq_row=mysql_fetch_array($sq_ar))
         
                                {
         
                                    if($sq_row["article_group_name"]!="")
         
                                    {
         
                                        ?>
         
                                        <option value="<?php echo $sq_row["group_id"];?>"<?php if($row2["article_refer"]==$sq_row["group_id"]){$flag1=1;?>selected="selected"<?php }?>><?php echo $sq_row["article_group_name"];?></option>
         
                                        <?php
         
                                    }
         
                                }
         
                             ?>
         
                             </select>
    Code (markup):
     
    Last edited by a moderator: Feb 19, 2017
    toraniz, Feb 19, 2017 IP
  6. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #6
    I think you are missing a space there:
    It is printing as:
    <option value="yes"selected="selected">Yes</option>

    OR

    row_article["group_name_article"] << on database, what column has "yes" "no" stored? The column name there: "group_name_article" doesn't sound to store "yes/no". Looks like you just copied some code from other line.
     
    Tanya Roberts, Feb 19, 2017 IP
  7. toraniz

    toraniz Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    Thanks for comment.
    It does not store yes or no in database column as I do not see there.
    I have put the code with no space here because I wanted to show the actual code .
    with the new code ;It is showing NO as pre-selected ,but after saving and return back to edit mode ,it is showing as YES for the selection.

    I have put the the given code in the file as below with space but no success:
    <option value="no" selected<?php if($row_article["group_name_article"]=="no"){?>selected="selected"<?php }?>>No</option>

    Is the above correct input?
     
    Last edited: Feb 19, 2017
    toraniz, Feb 19, 2017 IP
  8. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #8
    If you see this part, this is a column fetched from database and if the value from database it "no" then the select option is selected.
    But as you stated here, you don't store yes or no, what do you store here?
     
    Tanya Roberts, Feb 20, 2017 IP
  9. toraniz

    toraniz Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #9
    Thanks for your comment.
    I was trying to say that I do not see any store from database table when selecting YES or NO .
    I am storing either of Yes or No from drop down.
    There is no any selection by default from database.
    The pre-selected option from menu is Select...
    Sorry if my description seems nonsense ,I have no clue how this is working.
    Thanks
     
    toraniz, Feb 20, 2017 IP
  10. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #10
    
    <select name="article" id="article" onchange="set_article();">
    <option value="">Select...</option>
    <option value="yes"<?php if($row_article["group_name_article"]=="yes"){?>selected="selected"<?php }?>>Yes</option>
    
    <option value="no"<?php if($row_article["group_name_article"]=="no"){?>selected="selected"<?php }?>>No</option>
    
    </select>
    
    Code (markup):
    Here if this is the 1st article, it will show "Select..." on the dropdown as selected.
    If once it is saved, where is it saved? Like you do is set_article() which probably calls to some AJAX request and store it to database.

    And once that is stored, it needs to remember one of the option. right?[This part is troubling you]. Need few more details to make it clear. Column Names on the table(The column that stores "yes" or "no"). Or the code where set_article() is called that calls the ajax
     
    Tanya Roberts, Feb 21, 2017 IP
  11. toraniz

    toraniz Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #11
    My apologize ,actually yes you are right.
    I was mistakenly look at different database table.but now I found out the place where it is saving.

    It seems the option for Yes and No are stored as Yes or No in a column called group_name_article as it is showing in the code above I sent earlier.

    Below is the code which I have :

    Thanks
     
    toraniz, Feb 21, 2017 IP
  12. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #12
    I'm not even gonna start on how bad it is to store a boolean value in a database as "yes" or "no" (or "Yes" / "No" or any combination thereof...)
     
    PoPSiCLe, Feb 21, 2017 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #13
    Much less if it's only a yes/no, why not use a checkbox which only has yes/no?

    If it was actually stored in the DB as boolean all you'd need instead of the select is:
    
    echo '
      <input type="checkbox" name="article" id="article"', $row_article["group_name_article"] ? ' checked' : '', '>';
    
    Code (markup):
    Instead of wasting a select and multiple options on it.

    Also, even if you were to use the select, excellent poster child for why it's stupid to do <?php more than once in a file.
     
    deathshadow, Feb 23, 2017 IP
  14. yedort

    yedort Member

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    33
    #14
    You should seperate the attributes. That's what's making it fail.

    <option value="yes"<?php if($row_article["group_name_article"]=="yes"){?> selected="selected"<?php }?>>Yes</option>
    <option value="no"<?php if($row_article["group_name_article"]=="no"){?> selected="selected"<?php }?>>No</option>
     
    yedort, Mar 9, 2017 IP