5
if(isset($_GET["id"])){
        $sql=mysql_query("SELECT * FROM aMovie WHERE aName= '{$_GET['id']}'"); 
        $row=mysql_fetch_object($sql);  
}

<input type = "text" name = "name" value = "<?php echo $row->aC; ?>"/> 
<select name = "name" >
        <option value = "" <?php echo ($row->aC== "Deadpool") ? 'selected = "selected"': '';?>">Deadpool</option>
        <option value = "" <?php echo ($row->aC == "BATMAN VS SUPERMAN") ? 'selected = "selected"': '';?>">BATMAN VS SUPERMAN</option>
</select>

Assume that aMovie is my table name, and in my table there are aName and and aC. However, I would want to display aName which matches aC ["Deadpool" or "Batman Vs Superman"] and display it in the drop down button. It only works for the input type but not the drop down button.

1
  • 1
    you are putting "selected" inside "value". Commented Apr 12, 2016 at 9:06

4 Answers 4

3

Your <select> should be like:

<select name = "name" >
<option value="Deadpool" <?=($rows->aC == "Deadpool" ? 'selected="selected"': '')?>>Deadpool</option>
<option value="BATMAN VS SUPERMAN" <?=($rows->aC == "BATMAN VS SUPERMAN" ? 'selected="selected"': '')?>>BATMAN VS SUPERMAN</option>
</select>

selected="selected" will use outside the value attribute.

UPDATE:

As @Maninderpreet-Singh mentioned, you also need to change $row to $rows.

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

8 Comments

Doesn't work, it will still display Deadpool though my data is Batman Vs Superman :(
@ikon: var_dump($row->aC == "BATMAN VS SUPERMAN") if its return true, than this will work..
It works for an input box, not the drop down. Drop down still sets Deadpool as the static value
@ikon: var_dump($row->aC) yout value is not equal to BATMAN VS SUPERMAN
@ikon: did u checked: var_dump($row->aC == "BATMAN VS SUPERMAN")
|
1

try to change

<option <?php echo($row->aC== "Deadpool") ? 'selected = "selected"': '';?>  value="<?php echo $row->aC;?>">Deadpool</option>

Comments

1

try with this and you are using different variable in input

<input type = "text" name = "name" value = "<?php echo $rows->aC; ?>"/> 

$row and $rows are different

  <option value = "<?php echo $row->aC; ?>" <?php echo ($row->aC == "Deadpool") ? 'selected':'';?>">Deadpool</option>
  <option value = "<?php echo $row->aC; ?>" <?php echo ($row->aC == "BATMAN VS SUPERMAN") ? 'selected': '';?>">BATMAN VS SUPERMAN</option>

Comments

1
<input type = "text" name = "name1" value = "<?php echo $rows->aC; ?>"/> 
<select name = "name2" >
        <option value = " <?php echo($row->aC); ?>" <?php echo($row->aC=="Deadpool")?'selected': '';?>>Deadpool</option>
        <option value = " <?php echo($row->aC);?>" <?php echo($row->aC == "BATMAN VS SUPERMAN")?'selected': '';?>>BATMAN VS SUPERMAN</option>
</select>

1 Comment

never take name="name"

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.