Jump to content

Pre-populate the selected item of dropdown box based on an ID


LondonChris

Recommended Posts

Hi, I have looked on some forum answers to get to where i am. I think i am close but it seems to just be a syntax issue maybe.

 

if i set the two variant options below. the second version works in the code below to pre-populate the dropdown with "Wap", but i need it to be dynamic and return what ever is in the $record['SubChannel']. but it returns nothing.

i have test echo $SubChannel on the page which does return the text Wap. but i cant get it into the Dropdown box this way?

 

<?php
//Attempt Version 1 does not work
        $SubChannel= $record['SubChannel'];
        
        //Attempt Version 2 works.
       $SubChannel="Wap";

Below is a snipit of my code.

if ($_GET['action'] == 'edit' && isset($_GET['id'])) {
   $r = mysql_query("select * from tblhighlights where ID = ".$_GET['id']);
   $record = mysql_fetch_assoc($r);
   //var_dump ($record);
        
        //Attempt Version 1 does not work
        $SubChannel= $record['SubChannel'];
        
        //Attempt Version 2 works.
       //$SubChannel="Wap";
       
        
        
       echo $SubChannel;
        
      
?>
    
               
   <form method="post" action="">
        <input type="hidden" name="action" value="save">     
        <table id="EditTable">
  
            
            
             <tr>
<td  colspan ="3" class="test1"><strong>Edit Weekly Highlight </strong> </td>
</tr>
<tr>
<td align ="center">Year</td>
<td align ="center">Week</td>
<td align ="center">Channel</td>
<td align ="center">Area</td>
<td align ="center">Type</td>
<td align ="center">Activity</td>
<td align ="center">Person</td>


<tr>
<td valign="top"><input type="text" name="Year" size="5" valign="top" maxlength="4" value="<?=$record['Year']?>"<td>
<td valign="top"><input type="text" name="Week" size="5" maxlength="4" value="<?=$record['Week']?>"<td>       
<td> <input type="text" name="id" value="<?=$record['ID']?>"></td>
<td> <input type="text" name="area" value="<?=$record['Area']?>"></td>
<td valign="">
    <select  name="Channel">
<option value =" ">  </option>  
<option <?php if(isset($SubChannel) && $SubChannel == "Wap"){print "selected=\"selected\"";} ?>>Wap</option>
<option <?php if(isset($record['SubChannel']) && $record['SubChannel'] == "Web"){print "selected=\"selected\"";} ?>>Web</option>
    </select>
</td>



   <td> <input type="submit" name="save"value="Save"> </td>
   </form>
        </Table>

 

MOD EDIT: code tags added.

echo $record['SubChannel'];

 

(to see what's in it, if it's empty, sounds like the $_GET['id'] value does not exist in the database, or the database field names are not the same as `ID` or `SubChannel`

 

change this:

$r = mysql_query("select * from tblhighlights where ID = ".$_GET['id']);

for this:

$r = mysql_query("select * from tblhighlights where ID = ".$_GET['id'])or die(mysql_error());

to see if it spits out an error or not.

 

Hope this helps

Thanks WebStyles.

 

"i have test echo $SubChannel on the page which does return the text Wap. but i cant get it into the Dropdown box this way? "

 

$SubChannel is not empty, as when i echo $SubChannel on the page it returns the correct "Web" or "Wap" options based on the record i selected.

 

This is why i think it may be a syntax issue, when it try and put $SubChannel into the Select dropdown box prepopulated. it shows nothing, but if if just use a hard coded word like "Wap" as $SubChannel it does work.

This is the line that doesn't seem to be recognising the $SubChannel, maybe there are spaces or incorrect syntax or not reading it as text???

 

<option <?php if(isset($SubChannel) && $SubChannel == "Wap"){print "selected=\"selected\"";} ?>>Wap</option>

 

The above works if i set $SubChannel = "Wap";

it does not work if i set  $SubChannel= $record['SubChannel'];

Where's the value tag in your option?

 

options in dropdowns have this format:

 

<option value="something" selected>Something</option>

 

you need to include it:

 

<option <?php if(isset($SubChannel) && $SubChannel == "Wap"){echo ' value="wap" selected="selected"';} ?>>Wap</option>

My Bad!

 

I just checked did a lenght count on $SubChannel and it 5. for the word Wap. Now my math tells me it should be 3 ;)

 

So i realised i had a leading and ending space which was getting including because of the way i was entering the data. see below, there was a gap before and after the Value Wap.

 

<option value =" Wap "> Wap </option>

 

I will fix this up, but i am pretty sure this is the problem, thanks for your help though WebStyles. It made look in the right track.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.