0

following is my part of html form. If the form is showing a error then i want to select the user selected field. But unfortunately it's not working for me. can you guys help me plz?

My html form:

<select class="td" name="class_name">
    <option value="">--Select class--</option>
    <?php
    $class = mysql_query("SELECT * FROM e_class");

    while($res =  mysql_fetch_array($class))
    {           
        $class_name_ed = $res['class_name'];
        $class_id_ed = $res['class_id'];
        ?>
        <option value="<?php echo "$class_name_ed"; ?>" <?php 
if(isset($_POST['class_name']) == "$class_name_ed") echo 'selected = "selected"';  ?>> 
<?php echo $class_name_ed; ?></option>";
        <?php
    }
    ?>
</select>

But if i look at the source file then i can see following.. It's selected all the field

<select class="td" name="class_name">
    <option value="">--Select class--</option>
    <option value="Three" selected = "selected"> Three</option>";
    <option value="Four" selected = "selected"> Four</option>";
    <option value="Five" selected = "selected"> Five</option>";
</select>

Update: If the option is look like then what can i do:

<option value="<?php echo "$class_name_ed | $class_id_ed"; ?>" <?php if 
(isset($_POST['class_name']) && ($_POST['class_name'] == $class_name_ed)) echo 
'selected = "selected"';  ?>> <?php echo $class_name_ed; ?></option>
1
  • how does your DB structure look? Commented Sep 6, 2013 at 16:18

2 Answers 2

2
if(isset($_POST['class_name']) == "$class_name_ed")
   ^^^^^^

you're comparing a boolean true/false value against a string. Most likely you want something more like:

if (isset($_POST['class_name']) && ($_POST['class_name'] == $class_name_ed)) {

instead.

Sign up to request clarification or add additional context in comments.

2 Comments

And also change <?php echo $class_name_ed; ?></option>"; to <?php echo $class_name_ed; ?></option>
Ok, @Marc B I'm checking it.
0

Try with this code:

if(isset($_POST['class_name']) && $_POST['class_name'] == $class_name_ed) echo 'selected';

You have an error in the conditional. isset() returns true or false. Also you do not need to wrapp the variable $class_name_ed with "". If you want to be sure that is a string you can cast it like this:

if(isset($_POST['class_name']) && $_POST['class_name'] == (string)$class_name_ed) echo 'selected';

You do not need to use selected="selected" you can mark the option as selected. Three

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.