Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:26:52+00:00 2026-06-15T14:26:52+00:00

I was following a tutorial on PHP and HTML on how to use option=selected,

  • 0

I was following a tutorial on PHP and HTML on how to use option=selected, but I am confused.

Here is my code:

<form name ="rating" method ="POST" action ="" /> 
<? echo "<input type = \"hidden\" name = accountID value = " . $row['accountID'] . ">";?>
<select name="rating">
    <? $values = array('','Very Bad','Bad','Average','Above Average');

    for ($i =0; $i < count($values); $i = $i + 1){
        echo "<option value = \"$i\"" . if ($rating == $i + 1) {echo "selected";} . "> $values[$i] </option>"; 
    }    
    ?>  
</select>
<input type ="Submit" name ="formSubmit" value ="Submit">
</form>    
<?

What I am trying to achieve is to have whatever option is stored in the database to be selected by default. What am I missing in the code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:26:53+00:00Added an answer on June 15, 2026 at 2:26 pm

    try this out it will be what u want

      <form name ="rating" method ="POST" action ="" > <?php
    
    echo "<input type = 'hidden' name = 'accountID' value = '" . $row['accountID'] . "' >";
    
    ?>
        <select name="rating">
    
            <? $values = array('','Very Bad','Bad','Average','Above Average');
    
            for ($i =0; $i < count($values); $i = $i + 1){ ?>
                <option value = "$i"  <?php if ($rating == $i + 1) {echo " selected='selected'" ;} ?> > $values[$i] </option>
              <?php 
            }
    
            ?>
    
            </select>
            <input type ="Submit" name ="formSubmit" value ="Submit" />
            </form>
    
        <?
    

    EDIT:

    yes this variable $rating is not defined . so try to define it like that

      if (isset $POST['rating']) {$rating = $POST['rating'];}else {$rating = 0 ;}
    

    put it before this form somewhere inside php tag

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following their tutorial here: http://developers.sones.de/wiki/doku.php?id=quickreference:fiveminuteguide But when I copy and paste this
I've been making a webpage with php and html following this tutorial . I
I was following this tutorial . I need to use a php file's ouput
i have made a simple php contact form following this tutorial: http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme The big
I use PHP to generate dynamic Web pages. As stated on the following tutorial
I'm following a tutorial on Zend Framework here (http://www.ibm.com/developerworks/opensource/tutorials/os-php-zend2/section2.html). I barely got started, but
I'm following this tutorial here: http://brenelz.com/blog/implementing-paging-using-php-and-jquery/ Here is what I have: http://eataustineat.com/testingfolder/ I've encountered
I'm currently following a PHP tutorial, and I've got the following code:: $sql =
I was following a tutorial to understand how AJAX/PHP works but i'm having an
I am following this tutorial step by step http://code.google.com/apis/maps/articles/phpsqlajax.html . Actually I copy pasted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.