LondonChris Posted June 30, 2011 Share Posted June 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 30, 2011 Share Posted June 30, 2011 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 30, 2011 Share Posted June 30, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment Share on other sites More sharing options...
LondonChris Posted July 1, 2011 Author Share Posted July 1, 2011 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. Quote Link to comment Share on other sites More sharing options...
LondonChris Posted July 1, 2011 Author Share Posted July 1, 2011 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']; Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 1, 2011 Share Posted July 1, 2011 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> Quote Link to comment Share on other sites More sharing options...
LondonChris Posted July 1, 2011 Author Share Posted July 1, 2011 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. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 1, 2011 Share Posted July 1, 2011 you can simply use trim() on all posted variables to avoid that problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.